lua objects?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
Carl
Prole
Posts: 18
Joined: Wed Oct 28, 2009 10:32 pm

lua objects?

Post by Carl »

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 :neko:
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: lua objects?

Post by Robin »

Not only can you do that, in Lua it's very very easy:

Code: Select all

player = {}
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:

Code: Select all

player = {speed = 10}
Tables can be used as objects, classes, arrays and much more.

I recommend reading Programming in Lua (http://www.lua.org/pil/index.html).
Help us help you: attach a .love.
User avatar
Carl
Prole
Posts: 18
Joined: Wed Oct 28, 2009 10:32 pm

Re: lua objects?

Post by Carl »

Amazing! I did read about tables but assumed they were only to be used as arrays, turns out they got unlimited uses! :ultrahappy:
User avatar
osgeld
Party member
Posts: 303
Joined: Sun Nov 23, 2008 10:13 pm

Re: lua objects?

Post by osgeld »

yep, you can even stuff functions in them for class like structures
User avatar
Fizzadar
Prole
Posts: 30
Joined: Mon Oct 26, 2009 6:18 pm

Re: lua objects?

Post by Fizzadar »

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!
User avatar
subrime
Citizen
Posts: 76
Joined: Thu Nov 13, 2008 6:18 pm
Location: Australia

Re: lua objects?

Post by subrime »

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!
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: lua objects?

Post by kikito »

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!
When I write def I mean function.
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: lua objects?

Post by kikito »

Hi again,

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.
Post Reply

Who is online

Users browsing this forum: No registered users and 153 guests