OOP in Lua
This template is no longer in use as it results in poor readability.
This is a scripting tutorial teaches you the basics about how to start using an Object-Oriented developing interface with Lua.
- OOP Introduction - MTA-related by qaisjp3
- OOP
- MTA built-in client-sided OO functions
- MTA built-in server-sided OO functions
Glossary
- environment: either a table or an array containing values.
- self: predefined variable referring to the environment within which we are executing the code.
Initialising
There is a basic and simple predefined variables we should recognize and know: self.
Our first environment
local array = {} function array:example (argument) return "Hello" end
What we do upon above is defining a local environment and then declaring the function example as part of it. Alright, so how should we proceed in order to call the mentioned function? As follows:
array:example() array.example(array, example)
As Lua is so cool, we're able to call a function using two methods: ":" and ".". As you can see on the example above, if we use a dot we're supposed to send self's value to the function. Yes, that's right, and in case we use a colon, self's value will be the environment within which we are executing a code, i.e. array. We can send the self value in case we want a function to override its self and, instead of working as if self was the environment within it's working, it will work over the environment we sent (example under advanced examples).
Variables and further handling
local array = {text = "none"} function array:setKey (key, value) self[key] = value end function array:getKey (key) return self[key] end array:getKey("text") -- returns "none" array:setKey("text", "something") -- sets "text"'s value to 'something' array:getKey("text") -- returns "something"
What we do here is retrieving and modifying text's value, which a variable inside array, recurring to functions inside the same environment as the variable is.
Metatables
Content's provenance: NovaFusion
Knowledge of how to use metatables will allow you to be much more powerful in your use of Lua. Every table can have a metatable attached to it. A metatable is a table which, with some certain keys set, can change the behaviour of the table it's attached to. Let's see an example.
t = {} -- our normal table mt = {} -- our metatable, which contains nothing right now setmetatable(t, mt) -- sets mt to be t's metatable getmetatable(t) -- this will return mt
As you can see, getmetatable and setmetatable are the main functions here; I think it's pretty obvious what they do. Of course, in this case we could contract the first three lines into this:
t = setmetatable({}, {})
setmetatable returns its first argument, therefore we can use this shorter form.
Now, what do we put in these metatables? Metatables can contain anything, but they respond to certain keys (which are strings of course) which always start with __ (two underscores in a row), such as __index and __newindex. The values corresponding to these keys will usually be functions or other tables. An example:
t = setmetatable({}, { __index = function(t, key) if key == "foo" then return 0 else return table[key] end end })
So as you can see, we assign a function to the __index key. Now let's have a look at what this key is all about.
__index
The most used metatable key is most likely __index; it can contain either a function or table.
When you look up a table with a key, regardless of what the key is (t[4], t.foo, and t["foo"], for example), and a value hasn't been assigned for that key, Lua will look for an __index key in the table's metatable (if it has a metatable). If __index contains a table, Lua will look up the key originally used in the table belonging to __index. This probably sounds confusing, so here's an example:
other = { foo = 3 } t = setmetatable({}, { __index = other }) t.foo -- 3 t.bar -- nil
If __index contains a function, then it's called, with the table that is being looked up and the key used as parameters. As we saw in the code example above the last one, this allows us to use conditionals on the key, and basically anything else that Lua code can do. Therefore, in that example, if the key was equal to the string "foo" we would return 0, otherwise we look up the table table with the key that was used; this makes t an alias of table that returns 0 when the key "foo" is used.
You may be wondering why the table is passed as a first parameter to the __index function. This comes in handy if you use the same metatable for multiple tables, supporting code re-use and saving computer resources. We'll see an example of this when we take a look at the Vector class.
__newindex
Next in line is __newindex, which is similar to __index. Like __index, it can contain either a function or table.
When you try to set a value in a table that is not already present, Lua will look for a __newindex key in the metatable. It's the same sort of situation as __index; if __newindex is a table, the key and value will be set in the table specified:
Advanced examples
Overriding self's value:
local array = {text = "none"} local array2 = {text = "none2"} function array:setKey (key, value) self[key] = value end function array:getKey (key) return self[key] end array.getKey(array2, "text") -- returns "none2" array.setKey(array2, "text", "something2") -- sets array2's "text" value to 'something2' array.getKey(array2, "text") -- returns "something" array:getKey("text") -- returns "none" array.getKey(array, "text") -- same as above
A simple backpacks example:
local backpack = {list = {}} function backpack:create (owner, slots) if self.list[owner] then return end -- return false in case this player already owns a backpack local new = { items = {}, slots = slots or 100, owner = owner } setmetatable(new.items, { __newindex = function(table, key, value) -- this is called once a new value/item is added into the player's table/backpack if #table >= new.slots then return end -- return false in case there isn't any free slot left return rawset(table, key, value) end } ) self.list[owner] = new end function backpack:addItem (player, item, value) if not self.list[player] then return end self.list[player][item] = value end