• Lua Objects
    4 replies, posted
Hi, I've just started teaching myself lua and things are going great except I'm not quite sure how to create objects like in other scripting languages like java and c#. I'll be mostly using lua for creating games using Love2d. Could anyone give me a run through on how exactly to do this (I think there is more then one).
Take a look through the Gwilty Programming Assignment #2 Thread, this is discussed extensively there.
[QUOTE=subenji99;33119188]Take a look through the Gwilty Programming Assignment #2 Thread, this is discussed extensively there.[/QUOTE] Oh, thanks I'll do that!
Lua doesn't have objects in the traditional sense. Metatables can be used to accomplish the same goals.
[QUOTE=ROBO_DONUT;33122008]Lua doesn't have objects in the traditional sense. Metatables can be used to accomplish the same goals.[/QUOTE] To, um... 'rephrase' this: In Java and C++, you have [b]types[/b]. A [B]int[/B] is a type, so a is a [B]boolean[/B], and so is a [B]char[][/B] (array of characters). You can also create your own types, using the [B]class[/B] keyword (as I'm sure you are aware). The thing with Class types is that, two are not the same: The type of class A, is never the same as the type of class B (even if they inherit a common class). When C++ or Java is compiled, the compiler is aware of this. If you went: [cpp] class A { int thing; } class B { int not_a_thing; } int main() { A a(); a.not_a_thing = 7; // fail std::cout << a.not_a_thing; // more fail } [/cpp] The compiler cannot find "not_a_thing" in A at compile time. Lua, on the contrary, is not a compiled language, and is very dynamic. While it still has [b]types[/b], you cannot create your own: there are no 'type extensions'. The only available types are: [I]nil, boolean, number, string, function, thread,[/I] and [I]table[/I]. You can only 'simulate' classes in Lua. The way to do this is to change the [B]behaviour[/B] of a value. The only type you can do this with is [B]table[/B]. Any attempt to do it to other types will resulting in you changing the behaviour of EVERY other value of that type; whereas with tables, you can change the behaviour of each individual table. The way to do this is with [B]metatables[/B]. The [B]meta-[/B] prefix means "something about something". So, [B]metadata[/B] means "data about data", and a [B]metatable[/B] is a "table about a table". For example: [lua] metat = { __add = function(a, b) local new_t = {} for i, v in ipairs(a) do table.insert(new_t, v) end for i, v in ipairs(b) do table.insert(new_t, v) end return new_t end } t1 = {"apple", "banana"} setmetatable(t1, metat) t2 = {"mango", "pear"} setmetatable(t2, metat) t3 = {"pomegranate", "pineapple"} t4 = {"peach", "orange"} print(table.concat(t1, ", ")) print(table.concat(t1+t2, ", ")) -- This works, because it uses the first metatable it finds (t1's metatable here, not that it matters in this case, because they both use the same metatable). print(table.concat(t1+t3, ", ")) -- This works as well! Why? Because t3 doesn't have a metatable, so it uses t1's print(table.concat(t4+t1, ", ")) -- Same here! print(table.concat(t3+t4, ", ")) -- This will error :( Why? Because Lua can't find a metatable to tell it what to do! There is no "default" behaviour for adding tables. [/lua] I'm hoping you'll see how you can use this to simulate classes. The [I]__add[/I] [B]metamethod[/B] is the same as [B]operator+[/B] in C++. If you wish to control [B]t.something[/B] and [B]t["something"][/B], you can define [I]__index[/I] and [I]__newindex[/I]. If you wish to make the table callable (only works in Lua 5.2!), as in [B]t(a, b, c)[/B] you can define [I]__call[/I]. I suggest you take a look at an introduction I wrote a bazillion years ago: [url=http://wiki.garrysmod.com/?title=User:Deco_Da_Man]What are metatables?[/url] You seem like you're a few IQ levels about it's target audience... but it should still give you some insight. I'll leave you with this quote: "I&#8217;m So Meta, Even This Acronym" Good luck in your future endeavours ;)
Sorry, you need to Log In to post a reply to this thread.