Hi I'm new to LUA after coming from javascript/c# background. At this stage I'm just familiarizing my self with LUA and love but I'm curious as to how I go about creating custom objects?
For example in javascript I could create an object such as "player" then from that I can do stuff like player.speed = 10; Is something like that possible in LUA and if so how do I go about doing it?
Thanks
lua objects?
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
- Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
- Contact:
Re: lua objects?
Not only can you do that, in Lua it's very very easy:
There. You have made a new table, and bound the name "player" to it. Now you can set player.speed = 10. You can also set entries at table creation time:
Tables can be used as objects, classes, arrays and much more.
I recommend reading Programming in Lua (http://www.lua.org/pil/index.html).
Code: Select all
player = {}
Code: Select all
player = {speed = 10}
I recommend reading Programming in Lua (http://www.lua.org/pil/index.html).
Help us help you: attach a .love.
Re: lua objects?
Amazing! I did read about tables but assumed they were only to be used as arrays, turns out they got unlimited uses!
Re: lua objects?
yep, you can even stuff functions in them for class like structures
Re: lua objects?
I used to think tables == arrays, but then I discovered the use of
table:Something( lol ) will basically allow you to use self inside and ... I just feel all gewy inside
Lua is awesome!
table:Something( lol ) will basically allow you to use self inside and ... I just feel all gewy inside
Lua is awesome!
http://love2d.org/forum/viewtopic.php?f=3&t=1016 << epic name!
Re: lua objects?
but it doesn't stop there... you can also set up a __call function in the metatable and then use the table the same way as a function (while still keeping all its other awesomeness):
Code: Select all
t={}
t.note='associative arrays'
meta={__call=function(self,x) print(self.note..' and metatables are '..tostring(x)) end}
setmetatable(t,meta)
t('awesome!')
>associative arrays and metatables are awesome!
- kikito
- Inner party member
- Posts: 3153
- Joined: Sat Oct 03, 2009 5:22 pm
- Location: Madrid, Spain
- Contact:
Re: lua objects?
Hi Carl,
Lua is so flexible that by using metatables and metamethods it is possible to replicate C#/Java classes and instances functionality (also inheritance, mixins...)
I've done so in my PÄSSION lib (http://love2d.org/forum/viewtopic.php?f=5&t=1039). Concretely, the part that implements Object Orientation is a 100-lines file called "MiddleClass.lua" (It has its own google project page: http://code.google.com/p/lua-middleclass/)
Finally, there's more information about "traditional" object orientation in Lua on the Lua-users wiki: http://lua-users.org/wiki/ObjectOrientedProgramming
Regards!
Lua is so flexible that by using metatables and metamethods it is possible to replicate C#/Java classes and instances functionality (also inheritance, mixins...)
I've done so in my PÄSSION lib (http://love2d.org/forum/viewtopic.php?f=5&t=1039). Concretely, the part that implements Object Orientation is a 100-lines file called "MiddleClass.lua" (It has its own google project page: http://code.google.com/p/lua-middleclass/)
Finally, there's more information about "traditional" object orientation in Lua on the Lua-users wiki: http://lua-users.org/wiki/ObjectOrientedProgramming
Regards!
When I write def I mean function.
- kikito
- Inner party member
- Posts: 3153
- Joined: Sat Oct 03, 2009 5:22 pm
- Location: Madrid, Spain
- Contact:
Re: lua objects?
Hi again,
I've created a wiki page for MiddleClass, with examples and everything, if you are still interested on this stuff.
Regards!
I've created a wiki page for MiddleClass, with examples and everything, if you are still interested on this stuff.
Regards!
When I write def I mean function.
Who is online
Users browsing this forum: No registered users and 153 guests