<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.multitheftauto.com/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Novo</id>
	<title>Multi Theft Auto: Wiki - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.multitheftauto.com/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Novo"/>
	<link rel="alternate" type="text/html" href="https://wiki.multitheftauto.com/wiki/Special:Contributions/Novo"/>
	<updated>2026-04-27T10:46:36Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.39.3</generator>
	<entry>
		<id>https://wiki.multitheftauto.com/index.php?title=User:Novo&amp;diff=44023</id>
		<title>User:Novo</title>
		<link rel="alternate" type="text/html" href="https://wiki.multitheftauto.com/index.php?title=User:Novo&amp;diff=44023"/>
		<updated>2015-01-18T13:28:55Z</updated>

		<summary type="html">&lt;p&gt;Novo: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;__NOTOC__&lt;br /&gt;
== Contact me ==&lt;br /&gt;
=== Forum ===&lt;br /&gt;
* [http://forum.mtasa.com/memberlist.php?mode=viewprofile&amp;amp;u=53356 novo] - [http://forum.mtasa.com/ucp.php?i=pm&amp;amp;mode=compose&amp;amp;u=53356 PM]&lt;br /&gt;
&lt;br /&gt;
=== Skype ===&lt;br /&gt;
* Adam James @adam.mta&lt;br /&gt;
&lt;br /&gt;
=== E-mail ===&lt;br /&gt;
''&amp;lt;nowiki&amp;gt;(though Skype is preferred)&amp;lt;/nowiki&amp;gt;''&lt;br /&gt;
* Adam James - ''htx@hotmail.es''&lt;br /&gt;
&lt;br /&gt;
== Latest contributions ==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|'''Date'''&lt;br /&gt;
|'''Title'''&lt;br /&gt;
|'''Any extra information'''&lt;br /&gt;
|-&lt;br /&gt;
| 18 January 2015&lt;br /&gt;
| [[OOP_in_Lua|OOP in Lua]]&lt;br /&gt;
| ''Renamed from OOP Basis to OOP in Lua''&lt;br /&gt;
|-&lt;/div&gt;</summary>
		<author><name>Novo</name></author>
	</entry>
	<entry>
		<id>https://wiki.multitheftauto.com/index.php?title=User:Novo&amp;diff=44022</id>
		<title>User:Novo</title>
		<link rel="alternate" type="text/html" href="https://wiki.multitheftauto.com/index.php?title=User:Novo&amp;diff=44022"/>
		<updated>2015-01-18T13:28:16Z</updated>

		<summary type="html">&lt;p&gt;Novo: Created page with &amp;quot;__NOTOC__ == Contact me == === Forum === * [http://forum.mtasa.com/memberlist.php?mode=viewprofile&amp;amp;u=53356 novo] - [http://forum.mtasa.com/ucp.php?i=pm&amp;amp;mode=compose&amp;amp;u=53356 PM...&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;__NOTOC__&lt;br /&gt;
== Contact me ==&lt;br /&gt;
=== Forum ===&lt;br /&gt;
* [http://forum.mtasa.com/memberlist.php?mode=viewprofile&amp;amp;u=53356 novo] - [http://forum.mtasa.com/ucp.php?i=pm&amp;amp;mode=compose&amp;amp;u=53356 PM]&lt;br /&gt;
&lt;br /&gt;
=== Skype ===&lt;br /&gt;
* Adam James @adam.mta&lt;br /&gt;
&lt;br /&gt;
=== E-mail ===&lt;br /&gt;
 ''(though Skype is preferred)''&lt;br /&gt;
* Adam James - ''htx@hotmail.es''&lt;br /&gt;
&lt;br /&gt;
== Latest contributions ==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|'''Date'''&lt;br /&gt;
|'''Title'''&lt;br /&gt;
|'''Any extra information'''&lt;br /&gt;
|-&lt;br /&gt;
| 18 January 2015&lt;br /&gt;
| [[OOP_in_Lua|OOP in Lua]]&lt;br /&gt;
| ''Renamed from OOP Basis to OOP in Lua''&lt;br /&gt;
|-&lt;/div&gt;</summary>
		<author><name>Novo</name></author>
	</entry>
	<entry>
		<id>https://wiki.multitheftauto.com/index.php?title=OOP_in_Lua&amp;diff=44012</id>
		<title>OOP in Lua</title>
		<link rel="alternate" type="text/html" href="https://wiki.multitheftauto.com/index.php?title=OOP_in_Lua&amp;diff=44012"/>
		<updated>2015-01-18T03:46:45Z</updated>

		<summary type="html">&lt;p&gt;Novo: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;__TOC__&lt;br /&gt;
== Introduction and other related pages ==&lt;br /&gt;
This is a scripting tutorial teaches you the basics about how to start using an Object-Oriented developing interface with Lua. Originally created by [http://forum.mtasa.com/memberlist.php?mode=viewprofile&amp;amp;u=53356 novo] @adam.mta. - [http://forum.mtasa.com/viewtopic.php?f=148&amp;amp;t=84365 Forum topic].&lt;br /&gt;
* [[OOP_Introduction|OOP Introduction - MTA-related]] ''by qaisjp3''&lt;br /&gt;
* [[OOP|OOP]]&lt;br /&gt;
* [[OOP_client|MTA built-in client-sided OO functions]]&lt;br /&gt;
* [[OOP_server|MTA built-in server-sided OO functions]]&lt;br /&gt;
&lt;br /&gt;
== Glossary ==&lt;br /&gt;
* environment: either a table or an array containing values.&lt;br /&gt;
* self: predefined variable referring to the environment within which we are executing the code.&lt;br /&gt;
&lt;br /&gt;
== Initialising ==&lt;br /&gt;
There is a basic and simple predefined variables we should recognize and know: '''[[#Glossary|self]]'''.&lt;br /&gt;
=== Our first environment ===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
local array = {}&lt;br /&gt;
function array:example (argument)&lt;br /&gt;
	return &amp;quot;Hello&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
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:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
array:example()&lt;br /&gt;
array.example(array, example)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
As Lua is so cool, we're able to call a function using two methods: &amp;quot;''':'''&amp;quot; and &amp;quot;'''.'''&amp;quot;. 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|advanced examples]]).&lt;br /&gt;
&lt;br /&gt;
=== Variables and further handling ===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
local array = {text = &amp;quot;none&amp;quot;}&lt;br /&gt;
function array:setKey (key, value)&lt;br /&gt;
	self[key] = value&lt;br /&gt;
end&lt;br /&gt;
function array:getKey (key)&lt;br /&gt;
	return self[key]&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
array:getKey(&amp;quot;text&amp;quot;) -- returns &amp;quot;none&amp;quot;&lt;br /&gt;
array:setKey(&amp;quot;text&amp;quot;, &amp;quot;something&amp;quot;) -- sets &amp;quot;text&amp;quot;'s value to 'something'&lt;br /&gt;
array:getKey(&amp;quot;text&amp;quot;) -- returns &amp;quot;something&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
== Metatables ==&lt;br /&gt;
Content's provenance: [http://nova-fusion.com/2011/06/30/lua-metatables-tutorial/ NovaFusion]&lt;br /&gt;
----&lt;br /&gt;
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.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
t = {} -- our normal table&lt;br /&gt;
mt = {} -- our metatable, which contains nothing right now&lt;br /&gt;
setmetatable(t, mt) -- sets mt to be t's metatable&lt;br /&gt;
getmetatable(t) -- this will return mt&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
t = setmetatable({}, {})&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
'''setmetatable''' returns its first argument, therefore we can use this shorter form.&lt;br /&gt;
&lt;br /&gt;
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:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
t = setmetatable({}, {&lt;br /&gt;
  __index = function(t, key)&lt;br /&gt;
    if key == &amp;quot;foo&amp;quot; then&lt;br /&gt;
      return 0&lt;br /&gt;
    else&lt;br /&gt;
      return table[key]&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
})&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
=== __index ===&lt;br /&gt;
&lt;br /&gt;
The most used metatable key is most likely '''__index'''; it can contain either a function or table.&lt;br /&gt;
&lt;br /&gt;
When you look up a table with a key, regardless of what the key is (t[4], t.foo, and t[&amp;quot;foo&amp;quot;], 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:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
other = { foo = 3 }&lt;br /&gt;
t = setmetatable({}, { __index = other })&lt;br /&gt;
t.foo -- 3&lt;br /&gt;
t.bar -- nil&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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 &amp;quot;foo&amp;quot; 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 &amp;quot;foo&amp;quot; is used.&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
=== __newindex ===&lt;br /&gt;
&lt;br /&gt;
Next in line is '''__newindex''', which is similar to '''__index'''. Like '''__index''', it can contain either a function or table.&lt;br /&gt;
&lt;br /&gt;
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:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
other = {}&lt;br /&gt;
t = setmetatable({}, { __newindex = other })&lt;br /&gt;
t.foo = 3&lt;br /&gt;
other.foo -- 3&lt;br /&gt;
t.foo -- nil&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
As would be expected, if '''__newindex''' is a function, it will be called with the table, key, and value passed as parameters:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
t = setmetatable({}, {&lt;br /&gt;
  __newindex = function(t, key, value)&lt;br /&gt;
    if type(value) == &amp;quot;number&amp;quot; then&lt;br /&gt;
      rawset(t, key, value * value)&lt;br /&gt;
    else&lt;br /&gt;
      rawset(t, key, value)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
})&lt;br /&gt;
&lt;br /&gt;
t.foo = &amp;quot;foo&amp;quot;&lt;br /&gt;
t.bar = 4&lt;br /&gt;
t.la = 10&lt;br /&gt;
t.foo -- &amp;quot;foo&amp;quot;&lt;br /&gt;
t.bar -- 16&lt;br /&gt;
t.la -- 100&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
When creating a new key in '''t''', if the value is a number it will be squared, otherwise it will just be set anyway. This introduces us to our friends, '''rawget''' and '''rawset'''.&lt;br /&gt;
&lt;br /&gt;
=== rawget and rawset ===&lt;br /&gt;
&lt;br /&gt;
There are times when you need get and set a table's keys without having Lua do it's thing with metatables. As you might guess, [http://www.lua.org/manual/5.1/manual.html#pdf-rawget rawget] allows you to get the value of a key without Lua using '''__index''', and [http://www.lua.org/manual/5.1/manual.html#pdf-rawset rawset] allows you to set the value of a key without Lua using '''__newindex''' (no these don't provide a speed increase to conventional way of doing things). You'll need to use these when you would otherwise get stuck in an infinite loop. For example, in that last code example, the code '''t[key] = value * value''' would set off the same '''__newindex''' function again, which would get you stuck in an infinite loop. Using '''rawset(t, key, value * value)''' avoids this.&lt;br /&gt;
&lt;br /&gt;
As you probably can see, to use these functions, for parameters we must pass in the target table, the key, and if you're using '''rawset''', the value.&lt;br /&gt;
&lt;br /&gt;
=== Operators ===&lt;br /&gt;
&lt;br /&gt;
Many of the metatable keys available have to do with operators (as in, '''+''', '''-''', etc.), allowing you to make tables support the use of operators on them. For example, say we wanted a table to support the multiplication operator ('''*'''), this is how we would do it:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
t = setmetatable({ 1, 2, 3 }, {&lt;br /&gt;
  __mul = function(t, other)&lt;br /&gt;
    new = {}&lt;br /&gt;
    &lt;br /&gt;
    for i = 1, other do&lt;br /&gt;
      for _, v in ipairs(t) do table.insert(new, v) end&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    return new&lt;br /&gt;
  end&lt;br /&gt;
})&lt;br /&gt;
&lt;br /&gt;
t = t * 2 -- { 1, 2, 3, 1, 2, 3 }&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This allows us to create a new table with the original replicated a certain amount of times using the multiplication operator. As you can tell, the corresponding key for multiplication is '''__mul'''; unlike '''__index''' and '''__newindex''' the keys for operators can only contain functions. The first parameter these functions always receive is the target table, and then the value on the right hand side (except for the unary - which has the key of '''__unm'''). Here's a list of the supported operators:&lt;br /&gt;
&lt;br /&gt;
* __add: Addition (+)&lt;br /&gt;
* __sub: Subtraction (-)&lt;br /&gt;
* __mul: Multiplication (*)&lt;br /&gt;
* __div: Division (/)&lt;br /&gt;
* __mod: Modulos (%)&lt;br /&gt;
* __unm: Unary -, used for negation on numbers&lt;br /&gt;
* __concat: Concatenation (..)&lt;br /&gt;
* __eq: Equality (==)&lt;br /&gt;
* __lt: Less than (&amp;lt;)&lt;br /&gt;
* __le: Less than or equal to (&amp;lt;=)&lt;br /&gt;
&lt;br /&gt;
(There's only '''==''', '''&amp;lt;''', '''&amp;lt;=''' because you can implement full support for the comparison operators with just those; in fact only '''==''' and '''&amp;lt;''' are needed.)&lt;br /&gt;
&lt;br /&gt;
=== __call ===&lt;br /&gt;
&lt;br /&gt;
Next comes the '''__call''' key, which allows you to call tables as functions. A code example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
t = setmetatable({}, {&lt;br /&gt;
  __call = function(t, a, b, c, whatever)&lt;br /&gt;
    return (a + b + c) * whatever&lt;br /&gt;
  end&lt;br /&gt;
})&lt;br /&gt;
&lt;br /&gt;
t(1, 2, 3, 4) -- 24&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The function in call is passed the target table as usual, followed by the parameters that we passed in. '''__call''' is very useful for many things. One common thing it's used for is forwarding a call on a table to a function inside that table.&lt;br /&gt;
&lt;br /&gt;
=== __tostring ===&lt;br /&gt;
&lt;br /&gt;
Last of all is '''__tostring'''. If implemented, it's used by [http://www.lua.org/manual/5.1/manual.html#pdf-tostring tostring] to convert a table into a string, most handy when using a function like [http://www.lua.org/manual/5.1/manual.html#pdf-print print]. Normally, when you try to convert a table to a string, you something in the format of &amp;quot;table: 0x&amp;lt;hex-code-here&amp;gt;&amp;quot;, but you can change that using '''__tostring'''. An example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
t = setmetatable({ 1, 2, 3 }, {&lt;br /&gt;
  __tostring = function(t)&lt;br /&gt;
    sum = 0&lt;br /&gt;
    for _, v in pairs(t) do sum = sum + v end&lt;br /&gt;
    return &amp;quot;Sum: &amp;quot; .. sum&lt;br /&gt;
  end&lt;br /&gt;
})&lt;br /&gt;
&lt;br /&gt;
print(t) -- prints out &amp;quot;Sum: 6&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Building the Vector Class ===&lt;br /&gt;
&lt;br /&gt;
To wrap everything up, we'll write a class encapsulating a 2D vector. It's too large to put here, but you can see the full code at [https://gist.github.com/1055480 gist #1055480]. I've positioned all the stuff to do with metatables first in the file, as that's the most important stuff. (Be warned, this may be a bit confusing if you've never encountered Object-Oriented Programming before.)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
Vector = {}&lt;br /&gt;
Vector.__index = Vector&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This code sets up the table for the '''Vector''' class, and sets the '''__index''' key to point back at itself. Now, what's going on here? You've probably noticed that we've put all the metatable methods inside the '''Vector''' class. What you're seeing is the simplest way to achieve OOP (Object-Oriented Programming) in Lua. The '''Vector''' table represents the class, which contains all the functions that instances can use. '''Vector.new''' (shown below) creates a new instance of this class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
function Vector.new(x, y)&lt;br /&gt;
  return setmetatable({ x = x or 0, y = y or 0 }, Vector)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It creates a new table with '''x''' and '''y''' properties, and then sets the metatable to the '''Vector''' class. As we know, '''Vector''' contains all the metamethods and especially the '''__index''' key. This means that we can use all the functions we define in '''Vector''' through this new table. We'll come back to this in a moment.&lt;br /&gt;
&lt;br /&gt;
Another important thing is the last line:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
setmetatable(Vector, { __call = function(_, ...) return Vector.new(...) end })&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This means that we can create a new '''Vector''' instance by either calling '''Vector.new''' or just '''Vector'''.&lt;br /&gt;
&lt;br /&gt;
The last important thing that you may not be aware of is the colon syntax. When we define a function with a colon, like this:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
function t:method(a, b, c)&lt;br /&gt;
  -- ...&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
What we are really doing is defining this function:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
function t.method(self, a, b, c)&lt;br /&gt;
  -- ...&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is syntactic sugar to help with OOP. We can then use the colon syntax when calling functions:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
-- these are the same&lt;br /&gt;
t:method(1, 2, 3)&lt;br /&gt;
t.method(t, 1, 2, 3)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now, how do we use this '''Vector''' class? Here's a final example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
a = Vector.new(10, 10)&lt;br /&gt;
b = Vector(20, 11)&lt;br /&gt;
c = a + b&lt;br /&gt;
print(a:len()) -- 14.142135623731&lt;br /&gt;
print(a) -- (10, 10)&lt;br /&gt;
print(c) -- (30, 21)&lt;br /&gt;
print(a &amp;lt; c) -- true&lt;br /&gt;
print(a == b) -- false&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Because of the '''__index''' in '''Vector''', we can use all the methods defined in the class through the instances.&lt;br /&gt;
&lt;br /&gt;
== Advanced examples ==&lt;br /&gt;
Overriding self's value:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
local array = {text = &amp;quot;none&amp;quot;}&lt;br /&gt;
local array2 = {text = &amp;quot;none2&amp;quot;}&lt;br /&gt;
function array:setKey (key, value)&lt;br /&gt;
	self[key] = value&lt;br /&gt;
end&lt;br /&gt;
function array:getKey (key)&lt;br /&gt;
	return self[key]&lt;br /&gt;
end&lt;br /&gt;
array.getKey(array2, &amp;quot;text&amp;quot;) -- returns &amp;quot;none2&amp;quot;&lt;br /&gt;
array.setKey(array2, &amp;quot;text&amp;quot;, &amp;quot;something2&amp;quot;) -- sets array2's &amp;quot;text&amp;quot; value to 'something2'&lt;br /&gt;
array.getKey(array2, &amp;quot;text&amp;quot;) -- returns &amp;quot;something&amp;quot;&lt;br /&gt;
array:getKey(&amp;quot;text&amp;quot;) -- returns &amp;quot;none&amp;quot;&lt;br /&gt;
array.getKey(array, &amp;quot;text&amp;quot;) -- same as above&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A simple backpacks example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
local backpack = {list = {}}&lt;br /&gt;
function backpack:create (owner, slots)&lt;br /&gt;
	if self.list[owner] then return end -- return false in case this player already owns a backpack&lt;br /&gt;
		local new = {&lt;br /&gt;
		items = {},&lt;br /&gt;
		slots = slots or 100,&lt;br /&gt;
		owner = owner&lt;br /&gt;
	}&lt;br /&gt;
	setmetatable(new.items, &lt;br /&gt;
	{&lt;br /&gt;
		__newindex = function(table, key, value) -- this is called once a new value/item is added into the player's table/backpack&lt;br /&gt;
			if #table &amp;gt;= new.slots then return end -- return false in case there isn't any free slot left&lt;br /&gt;
			return rawset(table, key, value)&lt;br /&gt;
		end&lt;br /&gt;
	}&lt;br /&gt;
	)&lt;br /&gt;
	self.list[owner] = new&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function backpack:addItem (player, item, value)&lt;br /&gt;
	if not self.list[player] then return end&lt;br /&gt;
	self.list[player][item] = value&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Latest modifications ==&lt;br /&gt;
--[[User:Novo|Novo]] ([[User talk:Novo|talk]]) 03:43, 18 January 2015 (UTC) (Adam James @adam.mta)&lt;/div&gt;</summary>
		<author><name>Novo</name></author>
	</entry>
	<entry>
		<id>https://wiki.multitheftauto.com/index.php?title=OOP_in_Lua&amp;diff=44011</id>
		<title>OOP in Lua</title>
		<link rel="alternate" type="text/html" href="https://wiki.multitheftauto.com/index.php?title=OOP_in_Lua&amp;diff=44011"/>
		<updated>2015-01-18T03:45:04Z</updated>

		<summary type="html">&lt;p&gt;Novo: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;__TOC__&lt;br /&gt;
== Introduction and other related pages ==&lt;br /&gt;
This is a scripting tutorial teaches you the basics about how to start using an Object-Oriented developing interface with Lua. Originally created by [http://forum.mtasa.com/memberlist.php?mode=viewprofile&amp;amp;u=53356 Adam James] @adam.mta. - [http://forum.mtasa.com/viewtopic.php?f=148&amp;amp;t=84365 Forum topic].&lt;br /&gt;
* [[OOP_Introduction|OOP Introduction - MTA-related]] ''by qaisjp3''&lt;br /&gt;
* [[OOP|OOP]]&lt;br /&gt;
* [[OOP_client|MTA built-in client-sided OO functions]]&lt;br /&gt;
* [[OOP_server|MTA built-in server-sided OO functions]]&lt;br /&gt;
&lt;br /&gt;
== Glossary ==&lt;br /&gt;
* environment: either a table or an array containing values.&lt;br /&gt;
* self: predefined variable referring to the environment within which we are executing the code.&lt;br /&gt;
&lt;br /&gt;
== Initialising ==&lt;br /&gt;
There is a basic and simple predefined variables we should recognize and know: '''[[#Glossary|self]]'''.&lt;br /&gt;
=== Our first environment ===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
local array = {}&lt;br /&gt;
function array:example (argument)&lt;br /&gt;
	return &amp;quot;Hello&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
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:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
array:example()&lt;br /&gt;
array.example(array, example)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
As Lua is so cool, we're able to call a function using two methods: &amp;quot;''':'''&amp;quot; and &amp;quot;'''.'''&amp;quot;. 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|advanced examples]]).&lt;br /&gt;
&lt;br /&gt;
=== Variables and further handling ===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
local array = {text = &amp;quot;none&amp;quot;}&lt;br /&gt;
function array:setKey (key, value)&lt;br /&gt;
	self[key] = value&lt;br /&gt;
end&lt;br /&gt;
function array:getKey (key)&lt;br /&gt;
	return self[key]&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
array:getKey(&amp;quot;text&amp;quot;) -- returns &amp;quot;none&amp;quot;&lt;br /&gt;
array:setKey(&amp;quot;text&amp;quot;, &amp;quot;something&amp;quot;) -- sets &amp;quot;text&amp;quot;'s value to 'something'&lt;br /&gt;
array:getKey(&amp;quot;text&amp;quot;) -- returns &amp;quot;something&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
== Metatables ==&lt;br /&gt;
Content's provenance: [http://nova-fusion.com/2011/06/30/lua-metatables-tutorial/ NovaFusion]&lt;br /&gt;
----&lt;br /&gt;
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.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
t = {} -- our normal table&lt;br /&gt;
mt = {} -- our metatable, which contains nothing right now&lt;br /&gt;
setmetatable(t, mt) -- sets mt to be t's metatable&lt;br /&gt;
getmetatable(t) -- this will return mt&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
t = setmetatable({}, {})&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
'''setmetatable''' returns its first argument, therefore we can use this shorter form.&lt;br /&gt;
&lt;br /&gt;
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:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
t = setmetatable({}, {&lt;br /&gt;
  __index = function(t, key)&lt;br /&gt;
    if key == &amp;quot;foo&amp;quot; then&lt;br /&gt;
      return 0&lt;br /&gt;
    else&lt;br /&gt;
      return table[key]&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
})&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
=== __index ===&lt;br /&gt;
&lt;br /&gt;
The most used metatable key is most likely '''__index'''; it can contain either a function or table.&lt;br /&gt;
&lt;br /&gt;
When you look up a table with a key, regardless of what the key is (t[4], t.foo, and t[&amp;quot;foo&amp;quot;], 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:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
other = { foo = 3 }&lt;br /&gt;
t = setmetatable({}, { __index = other })&lt;br /&gt;
t.foo -- 3&lt;br /&gt;
t.bar -- nil&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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 &amp;quot;foo&amp;quot; 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 &amp;quot;foo&amp;quot; is used.&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
=== __newindex ===&lt;br /&gt;
&lt;br /&gt;
Next in line is '''__newindex''', which is similar to '''__index'''. Like '''__index''', it can contain either a function or table.&lt;br /&gt;
&lt;br /&gt;
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:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
other = {}&lt;br /&gt;
t = setmetatable({}, { __newindex = other })&lt;br /&gt;
t.foo = 3&lt;br /&gt;
other.foo -- 3&lt;br /&gt;
t.foo -- nil&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
As would be expected, if '''__newindex''' is a function, it will be called with the table, key, and value passed as parameters:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
t = setmetatable({}, {&lt;br /&gt;
  __newindex = function(t, key, value)&lt;br /&gt;
    if type(value) == &amp;quot;number&amp;quot; then&lt;br /&gt;
      rawset(t, key, value * value)&lt;br /&gt;
    else&lt;br /&gt;
      rawset(t, key, value)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
})&lt;br /&gt;
&lt;br /&gt;
t.foo = &amp;quot;foo&amp;quot;&lt;br /&gt;
t.bar = 4&lt;br /&gt;
t.la = 10&lt;br /&gt;
t.foo -- &amp;quot;foo&amp;quot;&lt;br /&gt;
t.bar -- 16&lt;br /&gt;
t.la -- 100&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
When creating a new key in '''t''', if the value is a number it will be squared, otherwise it will just be set anyway. This introduces us to our friends, '''rawget''' and '''rawset'''.&lt;br /&gt;
&lt;br /&gt;
=== rawget and rawset ===&lt;br /&gt;
&lt;br /&gt;
There are times when you need get and set a table's keys without having Lua do it's thing with metatables. As you might guess, [http://www.lua.org/manual/5.1/manual.html#pdf-rawget rawget] allows you to get the value of a key without Lua using '''__index''', and [http://www.lua.org/manual/5.1/manual.html#pdf-rawset rawset] allows you to set the value of a key without Lua using '''__newindex''' (no these don't provide a speed increase to conventional way of doing things). You'll need to use these when you would otherwise get stuck in an infinite loop. For example, in that last code example, the code '''t[key] = value * value''' would set off the same '''__newindex''' function again, which would get you stuck in an infinite loop. Using '''rawset(t, key, value * value)''' avoids this.&lt;br /&gt;
&lt;br /&gt;
As you probably can see, to use these functions, for parameters we must pass in the target table, the key, and if you're using '''rawset''', the value.&lt;br /&gt;
&lt;br /&gt;
=== Operators ===&lt;br /&gt;
&lt;br /&gt;
Many of the metatable keys available have to do with operators (as in, '''+''', '''-''', etc.), allowing you to make tables support the use of operators on them. For example, say we wanted a table to support the multiplication operator ('''*'''), this is how we would do it:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
t = setmetatable({ 1, 2, 3 }, {&lt;br /&gt;
  __mul = function(t, other)&lt;br /&gt;
    new = {}&lt;br /&gt;
    &lt;br /&gt;
    for i = 1, other do&lt;br /&gt;
      for _, v in ipairs(t) do table.insert(new, v) end&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    return new&lt;br /&gt;
  end&lt;br /&gt;
})&lt;br /&gt;
&lt;br /&gt;
t = t * 2 -- { 1, 2, 3, 1, 2, 3 }&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This allows us to create a new table with the original replicated a certain amount of times using the multiplication operator. As you can tell, the corresponding key for multiplication is '''__mul'''; unlike '''__index''' and '''__newindex''' the keys for operators can only contain functions. The first parameter these functions always receive is the target table, and then the value on the right hand side (except for the unary - which has the key of '''__unm'''). Here's a list of the supported operators:&lt;br /&gt;
&lt;br /&gt;
* __add: Addition (+)&lt;br /&gt;
* __sub: Subtraction (-)&lt;br /&gt;
* __mul: Multiplication (*)&lt;br /&gt;
* __div: Division (/)&lt;br /&gt;
* __mod: Modulos (%)&lt;br /&gt;
* __unm: Unary -, used for negation on numbers&lt;br /&gt;
* __concat: Concatenation (..)&lt;br /&gt;
* __eq: Equality (==)&lt;br /&gt;
* __lt: Less than (&amp;lt;)&lt;br /&gt;
* __le: Less than or equal to (&amp;lt;=)&lt;br /&gt;
&lt;br /&gt;
(There's only '''==''', '''&amp;lt;''', '''&amp;lt;=''' because you can implement full support for the comparison operators with just those; in fact only '''==''' and '''&amp;lt;''' are needed.)&lt;br /&gt;
&lt;br /&gt;
=== __call ===&lt;br /&gt;
&lt;br /&gt;
Next comes the '''__call''' key, which allows you to call tables as functions. A code example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
t = setmetatable({}, {&lt;br /&gt;
  __call = function(t, a, b, c, whatever)&lt;br /&gt;
    return (a + b + c) * whatever&lt;br /&gt;
  end&lt;br /&gt;
})&lt;br /&gt;
&lt;br /&gt;
t(1, 2, 3, 4) -- 24&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The function in call is passed the target table as usual, followed by the parameters that we passed in. '''__call''' is very useful for many things. One common thing it's used for is forwarding a call on a table to a function inside that table.&lt;br /&gt;
&lt;br /&gt;
=== __tostring ===&lt;br /&gt;
&lt;br /&gt;
Last of all is '''__tostring'''. If implemented, it's used by [http://www.lua.org/manual/5.1/manual.html#pdf-tostring tostring] to convert a table into a string, most handy when using a function like [http://www.lua.org/manual/5.1/manual.html#pdf-print print]. Normally, when you try to convert a table to a string, you something in the format of &amp;quot;table: 0x&amp;lt;hex-code-here&amp;gt;&amp;quot;, but you can change that using '''__tostring'''. An example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
t = setmetatable({ 1, 2, 3 }, {&lt;br /&gt;
  __tostring = function(t)&lt;br /&gt;
    sum = 0&lt;br /&gt;
    for _, v in pairs(t) do sum = sum + v end&lt;br /&gt;
    return &amp;quot;Sum: &amp;quot; .. sum&lt;br /&gt;
  end&lt;br /&gt;
})&lt;br /&gt;
&lt;br /&gt;
print(t) -- prints out &amp;quot;Sum: 6&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Building the Vector Class ===&lt;br /&gt;
&lt;br /&gt;
To wrap everything up, we'll write a class encapsulating a 2D vector. It's too large to put here, but you can see the full code at [https://gist.github.com/1055480 gist #1055480]. I've positioned all the stuff to do with metatables first in the file, as that's the most important stuff. (Be warned, this may be a bit confusing if you've never encountered Object-Oriented Programming before.)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
Vector = {}&lt;br /&gt;
Vector.__index = Vector&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This code sets up the table for the '''Vector''' class, and sets the '''__index''' key to point back at itself. Now, what's going on here? You've probably noticed that we've put all the metatable methods inside the '''Vector''' class. What you're seeing is the simplest way to achieve OOP (Object-Oriented Programming) in Lua. The '''Vector''' table represents the class, which contains all the functions that instances can use. '''Vector.new''' (shown below) creates a new instance of this class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
function Vector.new(x, y)&lt;br /&gt;
  return setmetatable({ x = x or 0, y = y or 0 }, Vector)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It creates a new table with '''x''' and '''y''' properties, and then sets the metatable to the '''Vector''' class. As we know, '''Vector''' contains all the metamethods and especially the '''__index''' key. This means that we can use all the functions we define in '''Vector''' through this new table. We'll come back to this in a moment.&lt;br /&gt;
&lt;br /&gt;
Another important thing is the last line:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
setmetatable(Vector, { __call = function(_, ...) return Vector.new(...) end })&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This means that we can create a new '''Vector''' instance by either calling '''Vector.new''' or just '''Vector'''.&lt;br /&gt;
&lt;br /&gt;
The last important thing that you may not be aware of is the colon syntax. When we define a function with a colon, like this:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
function t:method(a, b, c)&lt;br /&gt;
  -- ...&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
What we are really doing is defining this function:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
function t.method(self, a, b, c)&lt;br /&gt;
  -- ...&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is syntactic sugar to help with OOP. We can then use the colon syntax when calling functions:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
-- these are the same&lt;br /&gt;
t:method(1, 2, 3)&lt;br /&gt;
t.method(t, 1, 2, 3)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now, how do we use this '''Vector''' class? Here's a final example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
a = Vector.new(10, 10)&lt;br /&gt;
b = Vector(20, 11)&lt;br /&gt;
c = a + b&lt;br /&gt;
print(a:len()) -- 14.142135623731&lt;br /&gt;
print(a) -- (10, 10)&lt;br /&gt;
print(c) -- (30, 21)&lt;br /&gt;
print(a &amp;lt; c) -- true&lt;br /&gt;
print(a == b) -- false&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Because of the '''__index''' in '''Vector''', we can use all the methods defined in the class through the instances.&lt;br /&gt;
&lt;br /&gt;
== Advanced examples ==&lt;br /&gt;
Overriding self's value:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
local array = {text = &amp;quot;none&amp;quot;}&lt;br /&gt;
local array2 = {text = &amp;quot;none2&amp;quot;}&lt;br /&gt;
function array:setKey (key, value)&lt;br /&gt;
	self[key] = value&lt;br /&gt;
end&lt;br /&gt;
function array:getKey (key)&lt;br /&gt;
	return self[key]&lt;br /&gt;
end&lt;br /&gt;
array.getKey(array2, &amp;quot;text&amp;quot;) -- returns &amp;quot;none2&amp;quot;&lt;br /&gt;
array.setKey(array2, &amp;quot;text&amp;quot;, &amp;quot;something2&amp;quot;) -- sets array2's &amp;quot;text&amp;quot; value to 'something2'&lt;br /&gt;
array.getKey(array2, &amp;quot;text&amp;quot;) -- returns &amp;quot;something&amp;quot;&lt;br /&gt;
array:getKey(&amp;quot;text&amp;quot;) -- returns &amp;quot;none&amp;quot;&lt;br /&gt;
array.getKey(array, &amp;quot;text&amp;quot;) -- same as above&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A simple backpacks example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
local backpack = {list = {}}&lt;br /&gt;
function backpack:create (owner, slots)&lt;br /&gt;
	if self.list[owner] then return end -- return false in case this player already owns a backpack&lt;br /&gt;
		local new = {&lt;br /&gt;
		items = {},&lt;br /&gt;
		slots = slots or 100,&lt;br /&gt;
		owner = owner&lt;br /&gt;
	}&lt;br /&gt;
	setmetatable(new.items, &lt;br /&gt;
	{&lt;br /&gt;
		__newindex = function(table, key, value) -- this is called once a new value/item is added into the player's table/backpack&lt;br /&gt;
			if #table &amp;gt;= new.slots then return end -- return false in case there isn't any free slot left&lt;br /&gt;
			return rawset(table, key, value)&lt;br /&gt;
		end&lt;br /&gt;
	}&lt;br /&gt;
	)&lt;br /&gt;
	self.list[owner] = new&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function backpack:addItem (player, item, value)&lt;br /&gt;
	if not self.list[player] then return end&lt;br /&gt;
	self.list[player][item] = value&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Latest modifications ==&lt;br /&gt;
--[[User:Novo|Novo]] ([[User talk:Novo|talk]]) 03:43, 18 January 2015 (UTC) (Adam James @adam.mta)&lt;/div&gt;</summary>
		<author><name>Novo</name></author>
	</entry>
	<entry>
		<id>https://wiki.multitheftauto.com/index.php?title=OOP_in_Lua&amp;diff=44010</id>
		<title>OOP in Lua</title>
		<link rel="alternate" type="text/html" href="https://wiki.multitheftauto.com/index.php?title=OOP_in_Lua&amp;diff=44010"/>
		<updated>2015-01-18T03:44:38Z</updated>

		<summary type="html">&lt;p&gt;Novo: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;__TOC__&lt;br /&gt;
== Introduction and other related pages ==&lt;br /&gt;
This is a scripting tutorial teaches you the basics about how to start using an Object-Oriented developing interface with Lua. Originally created by [http://forum.mtasa.com/memberlist.php?mode=viewprofile&amp;amp;u=53356 Adam James] @adam.mta.&lt;br /&gt;
* [[OOP_Introduction|OOP Introduction - MTA-related]] ''by qaisjp3''&lt;br /&gt;
* [[OOP|OOP]]&lt;br /&gt;
* [[OOP_client|MTA built-in client-sided OO functions]]&lt;br /&gt;
* [[OOP_server|MTA built-in server-sided OO functions]]&lt;br /&gt;
&lt;br /&gt;
== Glossary ==&lt;br /&gt;
* environment: either a table or an array containing values.&lt;br /&gt;
* self: predefined variable referring to the environment within which we are executing the code.&lt;br /&gt;
&lt;br /&gt;
== Initialising ==&lt;br /&gt;
There is a basic and simple predefined variables we should recognize and know: '''[[#Glossary|self]]'''.&lt;br /&gt;
=== Our first environment ===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
local array = {}&lt;br /&gt;
function array:example (argument)&lt;br /&gt;
	return &amp;quot;Hello&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
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:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
array:example()&lt;br /&gt;
array.example(array, example)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
As Lua is so cool, we're able to call a function using two methods: &amp;quot;''':'''&amp;quot; and &amp;quot;'''.'''&amp;quot;. 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|advanced examples]]).&lt;br /&gt;
&lt;br /&gt;
=== Variables and further handling ===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
local array = {text = &amp;quot;none&amp;quot;}&lt;br /&gt;
function array:setKey (key, value)&lt;br /&gt;
	self[key] = value&lt;br /&gt;
end&lt;br /&gt;
function array:getKey (key)&lt;br /&gt;
	return self[key]&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
array:getKey(&amp;quot;text&amp;quot;) -- returns &amp;quot;none&amp;quot;&lt;br /&gt;
array:setKey(&amp;quot;text&amp;quot;, &amp;quot;something&amp;quot;) -- sets &amp;quot;text&amp;quot;'s value to 'something'&lt;br /&gt;
array:getKey(&amp;quot;text&amp;quot;) -- returns &amp;quot;something&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
== Metatables ==&lt;br /&gt;
Content's provenance: [http://nova-fusion.com/2011/06/30/lua-metatables-tutorial/ NovaFusion]&lt;br /&gt;
----&lt;br /&gt;
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.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
t = {} -- our normal table&lt;br /&gt;
mt = {} -- our metatable, which contains nothing right now&lt;br /&gt;
setmetatable(t, mt) -- sets mt to be t's metatable&lt;br /&gt;
getmetatable(t) -- this will return mt&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
t = setmetatable({}, {})&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
'''setmetatable''' returns its first argument, therefore we can use this shorter form.&lt;br /&gt;
&lt;br /&gt;
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:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
t = setmetatable({}, {&lt;br /&gt;
  __index = function(t, key)&lt;br /&gt;
    if key == &amp;quot;foo&amp;quot; then&lt;br /&gt;
      return 0&lt;br /&gt;
    else&lt;br /&gt;
      return table[key]&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
})&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
=== __index ===&lt;br /&gt;
&lt;br /&gt;
The most used metatable key is most likely '''__index'''; it can contain either a function or table.&lt;br /&gt;
&lt;br /&gt;
When you look up a table with a key, regardless of what the key is (t[4], t.foo, and t[&amp;quot;foo&amp;quot;], 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:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
other = { foo = 3 }&lt;br /&gt;
t = setmetatable({}, { __index = other })&lt;br /&gt;
t.foo -- 3&lt;br /&gt;
t.bar -- nil&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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 &amp;quot;foo&amp;quot; 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 &amp;quot;foo&amp;quot; is used.&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
=== __newindex ===&lt;br /&gt;
&lt;br /&gt;
Next in line is '''__newindex''', which is similar to '''__index'''. Like '''__index''', it can contain either a function or table.&lt;br /&gt;
&lt;br /&gt;
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:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
other = {}&lt;br /&gt;
t = setmetatable({}, { __newindex = other })&lt;br /&gt;
t.foo = 3&lt;br /&gt;
other.foo -- 3&lt;br /&gt;
t.foo -- nil&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
As would be expected, if '''__newindex''' is a function, it will be called with the table, key, and value passed as parameters:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
t = setmetatable({}, {&lt;br /&gt;
  __newindex = function(t, key, value)&lt;br /&gt;
    if type(value) == &amp;quot;number&amp;quot; then&lt;br /&gt;
      rawset(t, key, value * value)&lt;br /&gt;
    else&lt;br /&gt;
      rawset(t, key, value)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
})&lt;br /&gt;
&lt;br /&gt;
t.foo = &amp;quot;foo&amp;quot;&lt;br /&gt;
t.bar = 4&lt;br /&gt;
t.la = 10&lt;br /&gt;
t.foo -- &amp;quot;foo&amp;quot;&lt;br /&gt;
t.bar -- 16&lt;br /&gt;
t.la -- 100&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
When creating a new key in '''t''', if the value is a number it will be squared, otherwise it will just be set anyway. This introduces us to our friends, '''rawget''' and '''rawset'''.&lt;br /&gt;
&lt;br /&gt;
=== rawget and rawset ===&lt;br /&gt;
&lt;br /&gt;
There are times when you need get and set a table's keys without having Lua do it's thing with metatables. As you might guess, [http://www.lua.org/manual/5.1/manual.html#pdf-rawget rawget] allows you to get the value of a key without Lua using '''__index''', and [http://www.lua.org/manual/5.1/manual.html#pdf-rawset rawset] allows you to set the value of a key without Lua using '''__newindex''' (no these don't provide a speed increase to conventional way of doing things). You'll need to use these when you would otherwise get stuck in an infinite loop. For example, in that last code example, the code '''t[key] = value * value''' would set off the same '''__newindex''' function again, which would get you stuck in an infinite loop. Using '''rawset(t, key, value * value)''' avoids this.&lt;br /&gt;
&lt;br /&gt;
As you probably can see, to use these functions, for parameters we must pass in the target table, the key, and if you're using '''rawset''', the value.&lt;br /&gt;
&lt;br /&gt;
=== Operators ===&lt;br /&gt;
&lt;br /&gt;
Many of the metatable keys available have to do with operators (as in, '''+''', '''-''', etc.), allowing you to make tables support the use of operators on them. For example, say we wanted a table to support the multiplication operator ('''*'''), this is how we would do it:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
t = setmetatable({ 1, 2, 3 }, {&lt;br /&gt;
  __mul = function(t, other)&lt;br /&gt;
    new = {}&lt;br /&gt;
    &lt;br /&gt;
    for i = 1, other do&lt;br /&gt;
      for _, v in ipairs(t) do table.insert(new, v) end&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    return new&lt;br /&gt;
  end&lt;br /&gt;
})&lt;br /&gt;
&lt;br /&gt;
t = t * 2 -- { 1, 2, 3, 1, 2, 3 }&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This allows us to create a new table with the original replicated a certain amount of times using the multiplication operator. As you can tell, the corresponding key for multiplication is '''__mul'''; unlike '''__index''' and '''__newindex''' the keys for operators can only contain functions. The first parameter these functions always receive is the target table, and then the value on the right hand side (except for the unary - which has the key of '''__unm'''). Here's a list of the supported operators:&lt;br /&gt;
&lt;br /&gt;
* __add: Addition (+)&lt;br /&gt;
* __sub: Subtraction (-)&lt;br /&gt;
* __mul: Multiplication (*)&lt;br /&gt;
* __div: Division (/)&lt;br /&gt;
* __mod: Modulos (%)&lt;br /&gt;
* __unm: Unary -, used for negation on numbers&lt;br /&gt;
* __concat: Concatenation (..)&lt;br /&gt;
* __eq: Equality (==)&lt;br /&gt;
* __lt: Less than (&amp;lt;)&lt;br /&gt;
* __le: Less than or equal to (&amp;lt;=)&lt;br /&gt;
&lt;br /&gt;
(There's only '''==''', '''&amp;lt;''', '''&amp;lt;=''' because you can implement full support for the comparison operators with just those; in fact only '''==''' and '''&amp;lt;''' are needed.)&lt;br /&gt;
&lt;br /&gt;
=== __call ===&lt;br /&gt;
&lt;br /&gt;
Next comes the '''__call''' key, which allows you to call tables as functions. A code example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
t = setmetatable({}, {&lt;br /&gt;
  __call = function(t, a, b, c, whatever)&lt;br /&gt;
    return (a + b + c) * whatever&lt;br /&gt;
  end&lt;br /&gt;
})&lt;br /&gt;
&lt;br /&gt;
t(1, 2, 3, 4) -- 24&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The function in call is passed the target table as usual, followed by the parameters that we passed in. '''__call''' is very useful for many things. One common thing it's used for is forwarding a call on a table to a function inside that table.&lt;br /&gt;
&lt;br /&gt;
=== __tostring ===&lt;br /&gt;
&lt;br /&gt;
Last of all is '''__tostring'''. If implemented, it's used by [http://www.lua.org/manual/5.1/manual.html#pdf-tostring tostring] to convert a table into a string, most handy when using a function like [http://www.lua.org/manual/5.1/manual.html#pdf-print print]. Normally, when you try to convert a table to a string, you something in the format of &amp;quot;table: 0x&amp;lt;hex-code-here&amp;gt;&amp;quot;, but you can change that using '''__tostring'''. An example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
t = setmetatable({ 1, 2, 3 }, {&lt;br /&gt;
  __tostring = function(t)&lt;br /&gt;
    sum = 0&lt;br /&gt;
    for _, v in pairs(t) do sum = sum + v end&lt;br /&gt;
    return &amp;quot;Sum: &amp;quot; .. sum&lt;br /&gt;
  end&lt;br /&gt;
})&lt;br /&gt;
&lt;br /&gt;
print(t) -- prints out &amp;quot;Sum: 6&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Building the Vector Class ===&lt;br /&gt;
&lt;br /&gt;
To wrap everything up, we'll write a class encapsulating a 2D vector. It's too large to put here, but you can see the full code at [https://gist.github.com/1055480 gist #1055480]. I've positioned all the stuff to do with metatables first in the file, as that's the most important stuff. (Be warned, this may be a bit confusing if you've never encountered Object-Oriented Programming before.)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
Vector = {}&lt;br /&gt;
Vector.__index = Vector&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This code sets up the table for the '''Vector''' class, and sets the '''__index''' key to point back at itself. Now, what's going on here? You've probably noticed that we've put all the metatable methods inside the '''Vector''' class. What you're seeing is the simplest way to achieve OOP (Object-Oriented Programming) in Lua. The '''Vector''' table represents the class, which contains all the functions that instances can use. '''Vector.new''' (shown below) creates a new instance of this class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
function Vector.new(x, y)&lt;br /&gt;
  return setmetatable({ x = x or 0, y = y or 0 }, Vector)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It creates a new table with '''x''' and '''y''' properties, and then sets the metatable to the '''Vector''' class. As we know, '''Vector''' contains all the metamethods and especially the '''__index''' key. This means that we can use all the functions we define in '''Vector''' through this new table. We'll come back to this in a moment.&lt;br /&gt;
&lt;br /&gt;
Another important thing is the last line:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
setmetatable(Vector, { __call = function(_, ...) return Vector.new(...) end })&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This means that we can create a new '''Vector''' instance by either calling '''Vector.new''' or just '''Vector'''.&lt;br /&gt;
&lt;br /&gt;
The last important thing that you may not be aware of is the colon syntax. When we define a function with a colon, like this:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
function t:method(a, b, c)&lt;br /&gt;
  -- ...&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
What we are really doing is defining this function:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
function t.method(self, a, b, c)&lt;br /&gt;
  -- ...&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is syntactic sugar to help with OOP. We can then use the colon syntax when calling functions:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
-- these are the same&lt;br /&gt;
t:method(1, 2, 3)&lt;br /&gt;
t.method(t, 1, 2, 3)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now, how do we use this '''Vector''' class? Here's a final example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
a = Vector.new(10, 10)&lt;br /&gt;
b = Vector(20, 11)&lt;br /&gt;
c = a + b&lt;br /&gt;
print(a:len()) -- 14.142135623731&lt;br /&gt;
print(a) -- (10, 10)&lt;br /&gt;
print(c) -- (30, 21)&lt;br /&gt;
print(a &amp;lt; c) -- true&lt;br /&gt;
print(a == b) -- false&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Because of the '''__index''' in '''Vector''', we can use all the methods defined in the class through the instances.&lt;br /&gt;
&lt;br /&gt;
== Advanced examples ==&lt;br /&gt;
Overriding self's value:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
local array = {text = &amp;quot;none&amp;quot;}&lt;br /&gt;
local array2 = {text = &amp;quot;none2&amp;quot;}&lt;br /&gt;
function array:setKey (key, value)&lt;br /&gt;
	self[key] = value&lt;br /&gt;
end&lt;br /&gt;
function array:getKey (key)&lt;br /&gt;
	return self[key]&lt;br /&gt;
end&lt;br /&gt;
array.getKey(array2, &amp;quot;text&amp;quot;) -- returns &amp;quot;none2&amp;quot;&lt;br /&gt;
array.setKey(array2, &amp;quot;text&amp;quot;, &amp;quot;something2&amp;quot;) -- sets array2's &amp;quot;text&amp;quot; value to 'something2'&lt;br /&gt;
array.getKey(array2, &amp;quot;text&amp;quot;) -- returns &amp;quot;something&amp;quot;&lt;br /&gt;
array:getKey(&amp;quot;text&amp;quot;) -- returns &amp;quot;none&amp;quot;&lt;br /&gt;
array.getKey(array, &amp;quot;text&amp;quot;) -- same as above&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A simple backpacks example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
local backpack = {list = {}}&lt;br /&gt;
function backpack:create (owner, slots)&lt;br /&gt;
	if self.list[owner] then return end -- return false in case this player already owns a backpack&lt;br /&gt;
		local new = {&lt;br /&gt;
		items = {},&lt;br /&gt;
		slots = slots or 100,&lt;br /&gt;
		owner = owner&lt;br /&gt;
	}&lt;br /&gt;
	setmetatable(new.items, &lt;br /&gt;
	{&lt;br /&gt;
		__newindex = function(table, key, value) -- this is called once a new value/item is added into the player's table/backpack&lt;br /&gt;
			if #table &amp;gt;= new.slots then return end -- return false in case there isn't any free slot left&lt;br /&gt;
			return rawset(table, key, value)&lt;br /&gt;
		end&lt;br /&gt;
	}&lt;br /&gt;
	)&lt;br /&gt;
	self.list[owner] = new&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function backpack:addItem (player, item, value)&lt;br /&gt;
	if not self.list[player] then return end&lt;br /&gt;
	self.list[player][item] = value&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Latest modifications ==&lt;br /&gt;
--[[User:Novo|Novo]] ([[User talk:Novo|talk]]) 03:43, 18 January 2015 (UTC) (Adam James @adam.mta)&lt;/div&gt;</summary>
		<author><name>Novo</name></author>
	</entry>
	<entry>
		<id>https://wiki.multitheftauto.com/index.php?title=OOP_in_Lua&amp;diff=44009</id>
		<title>OOP in Lua</title>
		<link rel="alternate" type="text/html" href="https://wiki.multitheftauto.com/index.php?title=OOP_in_Lua&amp;diff=44009"/>
		<updated>2015-01-18T03:43:52Z</updated>

		<summary type="html">&lt;p&gt;Novo: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;__TOC__&lt;br /&gt;
== Introduction and other related pages ==&lt;br /&gt;
This is a scripting tutorial teaches you the basics about how to start using an Object-Oriented developing interface with Lua.&lt;br /&gt;
* [[OOP_Introduction|OOP Introduction - MTA-related]] ''by qaisjp3''&lt;br /&gt;
* [[OOP|OOP]]&lt;br /&gt;
* [[OOP_client|MTA built-in client-sided OO functions]]&lt;br /&gt;
* [[OOP_server|MTA built-in server-sided OO functions]]&lt;br /&gt;
&lt;br /&gt;
== Glossary ==&lt;br /&gt;
* environment: either a table or an array containing values.&lt;br /&gt;
* self: predefined variable referring to the environment within which we are executing the code.&lt;br /&gt;
&lt;br /&gt;
== Initialising ==&lt;br /&gt;
There is a basic and simple predefined variables we should recognize and know: '''[[#Glossary|self]]'''.&lt;br /&gt;
=== Our first environment ===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
local array = {}&lt;br /&gt;
function array:example (argument)&lt;br /&gt;
	return &amp;quot;Hello&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
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:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
array:example()&lt;br /&gt;
array.example(array, example)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
As Lua is so cool, we're able to call a function using two methods: &amp;quot;''':'''&amp;quot; and &amp;quot;'''.'''&amp;quot;. 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|advanced examples]]).&lt;br /&gt;
&lt;br /&gt;
=== Variables and further handling ===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
local array = {text = &amp;quot;none&amp;quot;}&lt;br /&gt;
function array:setKey (key, value)&lt;br /&gt;
	self[key] = value&lt;br /&gt;
end&lt;br /&gt;
function array:getKey (key)&lt;br /&gt;
	return self[key]&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
array:getKey(&amp;quot;text&amp;quot;) -- returns &amp;quot;none&amp;quot;&lt;br /&gt;
array:setKey(&amp;quot;text&amp;quot;, &amp;quot;something&amp;quot;) -- sets &amp;quot;text&amp;quot;'s value to 'something'&lt;br /&gt;
array:getKey(&amp;quot;text&amp;quot;) -- returns &amp;quot;something&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
== Metatables ==&lt;br /&gt;
Content's provenance: [http://nova-fusion.com/2011/06/30/lua-metatables-tutorial/ NovaFusion]&lt;br /&gt;
----&lt;br /&gt;
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.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
t = {} -- our normal table&lt;br /&gt;
mt = {} -- our metatable, which contains nothing right now&lt;br /&gt;
setmetatable(t, mt) -- sets mt to be t's metatable&lt;br /&gt;
getmetatable(t) -- this will return mt&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
t = setmetatable({}, {})&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
'''setmetatable''' returns its first argument, therefore we can use this shorter form.&lt;br /&gt;
&lt;br /&gt;
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:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
t = setmetatable({}, {&lt;br /&gt;
  __index = function(t, key)&lt;br /&gt;
    if key == &amp;quot;foo&amp;quot; then&lt;br /&gt;
      return 0&lt;br /&gt;
    else&lt;br /&gt;
      return table[key]&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
})&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
=== __index ===&lt;br /&gt;
&lt;br /&gt;
The most used metatable key is most likely '''__index'''; it can contain either a function or table.&lt;br /&gt;
&lt;br /&gt;
When you look up a table with a key, regardless of what the key is (t[4], t.foo, and t[&amp;quot;foo&amp;quot;], 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:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
other = { foo = 3 }&lt;br /&gt;
t = setmetatable({}, { __index = other })&lt;br /&gt;
t.foo -- 3&lt;br /&gt;
t.bar -- nil&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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 &amp;quot;foo&amp;quot; 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 &amp;quot;foo&amp;quot; is used.&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
=== __newindex ===&lt;br /&gt;
&lt;br /&gt;
Next in line is '''__newindex''', which is similar to '''__index'''. Like '''__index''', it can contain either a function or table.&lt;br /&gt;
&lt;br /&gt;
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:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
other = {}&lt;br /&gt;
t = setmetatable({}, { __newindex = other })&lt;br /&gt;
t.foo = 3&lt;br /&gt;
other.foo -- 3&lt;br /&gt;
t.foo -- nil&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
As would be expected, if '''__newindex''' is a function, it will be called with the table, key, and value passed as parameters:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
t = setmetatable({}, {&lt;br /&gt;
  __newindex = function(t, key, value)&lt;br /&gt;
    if type(value) == &amp;quot;number&amp;quot; then&lt;br /&gt;
      rawset(t, key, value * value)&lt;br /&gt;
    else&lt;br /&gt;
      rawset(t, key, value)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
})&lt;br /&gt;
&lt;br /&gt;
t.foo = &amp;quot;foo&amp;quot;&lt;br /&gt;
t.bar = 4&lt;br /&gt;
t.la = 10&lt;br /&gt;
t.foo -- &amp;quot;foo&amp;quot;&lt;br /&gt;
t.bar -- 16&lt;br /&gt;
t.la -- 100&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
When creating a new key in '''t''', if the value is a number it will be squared, otherwise it will just be set anyway. This introduces us to our friends, '''rawget''' and '''rawset'''.&lt;br /&gt;
&lt;br /&gt;
=== rawget and rawset ===&lt;br /&gt;
&lt;br /&gt;
There are times when you need get and set a table's keys without having Lua do it's thing with metatables. As you might guess, [http://www.lua.org/manual/5.1/manual.html#pdf-rawget rawget] allows you to get the value of a key without Lua using '''__index''', and [http://www.lua.org/manual/5.1/manual.html#pdf-rawset rawset] allows you to set the value of a key without Lua using '''__newindex''' (no these don't provide a speed increase to conventional way of doing things). You'll need to use these when you would otherwise get stuck in an infinite loop. For example, in that last code example, the code '''t[key] = value * value''' would set off the same '''__newindex''' function again, which would get you stuck in an infinite loop. Using '''rawset(t, key, value * value)''' avoids this.&lt;br /&gt;
&lt;br /&gt;
As you probably can see, to use these functions, for parameters we must pass in the target table, the key, and if you're using '''rawset''', the value.&lt;br /&gt;
&lt;br /&gt;
=== Operators ===&lt;br /&gt;
&lt;br /&gt;
Many of the metatable keys available have to do with operators (as in, '''+''', '''-''', etc.), allowing you to make tables support the use of operators on them. For example, say we wanted a table to support the multiplication operator ('''*'''), this is how we would do it:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
t = setmetatable({ 1, 2, 3 }, {&lt;br /&gt;
  __mul = function(t, other)&lt;br /&gt;
    new = {}&lt;br /&gt;
    &lt;br /&gt;
    for i = 1, other do&lt;br /&gt;
      for _, v in ipairs(t) do table.insert(new, v) end&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    return new&lt;br /&gt;
  end&lt;br /&gt;
})&lt;br /&gt;
&lt;br /&gt;
t = t * 2 -- { 1, 2, 3, 1, 2, 3 }&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This allows us to create a new table with the original replicated a certain amount of times using the multiplication operator. As you can tell, the corresponding key for multiplication is '''__mul'''; unlike '''__index''' and '''__newindex''' the keys for operators can only contain functions. The first parameter these functions always receive is the target table, and then the value on the right hand side (except for the unary - which has the key of '''__unm'''). Here's a list of the supported operators:&lt;br /&gt;
&lt;br /&gt;
* __add: Addition (+)&lt;br /&gt;
* __sub: Subtraction (-)&lt;br /&gt;
* __mul: Multiplication (*)&lt;br /&gt;
* __div: Division (/)&lt;br /&gt;
* __mod: Modulos (%)&lt;br /&gt;
* __unm: Unary -, used for negation on numbers&lt;br /&gt;
* __concat: Concatenation (..)&lt;br /&gt;
* __eq: Equality (==)&lt;br /&gt;
* __lt: Less than (&amp;lt;)&lt;br /&gt;
* __le: Less than or equal to (&amp;lt;=)&lt;br /&gt;
&lt;br /&gt;
(There's only '''==''', '''&amp;lt;''', '''&amp;lt;=''' because you can implement full support for the comparison operators with just those; in fact only '''==''' and '''&amp;lt;''' are needed.)&lt;br /&gt;
&lt;br /&gt;
=== __call ===&lt;br /&gt;
&lt;br /&gt;
Next comes the '''__call''' key, which allows you to call tables as functions. A code example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
t = setmetatable({}, {&lt;br /&gt;
  __call = function(t, a, b, c, whatever)&lt;br /&gt;
    return (a + b + c) * whatever&lt;br /&gt;
  end&lt;br /&gt;
})&lt;br /&gt;
&lt;br /&gt;
t(1, 2, 3, 4) -- 24&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The function in call is passed the target table as usual, followed by the parameters that we passed in. '''__call''' is very useful for many things. One common thing it's used for is forwarding a call on a table to a function inside that table.&lt;br /&gt;
&lt;br /&gt;
=== __tostring ===&lt;br /&gt;
&lt;br /&gt;
Last of all is '''__tostring'''. If implemented, it's used by [http://www.lua.org/manual/5.1/manual.html#pdf-tostring tostring] to convert a table into a string, most handy when using a function like [http://www.lua.org/manual/5.1/manual.html#pdf-print print]. Normally, when you try to convert a table to a string, you something in the format of &amp;quot;table: 0x&amp;lt;hex-code-here&amp;gt;&amp;quot;, but you can change that using '''__tostring'''. An example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
t = setmetatable({ 1, 2, 3 }, {&lt;br /&gt;
  __tostring = function(t)&lt;br /&gt;
    sum = 0&lt;br /&gt;
    for _, v in pairs(t) do sum = sum + v end&lt;br /&gt;
    return &amp;quot;Sum: &amp;quot; .. sum&lt;br /&gt;
  end&lt;br /&gt;
})&lt;br /&gt;
&lt;br /&gt;
print(t) -- prints out &amp;quot;Sum: 6&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Building the Vector Class ===&lt;br /&gt;
&lt;br /&gt;
To wrap everything up, we'll write a class encapsulating a 2D vector. It's too large to put here, but you can see the full code at [https://gist.github.com/1055480 gist #1055480]. I've positioned all the stuff to do with metatables first in the file, as that's the most important stuff. (Be warned, this may be a bit confusing if you've never encountered Object-Oriented Programming before.)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
Vector = {}&lt;br /&gt;
Vector.__index = Vector&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This code sets up the table for the '''Vector''' class, and sets the '''__index''' key to point back at itself. Now, what's going on here? You've probably noticed that we've put all the metatable methods inside the '''Vector''' class. What you're seeing is the simplest way to achieve OOP (Object-Oriented Programming) in Lua. The '''Vector''' table represents the class, which contains all the functions that instances can use. '''Vector.new''' (shown below) creates a new instance of this class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
function Vector.new(x, y)&lt;br /&gt;
  return setmetatable({ x = x or 0, y = y or 0 }, Vector)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It creates a new table with '''x''' and '''y''' properties, and then sets the metatable to the '''Vector''' class. As we know, '''Vector''' contains all the metamethods and especially the '''__index''' key. This means that we can use all the functions we define in '''Vector''' through this new table. We'll come back to this in a moment.&lt;br /&gt;
&lt;br /&gt;
Another important thing is the last line:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
setmetatable(Vector, { __call = function(_, ...) return Vector.new(...) end })&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This means that we can create a new '''Vector''' instance by either calling '''Vector.new''' or just '''Vector'''.&lt;br /&gt;
&lt;br /&gt;
The last important thing that you may not be aware of is the colon syntax. When we define a function with a colon, like this:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
function t:method(a, b, c)&lt;br /&gt;
  -- ...&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
What we are really doing is defining this function:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
function t.method(self, a, b, c)&lt;br /&gt;
  -- ...&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is syntactic sugar to help with OOP. We can then use the colon syntax when calling functions:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
-- these are the same&lt;br /&gt;
t:method(1, 2, 3)&lt;br /&gt;
t.method(t, 1, 2, 3)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now, how do we use this '''Vector''' class? Here's a final example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
a = Vector.new(10, 10)&lt;br /&gt;
b = Vector(20, 11)&lt;br /&gt;
c = a + b&lt;br /&gt;
print(a:len()) -- 14.142135623731&lt;br /&gt;
print(a) -- (10, 10)&lt;br /&gt;
print(c) -- (30, 21)&lt;br /&gt;
print(a &amp;lt; c) -- true&lt;br /&gt;
print(a == b) -- false&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Because of the '''__index''' in '''Vector''', we can use all the methods defined in the class through the instances.&lt;br /&gt;
&lt;br /&gt;
== Advanced examples ==&lt;br /&gt;
Overriding self's value:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
local array = {text = &amp;quot;none&amp;quot;}&lt;br /&gt;
local array2 = {text = &amp;quot;none2&amp;quot;}&lt;br /&gt;
function array:setKey (key, value)&lt;br /&gt;
	self[key] = value&lt;br /&gt;
end&lt;br /&gt;
function array:getKey (key)&lt;br /&gt;
	return self[key]&lt;br /&gt;
end&lt;br /&gt;
array.getKey(array2, &amp;quot;text&amp;quot;) -- returns &amp;quot;none2&amp;quot;&lt;br /&gt;
array.setKey(array2, &amp;quot;text&amp;quot;, &amp;quot;something2&amp;quot;) -- sets array2's &amp;quot;text&amp;quot; value to 'something2'&lt;br /&gt;
array.getKey(array2, &amp;quot;text&amp;quot;) -- returns &amp;quot;something&amp;quot;&lt;br /&gt;
array:getKey(&amp;quot;text&amp;quot;) -- returns &amp;quot;none&amp;quot;&lt;br /&gt;
array.getKey(array, &amp;quot;text&amp;quot;) -- same as above&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A simple backpacks example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
local backpack = {list = {}}&lt;br /&gt;
function backpack:create (owner, slots)&lt;br /&gt;
	if self.list[owner] then return end -- return false in case this player already owns a backpack&lt;br /&gt;
		local new = {&lt;br /&gt;
		items = {},&lt;br /&gt;
		slots = slots or 100,&lt;br /&gt;
		owner = owner&lt;br /&gt;
	}&lt;br /&gt;
	setmetatable(new.items, &lt;br /&gt;
	{&lt;br /&gt;
		__newindex = function(table, key, value) -- this is called once a new value/item is added into the player's table/backpack&lt;br /&gt;
			if #table &amp;gt;= new.slots then return end -- return false in case there isn't any free slot left&lt;br /&gt;
			return rawset(table, key, value)&lt;br /&gt;
		end&lt;br /&gt;
	}&lt;br /&gt;
	)&lt;br /&gt;
	self.list[owner] = new&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function backpack:addItem (player, item, value)&lt;br /&gt;
	if not self.list[player] then return end&lt;br /&gt;
	self.list[player][item] = value&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Latest modifications ==&lt;br /&gt;
--[[User:Novo|Novo]] ([[User talk:Novo|talk]]) 03:43, 18 January 2015 (UTC) (Adam James @adam.mta)&lt;/div&gt;</summary>
		<author><name>Novo</name></author>
	</entry>
	<entry>
		<id>https://wiki.multitheftauto.com/index.php?title=OOP_in_Lua&amp;diff=44008</id>
		<title>OOP in Lua</title>
		<link rel="alternate" type="text/html" href="https://wiki.multitheftauto.com/index.php?title=OOP_in_Lua&amp;diff=44008"/>
		<updated>2015-01-18T03:43:04Z</updated>

		<summary type="html">&lt;p&gt;Novo: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;__TOC__&lt;br /&gt;
== Introduction and other related pages ==&lt;br /&gt;
This is a scripting tutorial teaches you the basics about how to start using an Object-Oriented developing interface with Lua.&lt;br /&gt;
* [[OOP_Introduction|OOP Introduction - MTA-related]] ''by qaisjp3''&lt;br /&gt;
* [[OOP|OOP]]&lt;br /&gt;
* [[OOP_client|MTA built-in client-sided OO functions]]&lt;br /&gt;
* [[OOP_server|MTA built-in server-sided OO functions]]&lt;br /&gt;
&lt;br /&gt;
== Glossary ==&lt;br /&gt;
* environment: either a table or an array containing values.&lt;br /&gt;
* self: predefined variable referring to the environment within which we are executing the code.&lt;br /&gt;
&lt;br /&gt;
== Initialising ==&lt;br /&gt;
There is a basic and simple predefined variables we should recognize and know: '''[[#Glossary|self]]'''.&lt;br /&gt;
=== Our first environment ===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
local array = {}&lt;br /&gt;
function array:example (argument)&lt;br /&gt;
	return &amp;quot;Hello&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
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:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
array:example()&lt;br /&gt;
array.example(array, example)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
As Lua is so cool, we're able to call a function using two methods: &amp;quot;''':'''&amp;quot; and &amp;quot;'''.'''&amp;quot;. 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|advanced examples]]).&lt;br /&gt;
&lt;br /&gt;
=== Variables and further handling ===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
local array = {text = &amp;quot;none&amp;quot;}&lt;br /&gt;
function array:setKey (key, value)&lt;br /&gt;
	self[key] = value&lt;br /&gt;
end&lt;br /&gt;
function array:getKey (key)&lt;br /&gt;
	return self[key]&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
array:getKey(&amp;quot;text&amp;quot;) -- returns &amp;quot;none&amp;quot;&lt;br /&gt;
array:setKey(&amp;quot;text&amp;quot;, &amp;quot;something&amp;quot;) -- sets &amp;quot;text&amp;quot;'s value to 'something'&lt;br /&gt;
array:getKey(&amp;quot;text&amp;quot;) -- returns &amp;quot;something&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
== Metatables ==&lt;br /&gt;
Content's provenance: [http://nova-fusion.com/2011/06/30/lua-metatables-tutorial/ NovaFusion]&lt;br /&gt;
----&lt;br /&gt;
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.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
t = {} -- our normal table&lt;br /&gt;
mt = {} -- our metatable, which contains nothing right now&lt;br /&gt;
setmetatable(t, mt) -- sets mt to be t's metatable&lt;br /&gt;
getmetatable(t) -- this will return mt&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
t = setmetatable({}, {})&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
'''setmetatable''' returns its first argument, therefore we can use this shorter form.&lt;br /&gt;
&lt;br /&gt;
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:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
t = setmetatable({}, {&lt;br /&gt;
  __index = function(t, key)&lt;br /&gt;
    if key == &amp;quot;foo&amp;quot; then&lt;br /&gt;
      return 0&lt;br /&gt;
    else&lt;br /&gt;
      return table[key]&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
})&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
=== __index ===&lt;br /&gt;
&lt;br /&gt;
The most used metatable key is most likely '''__index'''; it can contain either a function or table.&lt;br /&gt;
&lt;br /&gt;
When you look up a table with a key, regardless of what the key is (t[4], t.foo, and t[&amp;quot;foo&amp;quot;], 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:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
other = { foo = 3 }&lt;br /&gt;
t = setmetatable({}, { __index = other })&lt;br /&gt;
t.foo -- 3&lt;br /&gt;
t.bar -- nil&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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 &amp;quot;foo&amp;quot; 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 &amp;quot;foo&amp;quot; is used.&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
=== __newindex ===&lt;br /&gt;
&lt;br /&gt;
Next in line is '''__newindex''', which is similar to '''__index'''. Like '''__index''', it can contain either a function or table.&lt;br /&gt;
&lt;br /&gt;
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:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
other = {}&lt;br /&gt;
t = setmetatable({}, { __newindex = other })&lt;br /&gt;
t.foo = 3&lt;br /&gt;
other.foo -- 3&lt;br /&gt;
t.foo -- nil&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
As would be expected, if '''__newindex''' is a function, it will be called with the table, key, and value passed as parameters:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
t = setmetatable({}, {&lt;br /&gt;
  __newindex = function(t, key, value)&lt;br /&gt;
    if type(value) == &amp;quot;number&amp;quot; then&lt;br /&gt;
      rawset(t, key, value * value)&lt;br /&gt;
    else&lt;br /&gt;
      rawset(t, key, value)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
})&lt;br /&gt;
&lt;br /&gt;
t.foo = &amp;quot;foo&amp;quot;&lt;br /&gt;
t.bar = 4&lt;br /&gt;
t.la = 10&lt;br /&gt;
t.foo -- &amp;quot;foo&amp;quot;&lt;br /&gt;
t.bar -- 16&lt;br /&gt;
t.la -- 100&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
When creating a new key in '''t''', if the value is a number it will be squared, otherwise it will just be set anyway. This introduces us to our friends, '''rawget''' and '''rawset'''.&lt;br /&gt;
&lt;br /&gt;
=== rawget and rawset ===&lt;br /&gt;
&lt;br /&gt;
There are times when you need get and set a table's keys without having Lua do it's thing with metatables. As you might guess, [http://www.lua.org/manual/5.1/manual.html#pdf-rawget rawget] allows you to get the value of a key without Lua using '''__index''', and [http://www.lua.org/manual/5.1/manual.html#pdf-rawset rawset] allows you to set the value of a key without Lua using '''__newindex''' (no these don't provide a speed increase to conventional way of doing things). You'll need to use these when you would otherwise get stuck in an infinite loop. For example, in that last code example, the code '''t[key] = value * value''' would set off the same '''__newindex''' function again, which would get you stuck in an infinite loop. Using '''rawset(t, key, value * value)''' avoids this.&lt;br /&gt;
&lt;br /&gt;
As you probably can see, to use these functions, for parameters we must pass in the target table, the key, and if you're using '''rawset''', the value.&lt;br /&gt;
&lt;br /&gt;
=== Operators ===&lt;br /&gt;
&lt;br /&gt;
Many of the metatable keys available have to do with operators (as in, '''+''', '''-''', etc.), allowing you to make tables support the use of operators on them. For example, say we wanted a table to support the multiplication operator ('''*'''), this is how we would do it:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
t = setmetatable({ 1, 2, 3 }, {&lt;br /&gt;
  __mul = function(t, other)&lt;br /&gt;
    new = {}&lt;br /&gt;
    &lt;br /&gt;
    for i = 1, other do&lt;br /&gt;
      for _, v in ipairs(t) do table.insert(new, v) end&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    return new&lt;br /&gt;
  end&lt;br /&gt;
})&lt;br /&gt;
&lt;br /&gt;
t = t * 2 -- { 1, 2, 3, 1, 2, 3 }&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This allows us to create a new table with the original replicated a certain amount of times using the multiplication operator. As you can tell, the corresponding key for multiplication is '''__mul'''; unlike '''__index''' and '''__newindex''' the keys for operators can only contain functions. The first parameter these functions always receive is the target table, and then the value on the right hand side (except for the unary - which has the key of '''__unm'''). Here's a list of the supported operators:&lt;br /&gt;
&lt;br /&gt;
* __add: Addition (+)&lt;br /&gt;
* __sub: Subtraction (-)&lt;br /&gt;
* __mul: Multiplication (*)&lt;br /&gt;
* __div: Division (/)&lt;br /&gt;
* __mod: Modulos (%)&lt;br /&gt;
* __unm: Unary -, used for negation on numbers&lt;br /&gt;
* __concat: Concatenation (..)&lt;br /&gt;
* __eq: Equality (==)&lt;br /&gt;
* __lt: Less than (&amp;lt;)&lt;br /&gt;
* __le: Less than or equal to (&amp;lt;=)&lt;br /&gt;
&lt;br /&gt;
(There's only '''==''', '''&amp;lt;''', '''&amp;lt;=''' because you can implement full support for the comparison operators with just those; in fact only '''==''' and '''&amp;lt;''' are needed.)&lt;br /&gt;
&lt;br /&gt;
=== __call ===&lt;br /&gt;
&lt;br /&gt;
Next comes the '''__call''' key, which allows you to call tables as functions. A code example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
t = setmetatable({}, {&lt;br /&gt;
  __call = function(t, a, b, c, whatever)&lt;br /&gt;
    return (a + b + c) * whatever&lt;br /&gt;
  end&lt;br /&gt;
})&lt;br /&gt;
&lt;br /&gt;
t(1, 2, 3, 4) -- 24&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The function in call is passed the target table as usual, followed by the parameters that we passed in. '''__call''' is very useful for many things. One common thing it's used for is forwarding a call on a table to a function inside that table.&lt;br /&gt;
&lt;br /&gt;
=== __tostring ===&lt;br /&gt;
&lt;br /&gt;
Last of all is '''__tostring'''. If implemented, it's used by [http://www.lua.org/manual/5.1/manual.html#pdf-tostring tostring] to convert a table into a string, most handy when using a function like [http://www.lua.org/manual/5.1/manual.html#pdf-print print]. Normally, when you try to convert a table to a string, you something in the format of &amp;quot;table: 0x&amp;lt;hex-code-here&amp;gt;&amp;quot;, but you can change that using '''__tostring'''. An example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
t = setmetatable({ 1, 2, 3 }, {&lt;br /&gt;
  __tostring = function(t)&lt;br /&gt;
    sum = 0&lt;br /&gt;
    for _, v in pairs(t) do sum = sum + v end&lt;br /&gt;
    return &amp;quot;Sum: &amp;quot; .. sum&lt;br /&gt;
  end&lt;br /&gt;
})&lt;br /&gt;
&lt;br /&gt;
print(t) -- prints out &amp;quot;Sum: 6&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Building the Vector Class ===&lt;br /&gt;
&lt;br /&gt;
To wrap everything up, we'll write a class encapsulating a 2D vector. It's too large to put here, but you can see the full code at [https://gist.github.com/1055480 gist #1055480]. I've positioned all the stuff to do with metatables first in the file, as that's the most important stuff. (Be warned, this may be a bit confusing if you've never encountered Object-Oriented Programming before.)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
Vector = {}&lt;br /&gt;
Vector.__index = Vector&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This code sets up the table for the '''Vector''' class, and sets the '''__index''' key to point back at itself. Now, what's going on here? You've probably noticed that we've put all the metatable methods inside the '''Vector''' class. What you're seeing is the simplest way to achieve OOP (Object-Oriented Programming) in Lua. The '''Vector''' table represents the class, which contains all the functions that instances can use. '''Vector.new''' (shown below) creates a new instance of this class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
function Vector.new(x, y)&lt;br /&gt;
  return setmetatable({ x = x or 0, y = y or 0 }, Vector)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It creates a new table with '''x''' and '''y''' properties, and then sets the metatable to the '''Vector''' class. As we know, '''Vector''' contains all the metamethods and especially the '''__index''' key. This means that we can use all the functions we define in '''Vector''' through this new table. We'll come back to this in a moment.&lt;br /&gt;
&lt;br /&gt;
Another important thing is the last line:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
setmetatable(Vector, { __call = function(_, ...) return Vector.new(...) end })&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This means that we can create a new '''Vector''' instance by either calling '''Vector.new''' or just '''Vector'''.&lt;br /&gt;
&lt;br /&gt;
The last important thing that you may not be aware of is the colon syntax. When we define a function with a colon, like this:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
function t:method(a, b, c)&lt;br /&gt;
  -- ...&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
What we are really doing is defining this function:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
function t.method(self, a, b, c)&lt;br /&gt;
  -- ...&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is syntactic sugar to help with OOP. We can then use the colon syntax when calling functions:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
-- these are the same&lt;br /&gt;
t:method(1, 2, 3)&lt;br /&gt;
t.method(t, 1, 2, 3)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now, how do we use this '''Vector''' class? Here's a final example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
a = Vector.new(10, 10)&lt;br /&gt;
b = Vector(20, 11)&lt;br /&gt;
c = a + b&lt;br /&gt;
print(a:len()) -- 14.142135623731&lt;br /&gt;
print(a) -- (10, 10)&lt;br /&gt;
print(c) -- (30, 21)&lt;br /&gt;
print(a &amp;lt; c) -- true&lt;br /&gt;
print(a == b) -- false&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Because of the '''__index''' in '''Vector''', we can use all the methods defined in the class through the instances.&lt;br /&gt;
&lt;br /&gt;
== Advanced examples ==&lt;br /&gt;
Overriding self's value:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
local array = {text = &amp;quot;none&amp;quot;}&lt;br /&gt;
local array2 = {text = &amp;quot;none2&amp;quot;}&lt;br /&gt;
function array:setKey (key, value)&lt;br /&gt;
	self[key] = value&lt;br /&gt;
end&lt;br /&gt;
function array:getKey (key)&lt;br /&gt;
	return self[key]&lt;br /&gt;
end&lt;br /&gt;
array.getKey(array2, &amp;quot;text&amp;quot;) -- returns &amp;quot;none2&amp;quot;&lt;br /&gt;
array.setKey(array2, &amp;quot;text&amp;quot;, &amp;quot;something2&amp;quot;) -- sets array2's &amp;quot;text&amp;quot; value to 'something2'&lt;br /&gt;
array.getKey(array2, &amp;quot;text&amp;quot;) -- returns &amp;quot;something&amp;quot;&lt;br /&gt;
array:getKey(&amp;quot;text&amp;quot;) -- returns &amp;quot;none&amp;quot;&lt;br /&gt;
array.getKey(array, &amp;quot;text&amp;quot;) -- same as above&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A simple backpacks example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
local backpack = {list = {}}&lt;br /&gt;
function backpack:create (owner, slots)&lt;br /&gt;
	if self.list[owner] then return end -- return false in case this player already owns a backpack&lt;br /&gt;
		local new = {&lt;br /&gt;
		items = {},&lt;br /&gt;
		slots = slots or 100,&lt;br /&gt;
		owner = owner&lt;br /&gt;
	}&lt;br /&gt;
	setmetatable(new.items, &lt;br /&gt;
	{&lt;br /&gt;
		__newindex = function(table, key, value) -- this is called once a new value/item is added into the player's table/backpack&lt;br /&gt;
			if #table &amp;gt;= new.slots then return end -- return false in case there isn't any free slot left&lt;br /&gt;
			return rawset(table, key, value)&lt;br /&gt;
		end&lt;br /&gt;
	}&lt;br /&gt;
	)&lt;br /&gt;
	self.list[owner] = new&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function backpack:addItem (player, item, value)&lt;br /&gt;
	if not self.list[player] then return end&lt;br /&gt;
	self.list[player][item] = value&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Novo</name></author>
	</entry>
	<entry>
		<id>https://wiki.multitheftauto.com/index.php?title=OOP_in_Lua&amp;diff=44007</id>
		<title>OOP in Lua</title>
		<link rel="alternate" type="text/html" href="https://wiki.multitheftauto.com/index.php?title=OOP_in_Lua&amp;diff=44007"/>
		<updated>2015-01-18T03:41:35Z</updated>

		<summary type="html">&lt;p&gt;Novo: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Note_box|Any further questions can be discussed at the [http://forum.mtasa.com/viewtopic.php?f=148&amp;amp;t=84365 topic] dedicated to this tutorial.}}&lt;br /&gt;
__TOC__&lt;br /&gt;
== Introduction and other related pages ==&lt;br /&gt;
This is a scripting tutorial teaches you the basics about how to start using an Object-Oriented developing interface with Lua.&lt;br /&gt;
* [[OOP_Introduction|OOP Introduction - MTA-related]] ''by qaisjp3''&lt;br /&gt;
* [[OOP|OOP]]&lt;br /&gt;
* [[OOP_client|MTA built-in client-sided OO functions]]&lt;br /&gt;
* [[OOP_server|MTA built-in server-sided OO functions]]&lt;br /&gt;
&lt;br /&gt;
== Glossary ==&lt;br /&gt;
* environment: either a table or an array containing values.&lt;br /&gt;
* self: predefined variable referring to the environment within which we are executing the code.&lt;br /&gt;
&lt;br /&gt;
== Initialising ==&lt;br /&gt;
There is a basic and simple predefined variables we should recognize and know: '''[[#Glossary|self]]'''.&lt;br /&gt;
=== Our first environment ===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
local array = {}&lt;br /&gt;
function array:example (argument)&lt;br /&gt;
	return &amp;quot;Hello&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
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:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
array:example()&lt;br /&gt;
array.example(array, example)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
As Lua is so cool, we're able to call a function using two methods: &amp;quot;''':'''&amp;quot; and &amp;quot;'''.'''&amp;quot;. 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|advanced examples]]).&lt;br /&gt;
&lt;br /&gt;
=== Variables and further handling ===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
local array = {text = &amp;quot;none&amp;quot;}&lt;br /&gt;
function array:setKey (key, value)&lt;br /&gt;
	self[key] = value&lt;br /&gt;
end&lt;br /&gt;
function array:getKey (key)&lt;br /&gt;
	return self[key]&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
array:getKey(&amp;quot;text&amp;quot;) -- returns &amp;quot;none&amp;quot;&lt;br /&gt;
array:setKey(&amp;quot;text&amp;quot;, &amp;quot;something&amp;quot;) -- sets &amp;quot;text&amp;quot;'s value to 'something'&lt;br /&gt;
array:getKey(&amp;quot;text&amp;quot;) -- returns &amp;quot;something&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
== Metatables ==&lt;br /&gt;
Content's provenance: [http://nova-fusion.com/2011/06/30/lua-metatables-tutorial/ NovaFusion]&lt;br /&gt;
----&lt;br /&gt;
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.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
t = {} -- our normal table&lt;br /&gt;
mt = {} -- our metatable, which contains nothing right now&lt;br /&gt;
setmetatable(t, mt) -- sets mt to be t's metatable&lt;br /&gt;
getmetatable(t) -- this will return mt&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
t = setmetatable({}, {})&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
'''setmetatable''' returns its first argument, therefore we can use this shorter form.&lt;br /&gt;
&lt;br /&gt;
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:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
t = setmetatable({}, {&lt;br /&gt;
  __index = function(t, key)&lt;br /&gt;
    if key == &amp;quot;foo&amp;quot; then&lt;br /&gt;
      return 0&lt;br /&gt;
    else&lt;br /&gt;
      return table[key]&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
})&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
=== __index ===&lt;br /&gt;
&lt;br /&gt;
The most used metatable key is most likely '''__index'''; it can contain either a function or table.&lt;br /&gt;
&lt;br /&gt;
When you look up a table with a key, regardless of what the key is (t[4], t.foo, and t[&amp;quot;foo&amp;quot;], 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:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
other = { foo = 3 }&lt;br /&gt;
t = setmetatable({}, { __index = other })&lt;br /&gt;
t.foo -- 3&lt;br /&gt;
t.bar -- nil&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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 &amp;quot;foo&amp;quot; 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 &amp;quot;foo&amp;quot; is used.&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
=== __newindex ===&lt;br /&gt;
&lt;br /&gt;
Next in line is '''__newindex''', which is similar to '''__index'''. Like '''__index''', it can contain either a function or table.&lt;br /&gt;
&lt;br /&gt;
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:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
other = {}&lt;br /&gt;
t = setmetatable({}, { __newindex = other })&lt;br /&gt;
t.foo = 3&lt;br /&gt;
other.foo -- 3&lt;br /&gt;
t.foo -- nil&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
As would be expected, if '''__newindex''' is a function, it will be called with the table, key, and value passed as parameters:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
t = setmetatable({}, {&lt;br /&gt;
  __newindex = function(t, key, value)&lt;br /&gt;
    if type(value) == &amp;quot;number&amp;quot; then&lt;br /&gt;
      rawset(t, key, value * value)&lt;br /&gt;
    else&lt;br /&gt;
      rawset(t, key, value)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
})&lt;br /&gt;
&lt;br /&gt;
t.foo = &amp;quot;foo&amp;quot;&lt;br /&gt;
t.bar = 4&lt;br /&gt;
t.la = 10&lt;br /&gt;
t.foo -- &amp;quot;foo&amp;quot;&lt;br /&gt;
t.bar -- 16&lt;br /&gt;
t.la -- 100&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
When creating a new key in '''t''', if the value is a number it will be squared, otherwise it will just be set anyway. This introduces us to our friends, '''rawget''' and '''rawset'''.&lt;br /&gt;
&lt;br /&gt;
=== rawget and rawset ===&lt;br /&gt;
&lt;br /&gt;
There are times when you need get and set a table's keys without having Lua do it's thing with metatables. As you might guess, [http://www.lua.org/manual/5.1/manual.html#pdf-rawget rawget] allows you to get the value of a key without Lua using '''__index''', and [http://www.lua.org/manual/5.1/manual.html#pdf-rawset rawset] allows you to set the value of a key without Lua using '''__newindex''' (no these don't provide a speed increase to conventional way of doing things). You'll need to use these when you would otherwise get stuck in an infinite loop. For example, in that last code example, the code '''t[key] = value * value''' would set off the same '''__newindex''' function again, which would get you stuck in an infinite loop. Using '''rawset(t, key, value * value)''' avoids this.&lt;br /&gt;
&lt;br /&gt;
As you probably can see, to use these functions, for parameters we must pass in the target table, the key, and if you're using '''rawset''', the value.&lt;br /&gt;
&lt;br /&gt;
=== Operators ===&lt;br /&gt;
&lt;br /&gt;
Many of the metatable keys available have to do with operators (as in, '''+''', '''-''', etc.), allowing you to make tables support the use of operators on them. For example, say we wanted a table to support the multiplication operator ('''*'''), this is how we would do it:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
t = setmetatable({ 1, 2, 3 }, {&lt;br /&gt;
  __mul = function(t, other)&lt;br /&gt;
    new = {}&lt;br /&gt;
    &lt;br /&gt;
    for i = 1, other do&lt;br /&gt;
      for _, v in ipairs(t) do table.insert(new, v) end&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    return new&lt;br /&gt;
  end&lt;br /&gt;
})&lt;br /&gt;
&lt;br /&gt;
t = t * 2 -- { 1, 2, 3, 1, 2, 3 }&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This allows us to create a new table with the original replicated a certain amount of times using the multiplication operator. As you can tell, the corresponding key for multiplication is '''__mul'''; unlike '''__index''' and '''__newindex''' the keys for operators can only contain functions. The first parameter these functions always receive is the target table, and then the value on the right hand side (except for the unary - which has the key of '''__unm'''). Here's a list of the supported operators:&lt;br /&gt;
&lt;br /&gt;
* __add: Addition (+)&lt;br /&gt;
* __sub: Subtraction (-)&lt;br /&gt;
* __mul: Multiplication (*)&lt;br /&gt;
* __div: Division (/)&lt;br /&gt;
* __mod: Modulos (%)&lt;br /&gt;
* __unm: Unary -, used for negation on numbers&lt;br /&gt;
* __concat: Concatenation (..)&lt;br /&gt;
* __eq: Equality (==)&lt;br /&gt;
* __lt: Less than (&amp;lt;)&lt;br /&gt;
* __le: Less than or equal to (&amp;lt;=)&lt;br /&gt;
&lt;br /&gt;
(There's only '''==''', '''&amp;lt;''', '''&amp;lt;=''' because you can implement full support for the comparison operators with just those; in fact only '''==''' and '''&amp;lt;''' are needed.)&lt;br /&gt;
&lt;br /&gt;
=== __call ===&lt;br /&gt;
&lt;br /&gt;
Next comes the '''__call''' key, which allows you to call tables as functions. A code example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
t = setmetatable({}, {&lt;br /&gt;
  __call = function(t, a, b, c, whatever)&lt;br /&gt;
    return (a + b + c) * whatever&lt;br /&gt;
  end&lt;br /&gt;
})&lt;br /&gt;
&lt;br /&gt;
t(1, 2, 3, 4) -- 24&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The function in call is passed the target table as usual, followed by the parameters that we passed in. '''__call''' is very useful for many things. One common thing it's used for is forwarding a call on a table to a function inside that table.&lt;br /&gt;
&lt;br /&gt;
=== __tostring ===&lt;br /&gt;
&lt;br /&gt;
Last of all is '''__tostring'''. If implemented, it's used by [http://www.lua.org/manual/5.1/manual.html#pdf-tostring tostring] to convert a table into a string, most handy when using a function like [http://www.lua.org/manual/5.1/manual.html#pdf-print print]. Normally, when you try to convert a table to a string, you something in the format of &amp;quot;table: 0x&amp;lt;hex-code-here&amp;gt;&amp;quot;, but you can change that using '''__tostring'''. An example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
t = setmetatable({ 1, 2, 3 }, {&lt;br /&gt;
  __tostring = function(t)&lt;br /&gt;
    sum = 0&lt;br /&gt;
    for _, v in pairs(t) do sum = sum + v end&lt;br /&gt;
    return &amp;quot;Sum: &amp;quot; .. sum&lt;br /&gt;
  end&lt;br /&gt;
})&lt;br /&gt;
&lt;br /&gt;
print(t) -- prints out &amp;quot;Sum: 6&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Building the Vector Class ===&lt;br /&gt;
&lt;br /&gt;
To wrap everything up, we'll write a class encapsulating a 2D vector. It's too large to put here, but you can see the full code at [https://gist.github.com/1055480 gist #1055480]. I've positioned all the stuff to do with metatables first in the file, as that's the most important stuff. (Be warned, this may be a bit confusing if you've never encountered Object-Oriented Programming before.)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
Vector = {}&lt;br /&gt;
Vector.__index = Vector&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This code sets up the table for the '''Vector''' class, and sets the '''__index''' key to point back at itself. Now, what's going on here? You've probably noticed that we've put all the metatable methods inside the '''Vector''' class. What you're seeing is the simplest way to achieve OOP (Object-Oriented Programming) in Lua. The '''Vector''' table represents the class, which contains all the functions that instances can use. '''Vector.new''' (shown below) creates a new instance of this class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
function Vector.new(x, y)&lt;br /&gt;
  return setmetatable({ x = x or 0, y = y or 0 }, Vector)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It creates a new table with '''x''' and '''y''' properties, and then sets the metatable to the '''Vector''' class. As we know, '''Vector''' contains all the metamethods and especially the '''__index''' key. This means that we can use all the functions we define in '''Vector''' through this new table. We'll come back to this in a moment.&lt;br /&gt;
&lt;br /&gt;
Another important thing is the last line:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
setmetatable(Vector, { __call = function(_, ...) return Vector.new(...) end })&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This means that we can create a new '''Vector''' instance by either calling '''Vector.new''' or just '''Vector'''.&lt;br /&gt;
&lt;br /&gt;
The last important thing that you may not be aware of is the colon syntax. When we define a function with a colon, like this:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
function t:method(a, b, c)&lt;br /&gt;
  -- ...&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
What we are really doing is defining this function:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
function t.method(self, a, b, c)&lt;br /&gt;
  -- ...&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is syntactic sugar to help with OOP. We can then use the colon syntax when calling functions:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
-- these are the same&lt;br /&gt;
t:method(1, 2, 3)&lt;br /&gt;
t.method(t, 1, 2, 3)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now, how do we use this '''Vector''' class? Here's a final example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
a = Vector.new(10, 10)&lt;br /&gt;
b = Vector(20, 11)&lt;br /&gt;
c = a + b&lt;br /&gt;
print(a:len()) -- 14.142135623731&lt;br /&gt;
print(a) -- (10, 10)&lt;br /&gt;
print(c) -- (30, 21)&lt;br /&gt;
print(a &amp;lt; c) -- true&lt;br /&gt;
print(a == b) -- false&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Because of the '''__index''' in '''Vector''', we can use all the methods defined in the class through the instances.&lt;br /&gt;
&lt;br /&gt;
== Advanced examples ==&lt;br /&gt;
Overriding self's value:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
local array = {text = &amp;quot;none&amp;quot;}&lt;br /&gt;
local array2 = {text = &amp;quot;none2&amp;quot;}&lt;br /&gt;
function array:setKey (key, value)&lt;br /&gt;
	self[key] = value&lt;br /&gt;
end&lt;br /&gt;
function array:getKey (key)&lt;br /&gt;
	return self[key]&lt;br /&gt;
end&lt;br /&gt;
array.getKey(array2, &amp;quot;text&amp;quot;) -- returns &amp;quot;none2&amp;quot;&lt;br /&gt;
array.setKey(array2, &amp;quot;text&amp;quot;, &amp;quot;something2&amp;quot;) -- sets array2's &amp;quot;text&amp;quot; value to 'something2'&lt;br /&gt;
array.getKey(array2, &amp;quot;text&amp;quot;) -- returns &amp;quot;something&amp;quot;&lt;br /&gt;
array:getKey(&amp;quot;text&amp;quot;) -- returns &amp;quot;none&amp;quot;&lt;br /&gt;
array.getKey(array, &amp;quot;text&amp;quot;) -- same as above&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A simple backpacks example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
local backpack = {list = {}}&lt;br /&gt;
function backpack:create (owner, slots)&lt;br /&gt;
	if self.list[owner] then return end -- return false in case this player already owns a backpack&lt;br /&gt;
		local new = {&lt;br /&gt;
		items = {},&lt;br /&gt;
		slots = slots or 100,&lt;br /&gt;
		owner = owner&lt;br /&gt;
	}&lt;br /&gt;
	setmetatable(new.items, &lt;br /&gt;
	{&lt;br /&gt;
		__newindex = function(table, key, value) -- this is called once a new value/item is added into the player's table/backpack&lt;br /&gt;
			if #table &amp;gt;= new.slots then return end -- return false in case there isn't any free slot left&lt;br /&gt;
			return rawset(table, key, value)&lt;br /&gt;
		end&lt;br /&gt;
	}&lt;br /&gt;
	)&lt;br /&gt;
	self.list[owner] = new&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function backpack:addItem (player, item, value)&lt;br /&gt;
	if not self.list[player] then return end&lt;br /&gt;
	self.list[player][item] = value&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Novo</name></author>
	</entry>
	<entry>
		<id>https://wiki.multitheftauto.com/index.php?title=OOP_in_Lua&amp;diff=44006</id>
		<title>OOP in Lua</title>
		<link rel="alternate" type="text/html" href="https://wiki.multitheftauto.com/index.php?title=OOP_in_Lua&amp;diff=44006"/>
		<updated>2015-01-18T03:34:03Z</updated>

		<summary type="html">&lt;p&gt;Novo: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;pageclass subcaption=&amp;quot;Adam James @adam.mta&amp;quot;&amp;gt;&amp;lt;/pageclass&amp;gt;&lt;br /&gt;
{{Note_box|Any further questions can be discussed at the [http://google.com topic] dedicated to this tutorial.}}&lt;br /&gt;
__TOC__&lt;br /&gt;
== Introduction and other related pages ==&lt;br /&gt;
This is a scripting tutorial teaches you the basics about how to start using an Object-Oriented developing interface with Lua.&lt;br /&gt;
* [[OOP_Introduction|OOP Introduction - MTA-related]] ''by qaisjp3''&lt;br /&gt;
* [[OOP|OOP]]&lt;br /&gt;
* [[OOP_client|MTA built-in client-sided OO functions]]&lt;br /&gt;
* [[OOP_server|MTA built-in server-sided OO functions]]&lt;br /&gt;
&lt;br /&gt;
== Glossary ==&lt;br /&gt;
* environment: either a table or an array containing values.&lt;br /&gt;
* self: predefined variable referring to the environment within which we are executing the code.&lt;br /&gt;
&lt;br /&gt;
== Initialising ==&lt;br /&gt;
There is a basic and simple predefined variables we should recognize and know: '''[[#Glossary|self]]'''.&lt;br /&gt;
=== Our first environment ===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
local array = {}&lt;br /&gt;
function array:example (argument)&lt;br /&gt;
	return &amp;quot;Hello&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
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:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
array:example()&lt;br /&gt;
array.example(array, example)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
As Lua is so cool, we're able to call a function using two methods: &amp;quot;''':'''&amp;quot; and &amp;quot;'''.'''&amp;quot;. 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|advanced examples]]).&lt;br /&gt;
&lt;br /&gt;
=== Variables and further handling ===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
local array = {text = &amp;quot;none&amp;quot;}&lt;br /&gt;
function array:setKey (key, value)&lt;br /&gt;
	self[key] = value&lt;br /&gt;
end&lt;br /&gt;
function array:getKey (key)&lt;br /&gt;
	return self[key]&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
array:getKey(&amp;quot;text&amp;quot;) -- returns &amp;quot;none&amp;quot;&lt;br /&gt;
array:setKey(&amp;quot;text&amp;quot;, &amp;quot;something&amp;quot;) -- sets &amp;quot;text&amp;quot;'s value to 'something'&lt;br /&gt;
array:getKey(&amp;quot;text&amp;quot;) -- returns &amp;quot;something&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
== Metatables ==&lt;br /&gt;
Content's provenance: [http://nova-fusion.com/2011/06/30/lua-metatables-tutorial/ NovaFusion]&lt;br /&gt;
----&lt;br /&gt;
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.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
t = {} -- our normal table&lt;br /&gt;
mt = {} -- our metatable, which contains nothing right now&lt;br /&gt;
setmetatable(t, mt) -- sets mt to be t's metatable&lt;br /&gt;
getmetatable(t) -- this will return mt&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
t = setmetatable({}, {})&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
'''setmetatable''' returns its first argument, therefore we can use this shorter form.&lt;br /&gt;
&lt;br /&gt;
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:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
t = setmetatable({}, {&lt;br /&gt;
  __index = function(t, key)&lt;br /&gt;
    if key == &amp;quot;foo&amp;quot; then&lt;br /&gt;
      return 0&lt;br /&gt;
    else&lt;br /&gt;
      return table[key]&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
})&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
=== __index ===&lt;br /&gt;
&lt;br /&gt;
The most used metatable key is most likely '''__index'''; it can contain either a function or table.&lt;br /&gt;
&lt;br /&gt;
When you look up a table with a key, regardless of what the key is (t[4], t.foo, and t[&amp;quot;foo&amp;quot;], 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:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
other = { foo = 3 }&lt;br /&gt;
t = setmetatable({}, { __index = other })&lt;br /&gt;
t.foo -- 3&lt;br /&gt;
t.bar -- nil&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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 &amp;quot;foo&amp;quot; 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 &amp;quot;foo&amp;quot; is used.&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
=== __newindex ===&lt;br /&gt;
&lt;br /&gt;
Next in line is '''__newindex''', which is similar to '''__index'''. Like '''__index''', it can contain either a function or table.&lt;br /&gt;
&lt;br /&gt;
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:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
other = {}&lt;br /&gt;
t = setmetatable({}, { __newindex = other })&lt;br /&gt;
t.foo = 3&lt;br /&gt;
other.foo -- 3&lt;br /&gt;
t.foo -- nil&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
As would be expected, if '''__newindex''' is a function, it will be called with the table, key, and value passed as parameters:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
t = setmetatable({}, {&lt;br /&gt;
  __newindex = function(t, key, value)&lt;br /&gt;
    if type(value) == &amp;quot;number&amp;quot; then&lt;br /&gt;
      rawset(t, key, value * value)&lt;br /&gt;
    else&lt;br /&gt;
      rawset(t, key, value)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
})&lt;br /&gt;
&lt;br /&gt;
t.foo = &amp;quot;foo&amp;quot;&lt;br /&gt;
t.bar = 4&lt;br /&gt;
t.la = 10&lt;br /&gt;
t.foo -- &amp;quot;foo&amp;quot;&lt;br /&gt;
t.bar -- 16&lt;br /&gt;
t.la -- 100&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
When creating a new key in '''t''', if the value is a number it will be squared, otherwise it will just be set anyway. This introduces us to our friends, '''rawget''' and '''rawset'''.&lt;br /&gt;
&lt;br /&gt;
=== rawget and rawset ===&lt;br /&gt;
&lt;br /&gt;
There are times when you need get and set a table's keys without having Lua do it's thing with metatables. As you might guess, [http://www.lua.org/manual/5.1/manual.html#pdf-rawget rawget] allows you to get the value of a key without Lua using '''__index''', and [http://www.lua.org/manual/5.1/manual.html#pdf-rawset rawset] allows you to set the value of a key without Lua using '''__newindex''' (no these don't provide a speed increase to conventional way of doing things). You'll need to use these when you would otherwise get stuck in an infinite loop. For example, in that last code example, the code '''t[key] = value * value''' would set off the same '''__newindex''' function again, which would get you stuck in an infinite loop. Using '''rawset(t, key, value * value)''' avoids this.&lt;br /&gt;
&lt;br /&gt;
As you probably can see, to use these functions, for parameters we must pass in the target table, the key, and if you're using '''rawset''', the value.&lt;br /&gt;
&lt;br /&gt;
=== Operators ===&lt;br /&gt;
&lt;br /&gt;
Many of the metatable keys available have to do with operators (as in, '''+''', '''-''', etc.), allowing you to make tables support the use of operators on them. For example, say we wanted a table to support the multiplication operator ('''*'''), this is how we would do it:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
t = setmetatable({ 1, 2, 3 }, {&lt;br /&gt;
  __mul = function(t, other)&lt;br /&gt;
    new = {}&lt;br /&gt;
    &lt;br /&gt;
    for i = 1, other do&lt;br /&gt;
      for _, v in ipairs(t) do table.insert(new, v) end&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    return new&lt;br /&gt;
  end&lt;br /&gt;
})&lt;br /&gt;
&lt;br /&gt;
t = t * 2 -- { 1, 2, 3, 1, 2, 3 }&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This allows us to create a new table with the original replicated a certain amount of times using the multiplication operator. As you can tell, the corresponding key for multiplication is '''__mul'''; unlike '''__index''' and '''__newindex''' the keys for operators can only contain functions. The first parameter these functions always receive is the target table, and then the value on the right hand side (except for the unary - which has the key of '''__unm'''). Here's a list of the supported operators:&lt;br /&gt;
&lt;br /&gt;
* __add: Addition (+)&lt;br /&gt;
* __sub: Subtraction (-)&lt;br /&gt;
* __mul: Multiplication (*)&lt;br /&gt;
* __div: Division (/)&lt;br /&gt;
* __mod: Modulos (%)&lt;br /&gt;
* __unm: Unary -, used for negation on numbers&lt;br /&gt;
* __concat: Concatenation (..)&lt;br /&gt;
* __eq: Equality (==)&lt;br /&gt;
* __lt: Less than (&amp;lt;)&lt;br /&gt;
* __le: Less than or equal to (&amp;lt;=)&lt;br /&gt;
&lt;br /&gt;
(There's only '''==''', '''&amp;lt;''', '''&amp;lt;=''' because you can implement full support for the comparison operators with just those; in fact only '''==''' and '''&amp;lt;''' are needed.)&lt;br /&gt;
&lt;br /&gt;
=== __call ===&lt;br /&gt;
&lt;br /&gt;
Next comes the '''__call''' key, which allows you to call tables as functions. A code example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
t = setmetatable({}, {&lt;br /&gt;
  __call = function(t, a, b, c, whatever)&lt;br /&gt;
    return (a + b + c) * whatever&lt;br /&gt;
  end&lt;br /&gt;
})&lt;br /&gt;
&lt;br /&gt;
t(1, 2, 3, 4) -- 24&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The function in call is passed the target table as usual, followed by the parameters that we passed in. '''__call''' is very useful for many things. One common thing it's used for is forwarding a call on a table to a function inside that table.&lt;br /&gt;
&lt;br /&gt;
=== __tostring ===&lt;br /&gt;
&lt;br /&gt;
Last of all is '''__tostring'''. If implemented, it's used by [http://www.lua.org/manual/5.1/manual.html#pdf-tostring tostring] to convert a table into a string, most handy when using a function like [http://www.lua.org/manual/5.1/manual.html#pdf-print print]. Normally, when you try to convert a table to a string, you something in the format of &amp;quot;table: 0x&amp;lt;hex-code-here&amp;gt;&amp;quot;, but you can change that using '''__tostring'''. An example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
t = setmetatable({ 1, 2, 3 }, {&lt;br /&gt;
  __tostring = function(t)&lt;br /&gt;
    sum = 0&lt;br /&gt;
    for _, v in pairs(t) do sum = sum + v end&lt;br /&gt;
    return &amp;quot;Sum: &amp;quot; .. sum&lt;br /&gt;
  end&lt;br /&gt;
})&lt;br /&gt;
&lt;br /&gt;
print(t) -- prints out &amp;quot;Sum: 6&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Building the Vector Class ===&lt;br /&gt;
&lt;br /&gt;
To wrap everything up, we'll write a class encapsulating a 2D vector. It's too large to put here, but you can see the full code at [https://gist.github.com/1055480 gist #1055480]. I've positioned all the stuff to do with metatables first in the file, as that's the most important stuff. (Be warned, this may be a bit confusing if you've never encountered Object-Oriented Programming before.)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
Vector = {}&lt;br /&gt;
Vector.__index = Vector&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This code sets up the table for the '''Vector''' class, and sets the '''__index''' key to point back at itself. Now, what's going on here? You've probably noticed that we've put all the metatable methods inside the '''Vector''' class. What you're seeing is the simplest way to achieve OOP (Object-Oriented Programming) in Lua. The '''Vector''' table represents the class, which contains all the functions that instances can use. '''Vector.new''' (shown below) creates a new instance of this class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
function Vector.new(x, y)&lt;br /&gt;
  return setmetatable({ x = x or 0, y = y or 0 }, Vector)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It creates a new table with '''x''' and '''y''' properties, and then sets the metatable to the '''Vector''' class. As we know, '''Vector''' contains all the metamethods and especially the '''__index''' key. This means that we can use all the functions we define in '''Vector''' through this new table. We'll come back to this in a moment.&lt;br /&gt;
&lt;br /&gt;
Another important thing is the last line:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
setmetatable(Vector, { __call = function(_, ...) return Vector.new(...) end })&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This means that we can create a new '''Vector''' instance by either calling '''Vector.new''' or just '''Vector'''.&lt;br /&gt;
&lt;br /&gt;
The last important thing that you may not be aware of is the colon syntax. When we define a function with a colon, like this:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
function t:method(a, b, c)&lt;br /&gt;
  -- ...&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
What we are really doing is defining this function:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
function t.method(self, a, b, c)&lt;br /&gt;
  -- ...&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is syntactic sugar to help with OOP. We can then use the colon syntax when calling functions:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
-- these are the same&lt;br /&gt;
t:method(1, 2, 3)&lt;br /&gt;
t.method(t, 1, 2, 3)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now, how do we use this '''Vector''' class? Here's a final example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
a = Vector.new(10, 10)&lt;br /&gt;
b = Vector(20, 11)&lt;br /&gt;
c = a + b&lt;br /&gt;
print(a:len()) -- 14.142135623731&lt;br /&gt;
print(a) -- (10, 10)&lt;br /&gt;
print(c) -- (30, 21)&lt;br /&gt;
print(a &amp;lt; c) -- true&lt;br /&gt;
print(a == b) -- false&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Because of the '''__index''' in '''Vector''', we can use all the methods defined in the class through the instances.&lt;br /&gt;
&lt;br /&gt;
== Advanced examples ==&lt;br /&gt;
Overriding self's value:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
local array = {text = &amp;quot;none&amp;quot;}&lt;br /&gt;
local array2 = {text = &amp;quot;none2&amp;quot;}&lt;br /&gt;
function array:setKey (key, value)&lt;br /&gt;
	self[key] = value&lt;br /&gt;
end&lt;br /&gt;
function array:getKey (key)&lt;br /&gt;
	return self[key]&lt;br /&gt;
end&lt;br /&gt;
array.getKey(array2, &amp;quot;text&amp;quot;) -- returns &amp;quot;none2&amp;quot;&lt;br /&gt;
array.setKey(array2, &amp;quot;text&amp;quot;, &amp;quot;something2&amp;quot;) -- sets array2's &amp;quot;text&amp;quot; value to 'something2'&lt;br /&gt;
array.getKey(array2, &amp;quot;text&amp;quot;) -- returns &amp;quot;something&amp;quot;&lt;br /&gt;
array:getKey(&amp;quot;text&amp;quot;) -- returns &amp;quot;none&amp;quot;&lt;br /&gt;
array.getKey(array, &amp;quot;text&amp;quot;) -- same as above&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A simple backpacks example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
local backpack = {list = {}}&lt;br /&gt;
function backpack:create (owner, slots)&lt;br /&gt;
	if self.list[owner] then return end -- return false in case this player already owns a backpack&lt;br /&gt;
		local new = {&lt;br /&gt;
		items = {},&lt;br /&gt;
		slots = slots or 100,&lt;br /&gt;
		owner = owner&lt;br /&gt;
	}&lt;br /&gt;
	setmetatable(new.items, &lt;br /&gt;
	{&lt;br /&gt;
		__newindex = function(table, key, value) -- this is called once a new value/item is added into the player's table/backpack&lt;br /&gt;
			if #table &amp;gt;= new.slots then return end -- return false in case there isn't any free slot left&lt;br /&gt;
			return rawset(table, key, value)&lt;br /&gt;
		end&lt;br /&gt;
	}&lt;br /&gt;
	)&lt;br /&gt;
	self.list[owner] = new&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function backpack:addItem (player, item, value)&lt;br /&gt;
	if not self.list[player] then return end&lt;br /&gt;
	self.list[player][item] = value&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Novo</name></author>
	</entry>
	<entry>
		<id>https://wiki.multitheftauto.com/index.php?title=OOP_in_Lua&amp;diff=44005</id>
		<title>OOP in Lua</title>
		<link rel="alternate" type="text/html" href="https://wiki.multitheftauto.com/index.php?title=OOP_in_Lua&amp;diff=44005"/>
		<updated>2015-01-18T03:33:26Z</updated>

		<summary type="html">&lt;p&gt;Novo: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;pageclass subcaption=&amp;quot;Adam James @adam.mta&amp;quot;&amp;gt;&amp;lt;/pageclass&amp;gt;&lt;br /&gt;
{{Note_box|Any further questions can be discussed at the [http://google.com topic] dedicated to this tutorial.}}&lt;br /&gt;
__TOC__&lt;br /&gt;
== Introduction and other related pages ==&lt;br /&gt;
This is a scripting tutorial teaches you the basics about how to start using an Object-Oriented developing interface with Lua.&lt;br /&gt;
* [[OOP_Introduction|OOP Introduction - MTA-related]] ''by qaisjp3''&lt;br /&gt;
* [[OOP|OOP]]&lt;br /&gt;
* [[OOP_client|MTA built-in client-sided OO functions]]&lt;br /&gt;
* [[OOP_server|MTA built-in server-sided OO functions]]&lt;br /&gt;
&lt;br /&gt;
== Glossary ==&lt;br /&gt;
* environment: either a table or an array containing values.&lt;br /&gt;
* self: predefined variable referring to the environment within which we are executing the code.&lt;br /&gt;
&lt;br /&gt;
== Initialising ==&lt;br /&gt;
There is a basic and simple predefined variables we should recognize and know: '''[[#Glossary|self]]'''.&lt;br /&gt;
=== Our first environment ===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
local array = {}&lt;br /&gt;
function array:example (argument)&lt;br /&gt;
	return &amp;quot;Hello&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
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:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
array:example()&lt;br /&gt;
array.example(array, example)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
As Lua is so cool, we're able to call a function using two methods: &amp;quot;''':'''&amp;quot; and &amp;quot;'''.'''&amp;quot;. 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|advanced examples]]).&lt;br /&gt;
&lt;br /&gt;
=== Variables and further handling ===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
local array = {text = &amp;quot;none&amp;quot;}&lt;br /&gt;
function array:setKey (key, value)&lt;br /&gt;
	self[key] = value&lt;br /&gt;
end&lt;br /&gt;
function array:getKey (key)&lt;br /&gt;
	return self[key]&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
array:getKey(&amp;quot;text&amp;quot;) -- returns &amp;quot;none&amp;quot;&lt;br /&gt;
array:setKey(&amp;quot;text&amp;quot;, &amp;quot;something&amp;quot;) -- sets &amp;quot;text&amp;quot;'s value to 'something'&lt;br /&gt;
array:getKey(&amp;quot;text&amp;quot;) -- returns &amp;quot;something&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
== Metatables ==&lt;br /&gt;
Content's provenance: [http://nova-fusion.com/2011/06/30/lua-metatables-tutorial/ NovaFusion]&lt;br /&gt;
----&lt;br /&gt;
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.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
t = {} -- our normal table&lt;br /&gt;
mt = {} -- our metatable, which contains nothing right now&lt;br /&gt;
setmetatable(t, mt) -- sets mt to be t's metatable&lt;br /&gt;
getmetatable(t) -- this will return mt&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
t = setmetatable({}, {})&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
'''setmetatable''' returns its first argument, therefore we can use this shorter form.&lt;br /&gt;
&lt;br /&gt;
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:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
t = setmetatable({}, {&lt;br /&gt;
  __index = function(t, key)&lt;br /&gt;
    if key == &amp;quot;foo&amp;quot; then&lt;br /&gt;
      return 0&lt;br /&gt;
    else&lt;br /&gt;
      return table[key]&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
})&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
=== __index ===&lt;br /&gt;
&lt;br /&gt;
The most used metatable key is most likely '''__index'''; it can contain either a function or table.&lt;br /&gt;
&lt;br /&gt;
When you look up a table with a key, regardless of what the key is (t[4], t.foo, and t[&amp;quot;foo&amp;quot;], 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:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
other = { foo = 3 }&lt;br /&gt;
t = setmetatable({}, { __index = other })&lt;br /&gt;
t.foo -- 3&lt;br /&gt;
t.bar -- nil&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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 &amp;quot;foo&amp;quot; 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 &amp;quot;foo&amp;quot; is used.&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
=== __newindex ===&lt;br /&gt;
&lt;br /&gt;
Next in line is '''__newindex''', which is similar to '''__index'''. Like '''__index''', it can contain either a function or table.&lt;br /&gt;
&lt;br /&gt;
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:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
other = {}&lt;br /&gt;
t = setmetatable({}, { __newindex = other })&lt;br /&gt;
t.foo = 3&lt;br /&gt;
other.foo -- 3&lt;br /&gt;
t.foo -- nil&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
As would be expected, if '''__newindex''' is a function, it will be called with the table, key, and value passed as parameters:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
t = setmetatable({}, {&lt;br /&gt;
  __newindex = function(t, key, value)&lt;br /&gt;
    if type(value) == &amp;quot;number&amp;quot; then&lt;br /&gt;
      rawset(t, key, value * value)&lt;br /&gt;
    else&lt;br /&gt;
      rawset(t, key, value)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
})&lt;br /&gt;
&lt;br /&gt;
t.foo = &amp;quot;foo&amp;quot;&lt;br /&gt;
t.bar = 4&lt;br /&gt;
t.la = 10&lt;br /&gt;
t.foo -- &amp;quot;foo&amp;quot;&lt;br /&gt;
t.bar -- 16&lt;br /&gt;
t.la -- 100&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
When creating a new key in '''t''', if the value is a number it will be squared, otherwise it will just be set anyway. This introduces us to our friends, '''rawget''' and '''rawset'''.&lt;br /&gt;
&lt;br /&gt;
=== rawget and rawset ===&lt;br /&gt;
&lt;br /&gt;
There are times when you need get and set a table's keys without having Lua do it's thing with metatables. As you might guess, [http://www.lua.org/manual/5.1/manual.html#pdf-rawget rawget] allows you to get the value of a key without Lua using '''__index''', and [http://www.lua.org/manual/5.1/manual.html#pdf-rawset rawset] allows you to set the value of a key without Lua using '''__newindex''' (no these don't provide a speed increase to conventional way of doing things). You'll need to use these when you would otherwise get stuck in an infinite loop. For example, in that last code example, the code '''t[key] = value * value''' would set off the same '''__newindex''' function again, which would get you stuck in an infinite loop. Using '''rawset(t, key, value * value)''' avoids this.&lt;br /&gt;
&lt;br /&gt;
As you probably can see, to use these functions, for parameters we must pass in the target table, the key, and if you're using '''rawset''', the value.&lt;br /&gt;
&lt;br /&gt;
=== Operators ===&lt;br /&gt;
&lt;br /&gt;
Many of the metatable keys available have to do with operators (as in, '''+''', '''-''', etc.), allowing you to make tables support the use of operators on them. For example, say we wanted a table to support the multiplication operator ('''*'''), this is how we would do it:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
t = setmetatable({ 1, 2, 3 }, {&lt;br /&gt;
  __mul = function(t, other)&lt;br /&gt;
    new = {}&lt;br /&gt;
    &lt;br /&gt;
    for i = 1, other do&lt;br /&gt;
      for _, v in ipairs(t) do table.insert(new, v) end&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    return new&lt;br /&gt;
  end&lt;br /&gt;
})&lt;br /&gt;
&lt;br /&gt;
t = t * 2 -- { 1, 2, 3, 1, 2, 3 }&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This allows us to create a new table with the original replicated a certain amount of times using the multiplication operator. As you can tell, the corresponding key for multiplication is '''__mul'''; unlike '''__index''' and '''__newindex''' the keys for operators can only contain functions. The first parameter these functions always receive is the target table, and then the value on the right hand side (except for the unary - which has the key of '''__unm'''). Here's a list of the supported operators:&lt;br /&gt;
&lt;br /&gt;
* __add: Addition (+)&lt;br /&gt;
* __sub: Subtraction (-)&lt;br /&gt;
* __mul: Multiplication (*)&lt;br /&gt;
* __div: Division (/)&lt;br /&gt;
* __mod: Modulos (%)&lt;br /&gt;
* __unm: Unary -, used for negation on numbers&lt;br /&gt;
* __concat: Concatenation (..)&lt;br /&gt;
* __eq: Equality (==)&lt;br /&gt;
* __lt: Less than (&amp;lt;)&lt;br /&gt;
* __le: Less than or equal to (&amp;lt;=)&lt;br /&gt;
&lt;br /&gt;
(There's only '''==''', '''&amp;lt;''', '''&amp;lt;=''' because you can implement full support for the comparison operators with just those; in fact only '''==''' and '''&amp;lt;''' are needed.)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== __call ===&lt;br /&gt;
&lt;br /&gt;
Next comes the '''__call''' key, which allows you to call tables as functions. A code example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
t = setmetatable({}, {&lt;br /&gt;
  __call = function(t, a, b, c, whatever)&lt;br /&gt;
    return (a + b + c) * whatever&lt;br /&gt;
  end&lt;br /&gt;
})&lt;br /&gt;
&lt;br /&gt;
t(1, 2, 3, 4) -- 24&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The function in call is passed the target table as usual, followed by the parameters that we passed in. '''__call''' is very useful for many things. One common thing it's used for is forwarding a call on a table to a function inside that table.&lt;br /&gt;
&lt;br /&gt;
=== __tostring ===&lt;br /&gt;
&lt;br /&gt;
Last of all is '''__tostring'''. If implemented, it's used by [http://www.lua.org/manual/5.1/manual.html#pdf-tostring tostring] to convert a table into a string, most handy when using a function like [http://www.lua.org/manual/5.1/manual.html#pdf-print print]. Normally, when you try to convert a table to a string, you something in the format of &amp;quot;table: 0x&amp;lt;hex-code-here&amp;gt;&amp;quot;, but you can change that using '''__tostring'''. An example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
t = setmetatable({ 1, 2, 3 }, {&lt;br /&gt;
  __tostring = function(t)&lt;br /&gt;
    sum = 0&lt;br /&gt;
    for _, v in pairs(t) do sum = sum + v end&lt;br /&gt;
    return &amp;quot;Sum: &amp;quot; .. sum&lt;br /&gt;
  end&lt;br /&gt;
})&lt;br /&gt;
&lt;br /&gt;
print(t) -- prints out &amp;quot;Sum: 6&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Building the Vector Class ===&lt;br /&gt;
&lt;br /&gt;
To wrap everything up, we'll write a class encapsulating a 2D vector. It's too large to put here, but you can see the full code at [https://gist.github.com/1055480 gist #1055480]. I've positioned all the stuff to do with metatables first in the file, as that's the most important stuff. (Be warned, this may be a bit confusing if you've never encountered Object-Oriented Programming before.)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
Vector = {}&lt;br /&gt;
Vector.__index = Vector&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This code sets up the table for the '''Vector''' class, and sets the '''__index''' key to point back at itself. Now, what's going on here? You've probably noticed that we've put all the metatable methods inside the '''Vector''' class. What you're seeing is the simplest way to achieve OOP (Object-Oriented Programming) in Lua. The '''Vector''' table represents the class, which contains all the functions that instances can use. '''Vector.new''' (shown below) creates a new instance of this class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
function Vector.new(x, y)&lt;br /&gt;
  return setmetatable({ x = x or 0, y = y or 0 }, Vector)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It creates a new table with '''x''' and '''y''' properties, and then sets the metatable to the '''Vector''' class. As we know, '''Vector''' contains all the metamethods and especially the '''__index''' key. This means that we can use all the functions we define in '''Vector''' through this new table. We'll come back to this in a moment.&lt;br /&gt;
&lt;br /&gt;
Another important thing is the last line:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
setmetatable(Vector, { __call = function(_, ...) return Vector.new(...) end })&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This means that we can create a new '''Vector''' instance by either calling '''Vector.new''' or just '''Vector'''.&lt;br /&gt;
&lt;br /&gt;
The last important thing that you may not be aware of is the colon syntax. When we define a function with a colon, like this:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
function t:method(a, b, c)&lt;br /&gt;
  -- ...&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
What we are really doing is defining this function:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
function t.method(self, a, b, c)&lt;br /&gt;
  -- ...&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is syntactic sugar to help with OOP. We can then use the colon syntax when calling functions:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
-- these are the same&lt;br /&gt;
t:method(1, 2, 3)&lt;br /&gt;
t.method(t, 1, 2, 3)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now, how do we use this '''Vector''' class? Here's a final example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
a = Vector.new(10, 10)&lt;br /&gt;
b = Vector(20, 11)&lt;br /&gt;
c = a + b&lt;br /&gt;
print(a:len()) -- 14.142135623731&lt;br /&gt;
print(a) -- (10, 10)&lt;br /&gt;
print(c) -- (30, 21)&lt;br /&gt;
print(a &amp;lt; c) -- true&lt;br /&gt;
print(a == b) -- false&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Because of the '''__index''' in '''Vector''', we can use all the methods defined in the class through the instances.&lt;br /&gt;
&lt;br /&gt;
== Advanced examples ==&lt;br /&gt;
Overriding self's value:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
local array = {text = &amp;quot;none&amp;quot;}&lt;br /&gt;
local array2 = {text = &amp;quot;none2&amp;quot;}&lt;br /&gt;
function array:setKey (key, value)&lt;br /&gt;
	self[key] = value&lt;br /&gt;
end&lt;br /&gt;
function array:getKey (key)&lt;br /&gt;
	return self[key]&lt;br /&gt;
end&lt;br /&gt;
array.getKey(array2, &amp;quot;text&amp;quot;) -- returns &amp;quot;none2&amp;quot;&lt;br /&gt;
array.setKey(array2, &amp;quot;text&amp;quot;, &amp;quot;something2&amp;quot;) -- sets array2's &amp;quot;text&amp;quot; value to 'something2'&lt;br /&gt;
array.getKey(array2, &amp;quot;text&amp;quot;) -- returns &amp;quot;something&amp;quot;&lt;br /&gt;
array:getKey(&amp;quot;text&amp;quot;) -- returns &amp;quot;none&amp;quot;&lt;br /&gt;
array.getKey(array, &amp;quot;text&amp;quot;) -- same as above&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A simple backpacks example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
local backpack = {list = {}}&lt;br /&gt;
function backpack:create (owner, slots)&lt;br /&gt;
	if self.list[owner] then return end -- return false in case this player already owns a backpack&lt;br /&gt;
		local new = {&lt;br /&gt;
		items = {},&lt;br /&gt;
		slots = slots or 100,&lt;br /&gt;
		owner = owner&lt;br /&gt;
	}&lt;br /&gt;
	setmetatable(new.items, &lt;br /&gt;
	{&lt;br /&gt;
		__newindex = function(table, key, value) -- this is called once a new value/item is added into the player's table/backpack&lt;br /&gt;
			if #table &amp;gt;= new.slots then return end -- return false in case there isn't any free slot left&lt;br /&gt;
			return rawset(table, key, value)&lt;br /&gt;
		end&lt;br /&gt;
	}&lt;br /&gt;
	)&lt;br /&gt;
	self.list[owner] = new&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function backpack:addItem (player, item, value)&lt;br /&gt;
	if not self.list[player] then return end&lt;br /&gt;
	self.list[player][item] = value&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Novo</name></author>
	</entry>
	<entry>
		<id>https://wiki.multitheftauto.com/index.php?title=OOP_in_Lua&amp;diff=44004</id>
		<title>OOP in Lua</title>
		<link rel="alternate" type="text/html" href="https://wiki.multitheftauto.com/index.php?title=OOP_in_Lua&amp;diff=44004"/>
		<updated>2015-01-18T03:26:42Z</updated>

		<summary type="html">&lt;p&gt;Novo: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;pageclass subcaption=&amp;quot;Adam James @adam.mta&amp;quot;&amp;gt;&amp;lt;/pageclass&amp;gt;&lt;br /&gt;
{{Note_box|Any further questions can be discussed at the [http://google.com topic] dedicated to this tutorial.}}&lt;br /&gt;
__TOC__&lt;br /&gt;
== Introduction and other related pages ==&lt;br /&gt;
This is a scripting tutorial teaches you the basics about how to start using an Object-Oriented developing interface with Lua.&lt;br /&gt;
* [[OOP_Introduction|OOP Introduction - MTA-related]] ''by qaisjp3''&lt;br /&gt;
* [[OOP|OOP]]&lt;br /&gt;
* [[OOP_client|MTA built-in client-sided OO functions]]&lt;br /&gt;
* [[OOP_server|MTA built-in server-sided OO functions]]&lt;br /&gt;
&lt;br /&gt;
== Glossary ==&lt;br /&gt;
* environment: either a table or an array containing values.&lt;br /&gt;
* self: predefined variable referring to the environment within which we are executing the code.&lt;br /&gt;
&lt;br /&gt;
== Initialising ==&lt;br /&gt;
There is a basic and simple predefined variables we should recognize and know: '''[[#Glossary|self]]'''.&lt;br /&gt;
=== Our first environment ===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
local array = {}&lt;br /&gt;
function array:example (argument)&lt;br /&gt;
	return &amp;quot;Hello&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
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:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
array:example()&lt;br /&gt;
array.example(array, example)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
As Lua is so cool, we're able to call a function using two methods: &amp;quot;''':'''&amp;quot; and &amp;quot;'''.'''&amp;quot;. 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|advanced examples]]).&lt;br /&gt;
&lt;br /&gt;
=== Variables and further handling ===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
local array = {text = &amp;quot;none&amp;quot;}&lt;br /&gt;
function array:setKey (key, value)&lt;br /&gt;
	self[key] = value&lt;br /&gt;
end&lt;br /&gt;
function array:getKey (key)&lt;br /&gt;
	return self[key]&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
array:getKey(&amp;quot;text&amp;quot;) -- returns &amp;quot;none&amp;quot;&lt;br /&gt;
array:setKey(&amp;quot;text&amp;quot;, &amp;quot;something&amp;quot;) -- sets &amp;quot;text&amp;quot;'s value to 'something'&lt;br /&gt;
array:getKey(&amp;quot;text&amp;quot;) -- returns &amp;quot;something&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
== Metatables ==&lt;br /&gt;
Content's provenance: [http://nova-fusion.com/2011/06/30/lua-metatables-tutorial/ NovaFusion]&lt;br /&gt;
----&lt;br /&gt;
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.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
t = {} -- our normal table&lt;br /&gt;
mt = {} -- our metatable, which contains nothing right now&lt;br /&gt;
setmetatable(t, mt) -- sets mt to be t's metatable&lt;br /&gt;
getmetatable(t) -- this will return mt&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
t = setmetatable({}, {})&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
'''setmetatable''' returns its first argument, therefore we can use this shorter form.&lt;br /&gt;
&lt;br /&gt;
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:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
t = setmetatable({}, {&lt;br /&gt;
  __index = function(t, key)&lt;br /&gt;
    if key == &amp;quot;foo&amp;quot; then&lt;br /&gt;
      return 0&lt;br /&gt;
    else&lt;br /&gt;
      return table[key]&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
})&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
=== __index ===&lt;br /&gt;
&lt;br /&gt;
The most used metatable key is most likely '''__index'''; it can contain either a function or table.&lt;br /&gt;
&lt;br /&gt;
When you look up a table with a key, regardless of what the key is (t[4], t.foo, and t[&amp;quot;foo&amp;quot;], 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:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
other = { foo = 3 }&lt;br /&gt;
t = setmetatable({}, { __index = other })&lt;br /&gt;
t.foo -- 3&lt;br /&gt;
t.bar -- nil&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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 &amp;quot;foo&amp;quot; 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 &amp;quot;foo&amp;quot; is used.&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
=== __newindex ===&lt;br /&gt;
&lt;br /&gt;
Next in line is '''__newindex''', which is similar to '''__index'''. Like '''__index''', it can contain either a function or table.&lt;br /&gt;
&lt;br /&gt;
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:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
other = {}&lt;br /&gt;
t = setmetatable({}, { __newindex = other })&lt;br /&gt;
t.foo = 3&lt;br /&gt;
other.foo -- 3&lt;br /&gt;
t.foo -- nil&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
As would be expected, if '''__newindex''' is a function, it will be called with the table, key, and value passed as parameters:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
t = setmetatable({}, {&lt;br /&gt;
  __newindex = function(t, key, value)&lt;br /&gt;
    if type(value) == &amp;quot;number&amp;quot; then&lt;br /&gt;
      rawset(t, key, value * value)&lt;br /&gt;
    else&lt;br /&gt;
      rawset(t, key, value)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
})&lt;br /&gt;
&lt;br /&gt;
t.foo = &amp;quot;foo&amp;quot;&lt;br /&gt;
t.bar = 4&lt;br /&gt;
t.la = 10&lt;br /&gt;
t.foo -- &amp;quot;foo&amp;quot;&lt;br /&gt;
t.bar -- 16&lt;br /&gt;
t.la -- 100&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
When creating a new key in '''t''', if the value is a number it will be squared, otherwise it will just be set anyway. This introduces us to our friends, '''rawget''' and '''rawset'''.&lt;br /&gt;
&lt;br /&gt;
=== rawget and rawset ===&lt;br /&gt;
&lt;br /&gt;
There are times when you need get and set a table's keys without having Lua do it's thing with metatables. As you might guess, [http://www.lua.org/manual/5.1/manual.html#pdf-rawget rawget] allows you to get the value of a key without Lua using '''__index''', and [http://www.lua.org/manual/5.1/manual.html#pdf-rawset rawset] allows you to set the value of a key without Lua using '''__newindex''' (no these don't provide a speed increase to conventional way of doing things). You'll need to use these when you would otherwise get stuck in an infinite loop. For example, in that last code example, the code '''t[key] = value * value''' would set off the same '''__newindex''' function again, which would get you stuck in an infinite loop. Using '''rawset(t, key, value * value)''' avoids this.&lt;br /&gt;
&lt;br /&gt;
As you probably can see, to use these functions, for parameters we must pass in the target table, the key, and if you're using '''rawset''', the value.&lt;br /&gt;
&lt;br /&gt;
=== Operators ===&lt;br /&gt;
&lt;br /&gt;
Many of the metatable keys available have to do with operators (as in, '''+''', '''-''', etc.), allowing you to make tables support the use of operators on them. For example, say we wanted a table to support the multiplication operator ('''*'''), this is how we would do it:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
t = setmetatable({ 1, 2, 3 }, {&lt;br /&gt;
  __mul = function(t, other)&lt;br /&gt;
    new = {}&lt;br /&gt;
    &lt;br /&gt;
    for i = 1, other do&lt;br /&gt;
      for _, v in ipairs(t) do table.insert(new, v) end&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    return new&lt;br /&gt;
  end&lt;br /&gt;
})&lt;br /&gt;
&lt;br /&gt;
t = t * 2 -- { 1, 2, 3, 1, 2, 3 }&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This allows us to create a new table with the original replicated a certain amount of times using the multiplication operator. As you can tell, the corresponding key for multiplication is '''__mul'''; unlike '''__index''' and '''__newindex''' the keys for operators can only contain functions. The first parameter these functions always receive is the target table, and then the value on the right hand side (except for the unary - which has the key of '''__unm'''). Here's a list of the supported operators:&lt;br /&gt;
&lt;br /&gt;
* __add: Addition (+)&lt;br /&gt;
* __sub: Subtraction (-)&lt;br /&gt;
* __mul: Multiplication (*)&lt;br /&gt;
* __div: Division (/)&lt;br /&gt;
* __mod: Modulos (%)&lt;br /&gt;
* __unm: Unary -, used for negation on numbers&lt;br /&gt;
* __concat: Concatenation (..)&lt;br /&gt;
* __eq: Equality (==)&lt;br /&gt;
* __lt: Less than (&amp;lt;)&lt;br /&gt;
* __le: Less than or equal to (&amp;lt;=)&lt;br /&gt;
&lt;br /&gt;
(There's only '''==''', '''&amp;lt;''', '''&amp;lt;=''' because you can implement full support for the comparison operators with just those; in fact only '''==''' and '''&amp;lt;''' are needed.)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== __call ===&lt;br /&gt;
&lt;br /&gt;
Next comes the '''__call''' key, which allows you to call tables as functions. A code example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
t = setmetatable({}, {&lt;br /&gt;
  __call = function(t, a, b, c, whatever)&lt;br /&gt;
    return (a + b + c) * whatever&lt;br /&gt;
  end&lt;br /&gt;
})&lt;br /&gt;
&lt;br /&gt;
t(1, 2, 3, 4) -- 24&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The function in call is passed the target table as usual, followed by the parameters that we passed in. '''__call''' is very useful for many things. One common thing it's used for is forwarding a call on a table to a function inside that table.&lt;br /&gt;
&lt;br /&gt;
=== __tostring ===&lt;br /&gt;
&lt;br /&gt;
Last of all is '''__tostring'''. If implemented, it's used by [http://www.lua.org/manual/5.1/manual.html#pdf-tostring tostring] to convert a table into a string, most handy when using a function like [http://www.lua.org/manual/5.1/manual.html#pdf-print print]. Normally, when you try to convert a table to a string, you something in the format of &amp;quot;table: 0x&amp;lt;hex-code-here&amp;gt;&amp;quot;, but you can change that using '''__tostring'''. An example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
t = setmetatable({ 1, 2, 3 }, {&lt;br /&gt;
  __tostring = function(t)&lt;br /&gt;
    sum = 0&lt;br /&gt;
    for _, v in pairs(t) do sum = sum + v end&lt;br /&gt;
    return &amp;quot;Sum: &amp;quot; .. sum&lt;br /&gt;
  end&lt;br /&gt;
})&lt;br /&gt;
&lt;br /&gt;
print(t) -- prints out &amp;quot;Sum: 6&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Advanced examples ==&lt;br /&gt;
Overriding self's value:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
local array = {text = &amp;quot;none&amp;quot;}&lt;br /&gt;
local array2 = {text = &amp;quot;none2&amp;quot;}&lt;br /&gt;
function array:setKey (key, value)&lt;br /&gt;
	self[key] = value&lt;br /&gt;
end&lt;br /&gt;
function array:getKey (key)&lt;br /&gt;
	return self[key]&lt;br /&gt;
end&lt;br /&gt;
array.getKey(array2, &amp;quot;text&amp;quot;) -- returns &amp;quot;none2&amp;quot;&lt;br /&gt;
array.setKey(array2, &amp;quot;text&amp;quot;, &amp;quot;something2&amp;quot;) -- sets array2's &amp;quot;text&amp;quot; value to 'something2'&lt;br /&gt;
array.getKey(array2, &amp;quot;text&amp;quot;) -- returns &amp;quot;something&amp;quot;&lt;br /&gt;
array:getKey(&amp;quot;text&amp;quot;) -- returns &amp;quot;none&amp;quot;&lt;br /&gt;
array.getKey(array, &amp;quot;text&amp;quot;) -- same as above&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A simple backpacks example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
local backpack = {list = {}}&lt;br /&gt;
function backpack:create (owner, slots)&lt;br /&gt;
	if self.list[owner] then return end -- return false in case this player already owns a backpack&lt;br /&gt;
		local new = {&lt;br /&gt;
		items = {},&lt;br /&gt;
		slots = slots or 100,&lt;br /&gt;
		owner = owner&lt;br /&gt;
	}&lt;br /&gt;
	setmetatable(new.items, &lt;br /&gt;
	{&lt;br /&gt;
		__newindex = function(table, key, value) -- this is called once a new value/item is added into the player's table/backpack&lt;br /&gt;
			if #table &amp;gt;= new.slots then return end -- return false in case there isn't any free slot left&lt;br /&gt;
			return rawset(table, key, value)&lt;br /&gt;
		end&lt;br /&gt;
	}&lt;br /&gt;
	)&lt;br /&gt;
	self.list[owner] = new&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function backpack:addItem (player, item, value)&lt;br /&gt;
	if not self.list[player] then return end&lt;br /&gt;
	self.list[player][item] = value&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Novo</name></author>
	</entry>
	<entry>
		<id>https://wiki.multitheftauto.com/index.php?title=OOP_in_Lua&amp;diff=44003</id>
		<title>OOP in Lua</title>
		<link rel="alternate" type="text/html" href="https://wiki.multitheftauto.com/index.php?title=OOP_in_Lua&amp;diff=44003"/>
		<updated>2015-01-18T03:17:56Z</updated>

		<summary type="html">&lt;p&gt;Novo: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;pageclass subcaption=&amp;quot;Adam James @adam.mta&amp;quot;&amp;gt;&amp;lt;/pageclass&amp;gt;&lt;br /&gt;
{{Note_box|Any further questions can be discussed at the [http://google.com topic] dedicated to this tutorial.}}&lt;br /&gt;
__TOC__&lt;br /&gt;
== Introduction and other related pages ==&lt;br /&gt;
This is a scripting tutorial teaches you the basics about how to start using an Object-Oriented developing interface with Lua.&lt;br /&gt;
* [[OOP_Introduction|OOP Introduction - MTA-related]] ''by qaisjp3''&lt;br /&gt;
* [[OOP|OOP]]&lt;br /&gt;
* [[OOP_client|MTA built-in client-sided OO functions]]&lt;br /&gt;
* [[OOP_server|MTA built-in server-sided OO functions]]&lt;br /&gt;
&lt;br /&gt;
== Glossary ==&lt;br /&gt;
* environment: either a table or an array containing values.&lt;br /&gt;
* self: predefined variable referring to the environment within which we are executing the code.&lt;br /&gt;
&lt;br /&gt;
== Initialising ==&lt;br /&gt;
There is a basic and simple predefined variables we should recognize and know: '''[[#Glossary|self]]'''.&lt;br /&gt;
=== Our first environment ===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
local array = {}&lt;br /&gt;
function array:example (argument)&lt;br /&gt;
	return &amp;quot;Hello&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
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:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
array:example()&lt;br /&gt;
array.example(array, example)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
As Lua is so cool, we're able to call a function using two methods: &amp;quot;''':'''&amp;quot; and &amp;quot;'''.'''&amp;quot;. 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|advanced examples]]).&lt;br /&gt;
&lt;br /&gt;
=== Variables and further handling ===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
local array = {text = &amp;quot;none&amp;quot;}&lt;br /&gt;
function array:setKey (key, value)&lt;br /&gt;
	self[key] = value&lt;br /&gt;
end&lt;br /&gt;
function array:getKey (key)&lt;br /&gt;
	return self[key]&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
array:getKey(&amp;quot;text&amp;quot;) -- returns &amp;quot;none&amp;quot;&lt;br /&gt;
array:setKey(&amp;quot;text&amp;quot;, &amp;quot;something&amp;quot;) -- sets &amp;quot;text&amp;quot;'s value to 'something'&lt;br /&gt;
array:getKey(&amp;quot;text&amp;quot;) -- returns &amp;quot;something&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
== Metatables ==&lt;br /&gt;
Content's provenance: [http://nova-fusion.com/2011/06/30/lua-metatables-tutorial/ NovaFusion]&lt;br /&gt;
----&lt;br /&gt;
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.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
t = {} -- our normal table&lt;br /&gt;
mt = {} -- our metatable, which contains nothing right now&lt;br /&gt;
setmetatable(t, mt) -- sets mt to be t's metatable&lt;br /&gt;
getmetatable(t) -- this will return mt&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
t = setmetatable({}, {})&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
'''setmetatable''' returns its first argument, therefore we can use this shorter form.&lt;br /&gt;
&lt;br /&gt;
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:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
t = setmetatable({}, {&lt;br /&gt;
  __index = function(t, key)&lt;br /&gt;
    if key == &amp;quot;foo&amp;quot; then&lt;br /&gt;
      return 0&lt;br /&gt;
    else&lt;br /&gt;
      return table[key]&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
})&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
=== __index ===&lt;br /&gt;
&lt;br /&gt;
The most used metatable key is most likely '''__index'''; it can contain either a function or table.&lt;br /&gt;
&lt;br /&gt;
When you look up a table with a key, regardless of what the key is (t[4], t.foo, and t[&amp;quot;foo&amp;quot;], 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:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
other = { foo = 3 }&lt;br /&gt;
t = setmetatable({}, { __index = other })&lt;br /&gt;
t.foo -- 3&lt;br /&gt;
t.bar -- nil&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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 &amp;quot;foo&amp;quot; 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 &amp;quot;foo&amp;quot; is used.&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
=== __newindex ===&lt;br /&gt;
&lt;br /&gt;
Next in line is '''__newindex''', which is similar to '''__index'''. Like '''__index''', it can contain either a function or table.&lt;br /&gt;
&lt;br /&gt;
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:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Advanced examples ==&lt;br /&gt;
Overriding self's value:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
local array = {text = &amp;quot;none&amp;quot;}&lt;br /&gt;
local array2 = {text = &amp;quot;none2&amp;quot;}&lt;br /&gt;
function array:setKey (key, value)&lt;br /&gt;
	self[key] = value&lt;br /&gt;
end&lt;br /&gt;
function array:getKey (key)&lt;br /&gt;
	return self[key]&lt;br /&gt;
end&lt;br /&gt;
array.getKey(array2, &amp;quot;text&amp;quot;) -- returns &amp;quot;none2&amp;quot;&lt;br /&gt;
array.setKey(array2, &amp;quot;text&amp;quot;, &amp;quot;something2&amp;quot;) -- sets array2's &amp;quot;text&amp;quot; value to 'something2'&lt;br /&gt;
array.getKey(array2, &amp;quot;text&amp;quot;) -- returns &amp;quot;something&amp;quot;&lt;br /&gt;
array:getKey(&amp;quot;text&amp;quot;) -- returns &amp;quot;none&amp;quot;&lt;br /&gt;
array.getKey(array, &amp;quot;text&amp;quot;) -- same as above&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A simple backpacks example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
local backpack = {list = {}}&lt;br /&gt;
function backpack:create (owner, slots)&lt;br /&gt;
	if self.list[owner] then return end -- return false in case this player already owns a backpack&lt;br /&gt;
		local new = {&lt;br /&gt;
		items = {},&lt;br /&gt;
		slots = slots or 100,&lt;br /&gt;
		owner = owner&lt;br /&gt;
	}&lt;br /&gt;
	setmetatable(new.items, &lt;br /&gt;
	{&lt;br /&gt;
		__newindex = function(table, key, value) -- this is called once a new value/item is added into the player's table/backpack&lt;br /&gt;
			if #table &amp;gt;= new.slots then return end -- return false in case there isn't any free slot left&lt;br /&gt;
			return rawset(table, key, value)&lt;br /&gt;
		end&lt;br /&gt;
	}&lt;br /&gt;
	)&lt;br /&gt;
	self.list[owner] = new&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function backpack:addItem (player, item, value)&lt;br /&gt;
	if not self.list[player] then return end&lt;br /&gt;
	self.list[player][item] = value&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Novo</name></author>
	</entry>
	<entry>
		<id>https://wiki.multitheftauto.com/index.php?title=OOP_in_Lua&amp;diff=44002</id>
		<title>OOP in Lua</title>
		<link rel="alternate" type="text/html" href="https://wiki.multitheftauto.com/index.php?title=OOP_in_Lua&amp;diff=44002"/>
		<updated>2015-01-18T03:09:56Z</updated>

		<summary type="html">&lt;p&gt;Novo: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;pageclass subcaption=&amp;quot;Adam James @adam.mta&amp;quot;&amp;gt;&amp;lt;/pageclass&amp;gt;&lt;br /&gt;
{{Note_box|Any further questions can be discussed at the [http://google.com topic] dedicated to this tutorial.}}&lt;br /&gt;
__TOC__&lt;br /&gt;
== Introduction and other related pages ==&lt;br /&gt;
This is a scripting tutorial teaches you the basics about how to start using an Object-Oriented developing interface with Lua.&lt;br /&gt;
* [[OOP_Introduction|OOP Introduction - MTA-related]] ''by qaisjp3''&lt;br /&gt;
* [[OOP|OOP]]&lt;br /&gt;
* [[OOP_client|MTA built-in client-sided OO functions]]&lt;br /&gt;
* [[OOP_server|MTA built-in server-sided OO functions]]&lt;br /&gt;
&lt;br /&gt;
== Glossary ==&lt;br /&gt;
* environment: either a table or an array containing values.&lt;br /&gt;
* self: predefined variable referring to the environment within which we are executing the code.&lt;br /&gt;
&lt;br /&gt;
== Initialising ==&lt;br /&gt;
There is a basic and simple predefined variables we should recognize and know: '''[[#Glossary|self]]'''.&lt;br /&gt;
=== Our first environment ===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
local array = {}&lt;br /&gt;
function array:example (argument)&lt;br /&gt;
	return &amp;quot;Hello&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
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:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
array:example()&lt;br /&gt;
array.example(array, example)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
As Lua is so cool, we're able to call a function using two methods: &amp;quot;''':'''&amp;quot; and &amp;quot;'''.'''&amp;quot;. 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|advanced examples]]).&lt;br /&gt;
&lt;br /&gt;
=== Variables and further handling ===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
local array = {text = &amp;quot;none&amp;quot;}&lt;br /&gt;
function array:setKey (key, value)&lt;br /&gt;
	self[key] = value&lt;br /&gt;
end&lt;br /&gt;
function array:getKey (key)&lt;br /&gt;
	return self[key]&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
array:getKey(&amp;quot;text&amp;quot;) -- returns &amp;quot;none&amp;quot;&lt;br /&gt;
array:setKey(&amp;quot;text&amp;quot;, &amp;quot;something&amp;quot;) -- sets &amp;quot;text&amp;quot;'s value to 'something'&lt;br /&gt;
array:getKey(&amp;quot;text&amp;quot;) -- returns &amp;quot;something&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
== Metatables ==&lt;br /&gt;
[http://nova-fusion.com/2011/06/30/lua-metatables-tutorial/ Source]&lt;br /&gt;
&lt;br /&gt;
== Advanced examples ==&lt;br /&gt;
Overriding self's value:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
local array = {text = &amp;quot;none&amp;quot;}&lt;br /&gt;
local array2 = {text = &amp;quot;none2&amp;quot;}&lt;br /&gt;
function array:setKey (key, value)&lt;br /&gt;
	self[key] = value&lt;br /&gt;
end&lt;br /&gt;
function array:getKey (key)&lt;br /&gt;
	return self[key]&lt;br /&gt;
end&lt;br /&gt;
array.getKey(array2, &amp;quot;text&amp;quot;) -- returns &amp;quot;none2&amp;quot;&lt;br /&gt;
array.setKey(array2, &amp;quot;text&amp;quot;, &amp;quot;something2&amp;quot;) -- sets array2's &amp;quot;text&amp;quot; value to 'something2'&lt;br /&gt;
array.getKey(array2, &amp;quot;text&amp;quot;) -- returns &amp;quot;something&amp;quot;&lt;br /&gt;
array:getKey(&amp;quot;text&amp;quot;) -- returns &amp;quot;none&amp;quot;&lt;br /&gt;
array.getKey(array, &amp;quot;text&amp;quot;) -- same as above&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A simple backpacks example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
local backpack = {list = {}}&lt;br /&gt;
function backpack:create (owner, slots)&lt;br /&gt;
	if self.list[owner] then return end -- return false in case this player already owns a backpack&lt;br /&gt;
		local new = {&lt;br /&gt;
		items = {},&lt;br /&gt;
		slots = slots or 100,&lt;br /&gt;
		owner = owner&lt;br /&gt;
	}&lt;br /&gt;
	setmetatable(new.items, &lt;br /&gt;
	{&lt;br /&gt;
		__newindex = function(table, key, value) -- this is called once a new value/item is added into the player's table/backpack&lt;br /&gt;
			if #table &amp;gt;= new.slots then return end -- return false in case there isn't any free slot left&lt;br /&gt;
			return rawset(table, key, value)&lt;br /&gt;
		end&lt;br /&gt;
	}&lt;br /&gt;
	)&lt;br /&gt;
	self.list[owner] = new&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function backpack:addItem (player, item, value)&lt;br /&gt;
	if not self.list[player] then return end&lt;br /&gt;
	self.list[player][item] = value&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Novo</name></author>
	</entry>
	<entry>
		<id>https://wiki.multitheftauto.com/index.php?title=OOP_in_Lua&amp;diff=44001</id>
		<title>OOP in Lua</title>
		<link rel="alternate" type="text/html" href="https://wiki.multitheftauto.com/index.php?title=OOP_in_Lua&amp;diff=44001"/>
		<updated>2015-01-18T03:09:15Z</updated>

		<summary type="html">&lt;p&gt;Novo: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;pageclass subcaption=&amp;quot;Adam James @adam.mta&amp;quot;&amp;gt;&amp;lt;/pageclass&amp;gt;&lt;br /&gt;
{{Note_box|Any further questions can be discussed at the [http://google.com topic] dedicated to this tutorial.}}&lt;br /&gt;
__TOC__&lt;br /&gt;
== Introduction and other related pages ==&lt;br /&gt;
This is a scripting tutorial teaches you the basics about how to start using an Object-Oriented developing interface with Lua.&lt;br /&gt;
* [[OOP_Introduction|OOP Introduction - MTA-related]] ''by qaisjp3''&lt;br /&gt;
* [[OOP|OOP]]&lt;br /&gt;
* [[OOP_client|MTA built-in client-sided OO functions]]&lt;br /&gt;
* [[OOP_server|MTA built-in server-sided OO functions]]&lt;br /&gt;
&lt;br /&gt;
== Glossary ==&lt;br /&gt;
* environment: either a table or an array containing values.&lt;br /&gt;
* self: predefined variable referring to the environment within which we are executing the code.&lt;br /&gt;
&lt;br /&gt;
== Initialising ==&lt;br /&gt;
There is a basic and simple predefined variables we should recognize and know: '''[[#Glossary|self]]'''.&lt;br /&gt;
=== Our first environment ===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
local array = {}&lt;br /&gt;
function array:example (argument)&lt;br /&gt;
	return &amp;quot;Hello&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
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:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
array:example()&lt;br /&gt;
array.example(array, example)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
As Lua is so cool, we're able to call a function using two methods: &amp;quot;''':'''&amp;quot; and &amp;quot;'''.'''&amp;quot;. 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|advanced examples]]).&lt;br /&gt;
&lt;br /&gt;
=== Variables and further handling ===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
local array = {text = &amp;quot;none&amp;quot;}&lt;br /&gt;
function array:setKey (key, value)&lt;br /&gt;
	self[key] = value&lt;br /&gt;
end&lt;br /&gt;
function array:getKey (key)&lt;br /&gt;
	return self[key]&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
array:getKey(&amp;quot;text&amp;quot;) -- returns &amp;quot;none&amp;quot;&lt;br /&gt;
array:setKey(&amp;quot;text&amp;quot;, &amp;quot;something&amp;quot;) -- sets &amp;quot;text&amp;quot;'s value to 'something'&lt;br /&gt;
array:getKey(&amp;quot;text&amp;quot;) -- returns &amp;quot;something&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
== Metatables ==&lt;br /&gt;
Everything you need to know about them is stupendously explained [http://nova-fusion.com/2011/06/30/lua-metatables-tutorial/ here].&lt;br /&gt;
&lt;br /&gt;
== Advanced examples ==&lt;br /&gt;
Overriding self's value:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
local array = {text = &amp;quot;none&amp;quot;}&lt;br /&gt;
local array2 = {text = &amp;quot;none2&amp;quot;}&lt;br /&gt;
function array:setKey (key, value)&lt;br /&gt;
	self[key] = value&lt;br /&gt;
end&lt;br /&gt;
function array:getKey (key)&lt;br /&gt;
	return self[key]&lt;br /&gt;
end&lt;br /&gt;
array.getKey(array2, &amp;quot;text&amp;quot;) -- returns &amp;quot;none2&amp;quot;&lt;br /&gt;
array.setKey(array2, &amp;quot;text&amp;quot;, &amp;quot;something2&amp;quot;) -- sets array2's &amp;quot;text&amp;quot; value to 'something2'&lt;br /&gt;
array.getKey(array2, &amp;quot;text&amp;quot;) -- returns &amp;quot;something&amp;quot;&lt;br /&gt;
array:getKey(&amp;quot;text&amp;quot;) -- returns &amp;quot;none&amp;quot;&lt;br /&gt;
array.getKey(array, &amp;quot;text&amp;quot;) -- same as above&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A simple backpacks example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
local backpack = {list = {}}&lt;br /&gt;
function backpack:create (owner, slots)&lt;br /&gt;
	if self.list[owner] then return end -- return false in case this player already owns a backpack&lt;br /&gt;
		local new = {&lt;br /&gt;
		items = {},&lt;br /&gt;
		slots = slots or 100,&lt;br /&gt;
		owner = owner&lt;br /&gt;
	}&lt;br /&gt;
	setmetatable(new.items, &lt;br /&gt;
	{&lt;br /&gt;
		__newindex = function(table, key, value) -- this is called once a new value/item is added into the player's table/backpack&lt;br /&gt;
			if #table &amp;gt;= new.slots then return end -- return false in case there isn't any free slot left&lt;br /&gt;
			return rawset(table, key, value)&lt;br /&gt;
		end&lt;br /&gt;
	}&lt;br /&gt;
	)&lt;br /&gt;
	self.list[owner] = new&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function backpack:addItem (player, item, value)&lt;br /&gt;
	if not self.list[player] then return end&lt;br /&gt;
	self.list[player][item] = value&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Novo</name></author>
	</entry>
	<entry>
		<id>https://wiki.multitheftauto.com/index.php?title=OOP_in_Lua&amp;diff=44000</id>
		<title>OOP in Lua</title>
		<link rel="alternate" type="text/html" href="https://wiki.multitheftauto.com/index.php?title=OOP_in_Lua&amp;diff=44000"/>
		<updated>2015-01-18T03:06:02Z</updated>

		<summary type="html">&lt;p&gt;Novo: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;pageclass subcaption=&amp;quot;Adam James @adam.mta&amp;quot;&amp;gt;&amp;lt;/pageclass&amp;gt;&lt;br /&gt;
{{Note_box|Any further questions can be discussed at the [http://google.com topic] dedicated to this tutorial.}}&lt;br /&gt;
__TOC__&lt;br /&gt;
== Introduction and other related pages ==&lt;br /&gt;
This is a scripting tutorial teaches you the basics about how to start using an Object-Oriented developing interface with Lua.&lt;br /&gt;
* [[OOP_Introduction|OOP Introduction - MTA-related]] ''by qaisjp3''&lt;br /&gt;
* [[OOP|OOP]]&lt;br /&gt;
* [[OOP_client|MTA built-in client-sided OO functions]]&lt;br /&gt;
* [[OOP_server|MTA built-in server-sided OO functions]]&lt;br /&gt;
&lt;br /&gt;
== Glossary ==&lt;br /&gt;
* environment: either a table or an array containing values.&lt;br /&gt;
* self: predefined variable referring to the environment within which we are executing the code.&lt;br /&gt;
&lt;br /&gt;
== Initialising ==&lt;br /&gt;
There is a basic and simple predefined variables we should recognize and know: '''[[#Glossary|self]]'''.&lt;br /&gt;
=== Our first environment ===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
local array = {}&lt;br /&gt;
function array:example (argument)&lt;br /&gt;
	return &amp;quot;Hello&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
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:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
array:example()&lt;br /&gt;
array.example(array, example)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
As Lua is so cool, we're able to call a function using two methods: &amp;quot;''':'''&amp;quot; and &amp;quot;'''.'''&amp;quot;. 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|advanced examples]]).&lt;br /&gt;
&lt;br /&gt;
=== Variables and further handling ===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
local array = {text = &amp;quot;none&amp;quot;}&lt;br /&gt;
function array:setKey (key, value)&lt;br /&gt;
	self[key] = value&lt;br /&gt;
end&lt;br /&gt;
function array:getKey (key)&lt;br /&gt;
	return self[key]&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
array:getKey(&amp;quot;text&amp;quot;) -- returns &amp;quot;none&amp;quot;&lt;br /&gt;
array:setKey(&amp;quot;text&amp;quot;, &amp;quot;something&amp;quot;) -- sets &amp;quot;text&amp;quot;'s value to 'something'&lt;br /&gt;
array:getKey(&amp;quot;text&amp;quot;) -- returns &amp;quot;something&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
== Metatables ==&lt;br /&gt;
Everything you need to know about them is stupendously explained [http://nova-fusion.com/2011/06/30/lua-metatables-tutorial/ here].&lt;br /&gt;
&lt;br /&gt;
== Advanced examples ==&lt;br /&gt;
Overriding self's value:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
local array = {text = &amp;quot;none&amp;quot;}&lt;br /&gt;
local array2 = {text = &amp;quot;none2&amp;quot;}&lt;br /&gt;
function array:setKey (key, value)&lt;br /&gt;
	self[key] = value&lt;br /&gt;
end&lt;br /&gt;
function array:getKey (key)&lt;br /&gt;
	return self[key]&lt;br /&gt;
end&lt;br /&gt;
array.getKey(array2, &amp;quot;text&amp;quot;) -- returns &amp;quot;none2&amp;quot;&lt;br /&gt;
array.setKey(array2, &amp;quot;text&amp;quot;, &amp;quot;something2&amp;quot;) -- sets array2's &amp;quot;text&amp;quot; value to 'something2'&lt;br /&gt;
array.getKey(array2, &amp;quot;text&amp;quot;) -- returns &amp;quot;something&amp;quot;&lt;br /&gt;
array:getKey(&amp;quot;text&amp;quot;) -- returns &amp;quot;none&amp;quot;&lt;br /&gt;
array.getKey(array, &amp;quot;text&amp;quot;) -- same as above&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A simple backpacks example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
local backpack = {list = {}}&lt;br /&gt;
function backpack:create (owner, slots)&lt;br /&gt;
	if self.list[owner] then return end -- return false in case this player already owns a backpack&lt;br /&gt;
		local new = {&lt;br /&gt;
		items = {},&lt;br /&gt;
		slots = slots or 100,&lt;br /&gt;
		owner = owner&lt;br /&gt;
	}&lt;br /&gt;
	setmetatable(new.items, &lt;br /&gt;
	{&lt;br /&gt;
		__newindex = function(table, key, value) -- this is called once a new value/item is added into the player's table/backpack&lt;br /&gt;
			if #table &amp;gt;= new.slots then return end -- return false in case there isn't any free slot left&lt;br /&gt;
			return rawset(table, key, value)&lt;br /&gt;
		end&lt;br /&gt;
	}&lt;br /&gt;
	)&lt;br /&gt;
	self.list[owner] = new&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function backpack:addItem (player, item, value)&lt;br /&gt;
	if not self.list[player] then return end&lt;br /&gt;
	self.list[player][item] = value&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
* [http://nova-fusion.com/2011/06/30/lua-metatables-tutorial/ Metatables tutorial].&lt;br /&gt;
* [[OOP|MTA's built-in OO-system]].&lt;br /&gt;
* [[OOP_client|MTA's built-in OO-functions]].&lt;/div&gt;</summary>
		<author><name>Novo</name></author>
	</entry>
	<entry>
		<id>https://wiki.multitheftauto.com/index.php?title=OOP_in_Lua&amp;diff=43999</id>
		<title>OOP in Lua</title>
		<link rel="alternate" type="text/html" href="https://wiki.multitheftauto.com/index.php?title=OOP_in_Lua&amp;diff=43999"/>
		<updated>2015-01-18T03:00:52Z</updated>

		<summary type="html">&lt;p&gt;Novo: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;pageclass subcaption=&amp;quot;Adam James @adam.mta&amp;quot;&amp;gt;&amp;lt;/pageclass&amp;gt;&lt;br /&gt;
{{Note_box|Any further questions can be discussed at the [http://google.com topic] dedicated to this tutorial.}}&lt;br /&gt;
__TOC__&lt;br /&gt;
== Introduction and other related tutorials ==&lt;br /&gt;
This is a scripting tutorial teaches you the basics about how to start using an Object-Oriented developing interface with Lua.&lt;br /&gt;
&lt;br /&gt;
== Glossary ==&lt;br /&gt;
* environment: either a table or an array containing values.&lt;br /&gt;
* self: predefined variable referring to the environment within which we are executing the code.&lt;br /&gt;
&lt;br /&gt;
== Initialising ==&lt;br /&gt;
There is a basic and simple predefined variables we should recognize and know: '''[[#Glossary|self]]'''.&lt;br /&gt;
=== Our first environment ===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
local array = {}&lt;br /&gt;
function array:example (argument)&lt;br /&gt;
	return &amp;quot;Hello&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
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:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
array:example()&lt;br /&gt;
array.example(array, example)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
As Lua is so cool, we're able to call a function using two methods: &amp;quot;''':'''&amp;quot; and &amp;quot;'''.'''&amp;quot;. 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|advanced examples]]).&lt;br /&gt;
&lt;br /&gt;
=== Variables and further handling ===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
local array = {text = &amp;quot;none&amp;quot;}&lt;br /&gt;
function array:setKey (key, value)&lt;br /&gt;
	self[key] = value&lt;br /&gt;
end&lt;br /&gt;
function array:getKey (key)&lt;br /&gt;
	return self[key]&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
array:getKey(&amp;quot;text&amp;quot;) -- returns &amp;quot;none&amp;quot;&lt;br /&gt;
array:setKey(&amp;quot;text&amp;quot;, &amp;quot;something&amp;quot;) -- sets &amp;quot;text&amp;quot;'s value to 'something'&lt;br /&gt;
array:getKey(&amp;quot;text&amp;quot;) -- returns &amp;quot;something&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
== Metatables ==&lt;br /&gt;
Everything you need to know about them is stupendously explained [http://nova-fusion.com/2011/06/30/lua-metatables-tutorial/ here].&lt;br /&gt;
&lt;br /&gt;
== Advanced examples ==&lt;br /&gt;
Overriding self's value:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
local array = {text = &amp;quot;none&amp;quot;}&lt;br /&gt;
local array2 = {text = &amp;quot;none2&amp;quot;}&lt;br /&gt;
function array:setKey (key, value)&lt;br /&gt;
	self[key] = value&lt;br /&gt;
end&lt;br /&gt;
function array:getKey (key)&lt;br /&gt;
	return self[key]&lt;br /&gt;
end&lt;br /&gt;
array.getKey(array2, &amp;quot;text&amp;quot;) -- returns &amp;quot;none2&amp;quot;&lt;br /&gt;
array.setKey(array2, &amp;quot;text&amp;quot;, &amp;quot;something2&amp;quot;) -- sets array2's &amp;quot;text&amp;quot; value to 'something2'&lt;br /&gt;
array.getKey(array2, &amp;quot;text&amp;quot;) -- returns &amp;quot;something&amp;quot;&lt;br /&gt;
array:getKey(&amp;quot;text&amp;quot;) -- returns &amp;quot;none&amp;quot;&lt;br /&gt;
array.getKey(array, &amp;quot;text&amp;quot;) -- same as above&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A simple backpacks example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
local backpack = {list = {}}&lt;br /&gt;
function backpack:create (owner, slots)&lt;br /&gt;
	if self.list[owner] then return end -- return false in case this player already owns a backpack&lt;br /&gt;
		local new = {&lt;br /&gt;
		items = {},&lt;br /&gt;
		slots = slots or 100,&lt;br /&gt;
		owner = owner&lt;br /&gt;
	}&lt;br /&gt;
	setmetatable(new.items, &lt;br /&gt;
	{&lt;br /&gt;
		__newindex = function(table, key, value) -- this is called once a new value/item is added into the player's table/backpack&lt;br /&gt;
			if #table &amp;gt;= new.slots then return end -- return false in case there isn't any free slot left&lt;br /&gt;
			return rawset(table, key, value)&lt;br /&gt;
		end&lt;br /&gt;
	}&lt;br /&gt;
	)&lt;br /&gt;
	self.list[owner] = new&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function backpack:addItem (player, item, value)&lt;br /&gt;
	if not self.list[player] then return end&lt;br /&gt;
	self.list[player][item] = value&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
* [http://nova-fusion.com/2011/06/30/lua-metatables-tutorial/ Metatables tutorial].&lt;br /&gt;
* [[OOP|MTA's built-in OO-system]].&lt;br /&gt;
* [[OOP_client|MTA's built-in OO-functions]].&lt;/div&gt;</summary>
		<author><name>Novo</name></author>
	</entry>
	<entry>
		<id>https://wiki.multitheftauto.com/index.php?title=OOP_in_Lua&amp;diff=43996</id>
		<title>OOP in Lua</title>
		<link rel="alternate" type="text/html" href="https://wiki.multitheftauto.com/index.php?title=OOP_in_Lua&amp;diff=43996"/>
		<updated>2015-01-18T02:44:04Z</updated>

		<summary type="html">&lt;p&gt;Novo: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;pageclass subcaption=&amp;quot;Adam James @adam.mta&amp;quot;&amp;gt;&amp;lt;/pageclass&amp;gt;&lt;br /&gt;
{{Note_box|&lt;br /&gt;
Any further questions can be discussed at the [http://google.com topic] dedicated to this tutorial.}}&lt;br /&gt;
&lt;br /&gt;
This is a scripting tutorial that teaches you how to start using an Object-Oriented developing interface with Lua.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
== Glossary ==&lt;br /&gt;
* environment: either a table or an array containing values.&lt;br /&gt;
* self: predefined variable referring to the environment within which we are executing the code.&lt;br /&gt;
&lt;br /&gt;
== Initialising ==&lt;br /&gt;
There is a basic and simple predefined variables we should recognize and know: '''[[#Glossary|self]]'''.&lt;br /&gt;
=== Our first environment ===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
local array = {}&lt;br /&gt;
function array:example (argument)&lt;br /&gt;
	return &amp;quot;Hello&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
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:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
array:example()&lt;br /&gt;
array.example(array, example)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
As Lua is so cool, we're able to call a function using two methods: &amp;quot;''':'''&amp;quot; and &amp;quot;'''.'''&amp;quot;. 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|advanced examples]]).&lt;br /&gt;
&lt;br /&gt;
=== Variables and further handling ===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
local array = {text = &amp;quot;none&amp;quot;}&lt;br /&gt;
function array:setKey (key, value)&lt;br /&gt;
	self[key] = value&lt;br /&gt;
end&lt;br /&gt;
function array:getKey (key)&lt;br /&gt;
	return self[key]&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
array:getKey(&amp;quot;text&amp;quot;) -- returns &amp;quot;none&amp;quot;&lt;br /&gt;
array:setKey(&amp;quot;text&amp;quot;, &amp;quot;something&amp;quot;) -- sets &amp;quot;text&amp;quot;'s value to 'something'&lt;br /&gt;
array:getKey(&amp;quot;text&amp;quot;) -- returns &amp;quot;something&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
== Metatables ==&lt;br /&gt;
Everything you need to know about them is stupendously explained [http://nova-fusion.com/2011/06/30/lua-metatables-tutorial/ here].&lt;br /&gt;
&lt;br /&gt;
== Advanced examples ==&lt;br /&gt;
Overriding self's value:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
local array = {text = &amp;quot;none&amp;quot;}&lt;br /&gt;
local array2 = {text = &amp;quot;none2&amp;quot;}&lt;br /&gt;
function array:setKey (key, value)&lt;br /&gt;
	self[key] = value&lt;br /&gt;
end&lt;br /&gt;
function array:getKey (key)&lt;br /&gt;
	return self[key]&lt;br /&gt;
end&lt;br /&gt;
array.getKey(array2, &amp;quot;text&amp;quot;) -- returns &amp;quot;none2&amp;quot;&lt;br /&gt;
array.setKey(array2, &amp;quot;text&amp;quot;, &amp;quot;something2&amp;quot;) -- sets array2's &amp;quot;text&amp;quot; value to 'something2'&lt;br /&gt;
array.getKey(array2, &amp;quot;text&amp;quot;) -- returns &amp;quot;something&amp;quot;&lt;br /&gt;
array:getKey(&amp;quot;text&amp;quot;) -- returns &amp;quot;none&amp;quot;&lt;br /&gt;
array.getKey(array, &amp;quot;text&amp;quot;) -- same as above&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A simple backpacks example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
local backpack = {list = {}}&lt;br /&gt;
function backpack:create (owner, slots)&lt;br /&gt;
	if self.list[owner] then return end -- return false in case this player already owns a backpack&lt;br /&gt;
		local new = {&lt;br /&gt;
		items = {},&lt;br /&gt;
		slots = slots or 100,&lt;br /&gt;
		owner = owner&lt;br /&gt;
	}&lt;br /&gt;
	setmetatable(new.items, &lt;br /&gt;
	{&lt;br /&gt;
		__newindex = function(table, key, value) -- this is called once a new value/item is added into the player's table/backpack&lt;br /&gt;
			if #table &amp;gt;= new.slots then return end -- return false in case there isn't any free slot left&lt;br /&gt;
			return rawset(table, key, value)&lt;br /&gt;
		end&lt;br /&gt;
	}&lt;br /&gt;
	)&lt;br /&gt;
	self.list[owner] = new&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function backpack:addItem (player, item, value)&lt;br /&gt;
	if not self.list[player] then return end&lt;br /&gt;
	self.list[player][item] = value&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
* [http://nova-fusion.com/2011/06/30/lua-metatables-tutorial/ Metatables tutorial].&lt;br /&gt;
* [[OOP|MTA's built-in OO-system]].&lt;br /&gt;
* [[OOP_client|MTA's built-in OO-functions]].&lt;/div&gt;</summary>
		<author><name>Novo</name></author>
	</entry>
	<entry>
		<id>https://wiki.multitheftauto.com/index.php?title=OOP_in_Lua&amp;diff=43995</id>
		<title>OOP in Lua</title>
		<link rel="alternate" type="text/html" href="https://wiki.multitheftauto.com/index.php?title=OOP_in_Lua&amp;diff=43995"/>
		<updated>2015-01-18T02:42:01Z</updated>

		<summary type="html">&lt;p&gt;Novo: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;pageclass subcaption=&amp;quot;Adam James @adam.mta&amp;quot;&amp;gt;&amp;lt;/pageclass&amp;gt;&lt;br /&gt;
{{Note_box|&lt;br /&gt;
Any further questions can be discussed at the [http://google.com topic] dedicated to this tutorial.}}&lt;br /&gt;
&lt;br /&gt;
This is a scripting tutorial that teaches you how to start using an Object-Oriented developing interface with Lua.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
== Glossary ==&lt;br /&gt;
* environment: either a table or an array containing values.&lt;br /&gt;
&lt;br /&gt;
== Initialising ==&lt;br /&gt;
There is a basic and simple predefined variables we should recognize and know: '''self'''. Which refers to the environment within which we are executing a code.&lt;br /&gt;
=== Our first environment ===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
local array = {}&lt;br /&gt;
function array:example (argument)&lt;br /&gt;
	return &amp;quot;Hello&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
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:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
array:example()&lt;br /&gt;
array.example(array, example)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
As Lua is so cool, we're able to call a function using two methods: &amp;quot;''':'''&amp;quot; and &amp;quot;'''.'''&amp;quot;. 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|advanced examples]]).&lt;br /&gt;
&lt;br /&gt;
=== Variables and further handling ===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
local array = {text = &amp;quot;none&amp;quot;}&lt;br /&gt;
function array:setKey (key, value)&lt;br /&gt;
	self[key] = value&lt;br /&gt;
end&lt;br /&gt;
function array:getKey (key)&lt;br /&gt;
	return self[key]&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
array:getKey(&amp;quot;text&amp;quot;) -- returns &amp;quot;none&amp;quot;&lt;br /&gt;
array:setKey(&amp;quot;text&amp;quot;, &amp;quot;something&amp;quot;) -- sets &amp;quot;text&amp;quot;'s value to 'something'&lt;br /&gt;
array:getKey(&amp;quot;text&amp;quot;) -- returns &amp;quot;something&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
== Metatables ==&lt;br /&gt;
Everything you need to know about them is stupendously explained [http://nova-fusion.com/2011/06/30/lua-metatables-tutorial/ here].&lt;br /&gt;
&lt;br /&gt;
== Advanced examples ==&lt;br /&gt;
Overriding self's value:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
local array = {text = &amp;quot;none&amp;quot;}&lt;br /&gt;
local array2 = {text = &amp;quot;none2&amp;quot;}&lt;br /&gt;
function array:setKey (key, value)&lt;br /&gt;
	self[key] = value&lt;br /&gt;
end&lt;br /&gt;
function array:getKey (key)&lt;br /&gt;
	return self[key]&lt;br /&gt;
end&lt;br /&gt;
array.getKey(array2, &amp;quot;text&amp;quot;) -- returns &amp;quot;none2&amp;quot;&lt;br /&gt;
array.setKey(array2, &amp;quot;text&amp;quot;, &amp;quot;something2&amp;quot;) -- sets array2's &amp;quot;text&amp;quot; value to 'something2'&lt;br /&gt;
array.getKey(array2, &amp;quot;text&amp;quot;) -- returns &amp;quot;something&amp;quot;&lt;br /&gt;
array:getKey(&amp;quot;text&amp;quot;) -- returns &amp;quot;none&amp;quot;&lt;br /&gt;
array.getKey(array, &amp;quot;text&amp;quot;) -- same as above&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A simple backpacks example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
local backpack = {list = {}}&lt;br /&gt;
function backpack:create (owner, slots)&lt;br /&gt;
	if self.list[owner] then return end -- return false in case this player already owns a backpack&lt;br /&gt;
		local new = {&lt;br /&gt;
		items = {},&lt;br /&gt;
		slots = slots or 100,&lt;br /&gt;
		owner = owner&lt;br /&gt;
	}&lt;br /&gt;
	setmetatable(new.items, &lt;br /&gt;
	{&lt;br /&gt;
		__newindex = function(table, key, value) -- this is called once a new value/item is added into the player's table/backpack&lt;br /&gt;
			if #table &amp;gt;= new.slots then return end -- return false in case there isn't any free slot left&lt;br /&gt;
			return rawset(table, key, value)&lt;br /&gt;
		end&lt;br /&gt;
	}&lt;br /&gt;
	)&lt;br /&gt;
	self.list[owner] = new&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function backpack:addItem (player, item, value)&lt;br /&gt;
	if not self.list[player] then return end&lt;br /&gt;
	self.list[player][item] = value&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
* [http://nova-fusion.com/2011/06/30/lua-metatables-tutorial/ Metatables tutorial].&lt;br /&gt;
* [[OOP|MTA's built-in OO-system]].&lt;br /&gt;
* [[OOP_client|MTA's built-in OO-functions]].&lt;/div&gt;</summary>
		<author><name>Novo</name></author>
	</entry>
	<entry>
		<id>https://wiki.multitheftauto.com/index.php?title=OOP_in_Lua&amp;diff=43994</id>
		<title>OOP in Lua</title>
		<link rel="alternate" type="text/html" href="https://wiki.multitheftauto.com/index.php?title=OOP_in_Lua&amp;diff=43994"/>
		<updated>2015-01-18T02:29:04Z</updated>

		<summary type="html">&lt;p&gt;Novo: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;pageclass subcaption=&amp;quot;Adam James @adam.mta&amp;quot;&amp;gt;&amp;lt;/pageclass&amp;gt;&lt;br /&gt;
{{Note_box|&lt;br /&gt;
Any further questions can be discussed at the [http://google.com topic] dedicated to this tutorial.}}&lt;br /&gt;
&lt;br /&gt;
This is a scripting tutorial that teaches you how to start using an Object-Oriented developing interface with Lua.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
== Glossary ==&lt;br /&gt;
* environment: either a table or an array containing values.&lt;br /&gt;
&lt;br /&gt;
== Initialising ==&lt;br /&gt;
There is a basic and simple predefined variables we should recognize and know: '''self'''. Which refers to the environment within which we are executing a code.&lt;br /&gt;
=== Our first environment ===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
local array = {}&lt;br /&gt;
function array:example (argument)&lt;br /&gt;
	return &amp;quot;Hello&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
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:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
array:example()&lt;br /&gt;
array.example(array, example)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
As Lua is so cool, we're able to call a function using two methods: &amp;quot;''':'''&amp;quot; and &amp;quot;'''.'''&amp;quot;. 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|advanced examples]]).&lt;br /&gt;
&lt;br /&gt;
=== Variables and further handling ===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
local array = {text = &amp;quot;none&amp;quot;}&lt;br /&gt;
function array:setKey (key, value)&lt;br /&gt;
	self[key] = value&lt;br /&gt;
end&lt;br /&gt;
function array:getKey (key)&lt;br /&gt;
	return self[key]&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
array:getKey(&amp;quot;text&amp;quot;) -- returns &amp;quot;none&amp;quot;&lt;br /&gt;
array:setKey(&amp;quot;text&amp;quot;, &amp;quot;something&amp;quot;) -- sets &amp;quot;text&amp;quot;'s value to 'something'&lt;br /&gt;
array:getKey(&amp;quot;text&amp;quot;) -- returns &amp;quot;something&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
== Metatables ==&lt;br /&gt;
Everything you need to know about them is stupendously explained [http://nova-fusion.com/2011/06/30/lua-metatables-tutorial/ here].&lt;br /&gt;
&lt;br /&gt;
== Advanced examples ==&lt;br /&gt;
Overriding self's value:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
local array = {text = &amp;quot;none&amp;quot;}&lt;br /&gt;
local array2 = {text = &amp;quot;none2&amp;quot;}&lt;br /&gt;
function array:setKey (key, value)&lt;br /&gt;
	self[key] = value&lt;br /&gt;
end&lt;br /&gt;
function array:getKey (key)&lt;br /&gt;
	return self[key]&lt;br /&gt;
end&lt;br /&gt;
array.getKey(array2, &amp;quot;text&amp;quot;) -- returns &amp;quot;none2&amp;quot;&lt;br /&gt;
array.setKey(array2, &amp;quot;text&amp;quot;, &amp;quot;something2&amp;quot;) -- sets array2's &amp;quot;text&amp;quot; value to 'something2'&lt;br /&gt;
array.getKey(array2, &amp;quot;text&amp;quot;) -- returns &amp;quot;something&amp;quot;&lt;br /&gt;
array:getKey(&amp;quot;text&amp;quot;) -- returns &amp;quot;none&amp;quot;&lt;br /&gt;
array.getKey(array, &amp;quot;text&amp;quot;) -- same as above&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
* [http://nova-fusion.com/2011/06/30/lua-metatables-tutorial/ Metatables tutorial].&lt;br /&gt;
* [[OOP|MTA's built-in OO-system]].&lt;br /&gt;
* [[OOP_client|MTA's built-in OO-functions]].&lt;/div&gt;</summary>
		<author><name>Novo</name></author>
	</entry>
	<entry>
		<id>https://wiki.multitheftauto.com/index.php?title=OOP_in_Lua&amp;diff=43992</id>
		<title>OOP in Lua</title>
		<link rel="alternate" type="text/html" href="https://wiki.multitheftauto.com/index.php?title=OOP_in_Lua&amp;diff=43992"/>
		<updated>2015-01-18T02:07:56Z</updated>

		<summary type="html">&lt;p&gt;Novo: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;pageclass subcaption=&amp;quot;Adam James @adam.mta&amp;quot;&amp;gt;&amp;lt;/pageclass&amp;gt;&lt;br /&gt;
{{Note_box|&lt;br /&gt;
Any further questions can be discussed at the [http://google.com topic] dedicated to this tutorial.}}&lt;br /&gt;
&lt;br /&gt;
This is a scripting tutorial that teaches you how to start using an Object-Oriented developing interface with Lua.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
== Glossary ==&lt;br /&gt;
* environment: either a table or an array containing values.&lt;br /&gt;
&lt;br /&gt;
== Initialising ==&lt;br /&gt;
There is a basic and simple predefined variables we should recognize and know: '''self'''. Which refers to the environment within which we are executing a code.&lt;br /&gt;
=== Our first environment ===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
local array = {}&lt;br /&gt;
function array:example (argument)&lt;br /&gt;
	return &amp;quot;Hello&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
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:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
array:example()&lt;br /&gt;
array.example(array, example)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
As Lua is so cool, we're able to call a function using two methods: &amp;quot;''':'''&amp;quot; and &amp;quot;'''.'''&amp;quot;. 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'''.&lt;br /&gt;
&lt;br /&gt;
=== Variables and further handling ===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
local array = {text = &amp;quot;none&amp;quot;}&lt;br /&gt;
function array:setKey (key, value)&lt;br /&gt;
	self[key] = value&lt;br /&gt;
end&lt;br /&gt;
function array:getKey (key)&lt;br /&gt;
	return self[key]&lt;br /&gt;
end&lt;br /&gt;
print(array:getKey(&amp;quot;text&amp;quot;)) -- &amp;quot;none&amp;quot;&lt;br /&gt;
&lt;br /&gt;
array:setKey(&amp;quot;text&amp;quot;, &amp;quot;something&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
print(array:getKey(&amp;quot;text&amp;quot;)) -- &amp;quot;something&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
* [[OOP|MTA's built-in OO-system]].&lt;br /&gt;
* [[OOP_client|MTA's built-in OO-functions]].&lt;/div&gt;</summary>
		<author><name>Novo</name></author>
	</entry>
	<entry>
		<id>https://wiki.multitheftauto.com/index.php?title=OOP_in_Lua&amp;diff=43991</id>
		<title>OOP in Lua</title>
		<link rel="alternate" type="text/html" href="https://wiki.multitheftauto.com/index.php?title=OOP_in_Lua&amp;diff=43991"/>
		<updated>2015-01-18T01:58:39Z</updated>

		<summary type="html">&lt;p&gt;Novo: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;pageclass subcaption=&amp;quot;Adam James @adam.mta&amp;quot;&amp;gt;&amp;lt;/pageclass&amp;gt;&lt;br /&gt;
== '''This content is not concluded yet.''' ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
{{Note_box|&lt;br /&gt;
Any further questions can be discussed at the [http://google.com topic] dedicated to this tutorial.}}&lt;br /&gt;
&lt;br /&gt;
This is a scripting tutorial that teaches you how to start using an Object-Oriented developing interface with Lua.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
== Glossary ==&lt;br /&gt;
* environment: either a table or an array containing values.&lt;br /&gt;
&lt;br /&gt;
== Initialising ==&lt;br /&gt;
There is a basic and simple predefined variables we should recognize and know: '''self'''. Which refers to the environment within which we are executing a code.&lt;br /&gt;
=== Our first environment ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
local array = {}&lt;br /&gt;
function array:example (argument)&lt;br /&gt;
	return &amp;quot;Hello&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
What we do upon above is defining a ''local'' environment and then declaring the function '''example''' as part of it.&lt;br /&gt;
&lt;br /&gt;
Alright, so how should we proceed in order to call the mentioned function? As follows:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
array:example()&lt;br /&gt;
array.example(array, example)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
As Lua is so cool, we're able to call a function using two methods: &amp;quot;''':'''&amp;quot; and &amp;quot;'''.'''&amp;quot;. 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'''.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
* [[OOP|MTA's built-in OO-system]].&lt;br /&gt;
* [[OOP_client|MTA's built-in OO-functions]].&lt;/div&gt;</summary>
		<author><name>Novo</name></author>
	</entry>
	<entry>
		<id>https://wiki.multitheftauto.com/index.php?title=OOP_in_Lua&amp;diff=43990</id>
		<title>OOP in Lua</title>
		<link rel="alternate" type="text/html" href="https://wiki.multitheftauto.com/index.php?title=OOP_in_Lua&amp;diff=43990"/>
		<updated>2015-01-18T01:52:16Z</updated>

		<summary type="html">&lt;p&gt;Novo: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;pageclass subcaption=&amp;quot;Adam James @adam.mta&amp;quot;&amp;gt;&amp;lt;/pageclass&amp;gt;&lt;br /&gt;
== '''This content is not concluded yet.''' ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
{{Note_box|&lt;br /&gt;
Any further questions can be discussed at the [http://google.com topic] dedicated to this tutorial.}}&lt;br /&gt;
&lt;br /&gt;
This is a scripting tutorial that teaches you how to start using an Object-Oriented developing interface with Lua.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
== Glossary ==&lt;br /&gt;
* environment: either a table or an array containing values.&lt;br /&gt;
&lt;br /&gt;
== Initialising ==&lt;br /&gt;
There is a basic and simple predefined variables we should recognize and know: '''self'''. Which refers to the environment within which we are executing a code.&lt;br /&gt;
=== Our first environment ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
local array = {}&lt;br /&gt;
function array:example (argument)&lt;br /&gt;
	return &amp;quot;Hello&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
What we do upon above is defining a ''local'' environment and then declaring the function '''example''' as part of it.&lt;br /&gt;
&lt;br /&gt;
Alright, so how should we proceed in order to call the mentioned function? As follows:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
array:example()&lt;br /&gt;
array.example(array, example)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
As Lua is so cool, we're able to call a function using two methods: &amp;quot;''':'''&amp;quot; and &amp;quot;'''.'''&amp;quot;. As you can see on the example above, if we use a dot we're supposed to send '''self's value''' to the function.&lt;br /&gt;
&lt;br /&gt;
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'''.&lt;/div&gt;</summary>
		<author><name>Novo</name></author>
	</entry>
	<entry>
		<id>https://wiki.multitheftauto.com/index.php?title=OOP_in_Lua&amp;diff=43989</id>
		<title>OOP in Lua</title>
		<link rel="alternate" type="text/html" href="https://wiki.multitheftauto.com/index.php?title=OOP_in_Lua&amp;diff=43989"/>
		<updated>2015-01-18T01:51:49Z</updated>

		<summary type="html">&lt;p&gt;Novo: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;pageclass subcaption=&amp;quot;Adam James @adam.mta&amp;quot;&amp;gt;&amp;lt;/pageclass&amp;gt;&lt;br /&gt;
== '''This content is not concluded yet.''' ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
{{Note_box|&lt;br /&gt;
Any further questions can be discussed at the [http://google.com topic] dedicated to this tutorial.}}&lt;br /&gt;
&lt;br /&gt;
This is a scripting tutorial that teaches you how to start using an Object-Oriented developing interface with Lua.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
== Glossary ==&lt;br /&gt;
* environment: either a table or an array containing values.&lt;br /&gt;
&lt;br /&gt;
== Initialising ==&lt;br /&gt;
There is a basic and simple predefined variables we should recognize and know: '''self'''. Which refers to the environment within which we are executing a code.&lt;br /&gt;
=== Our first environment ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
local array = {}&lt;br /&gt;
function array:example (argument)&lt;br /&gt;
	return &amp;quot;Hello&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
What we do upon above is defining a ''local'' environment and then declaring the function '''example''' as part of it.&lt;br /&gt;
&lt;br /&gt;
Alright, so how should we proceed in order to call the mentioned function? As follows:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
array:example()&lt;br /&gt;
array.example(array, example)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
As Lua is so cool, we're able to call a function using two methods: &amp;quot;''':'''&amp;quot; and &amp;quot;'''.'''&amp;quot;. 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 use a colon, self's value will be the environment within which we are executing a code, i.e. '''array'''.&lt;/div&gt;</summary>
		<author><name>Novo</name></author>
	</entry>
	<entry>
		<id>https://wiki.multitheftauto.com/index.php?title=OOP_in_Lua&amp;diff=43988</id>
		<title>OOP in Lua</title>
		<link rel="alternate" type="text/html" href="https://wiki.multitheftauto.com/index.php?title=OOP_in_Lua&amp;diff=43988"/>
		<updated>2015-01-18T01:44:08Z</updated>

		<summary type="html">&lt;p&gt;Novo: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;pageclass subcaption=&amp;quot;Adam James @adam.mta&amp;quot;&amp;gt;&amp;lt;/pageclass&amp;gt;&lt;br /&gt;
== '''This content is not concluded yet.''' ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
{{Note_box|&lt;br /&gt;
Any further questions can be discussed at the [http://google.com topic] dedicated to this tutorial.}}&lt;br /&gt;
&lt;br /&gt;
This is a scripting tutorial that teaches you how to start using an Object-Oriented developing interface with Lua.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
== Glossary ==&lt;br /&gt;
* environment: either a table or an array containing values.&lt;br /&gt;
&lt;br /&gt;
== Initialising ==&lt;br /&gt;
There is a basic and simple predefined variables we should recognize and know: '''self'''. Which refers to the environment within which we are executing a code.&lt;br /&gt;
=== Our first environment ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
local environment = {}&lt;br /&gt;
function environment:example (argument)&lt;br /&gt;
	return &amp;quot;Hello&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
What we do upon above is defining a ''local'' environment and then declaring the function '''example''' as part of it.&lt;/div&gt;</summary>
		<author><name>Novo</name></author>
	</entry>
	<entry>
		<id>https://wiki.multitheftauto.com/index.php?title=OOP_in_Lua&amp;diff=43987</id>
		<title>OOP in Lua</title>
		<link rel="alternate" type="text/html" href="https://wiki.multitheftauto.com/index.php?title=OOP_in_Lua&amp;diff=43987"/>
		<updated>2015-01-18T01:38:52Z</updated>

		<summary type="html">&lt;p&gt;Novo: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== '''This content is not concluded yet.''' ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
{{Note_box|&lt;br /&gt;
Any further questions can be discussed at the [http://google.com topic] dedicated to this tutorial.}}&lt;br /&gt;
&lt;br /&gt;
This is a scripting tutorial that teaches you how to start using an Object-Oriented developing interface with Lua.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
== Glossary ==&lt;br /&gt;
* environment: either a table or an array containing values.&lt;br /&gt;
&lt;br /&gt;
== Initialising ==&lt;br /&gt;
There is a basic and simple predefined variables we should recognize and know: '''self'''. Which refers to the environment within which we are executing a code.&lt;br /&gt;
=== Our first environment ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
local environment = {}&lt;br /&gt;
function environment:example (argument)&lt;br /&gt;
	return &amp;quot;Hello&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
What we do upon above is defining a ''local'' environment and then declaring the function '''example''' as part of it.&lt;/div&gt;</summary>
		<author><name>Novo</name></author>
	</entry>
	<entry>
		<id>https://wiki.multitheftauto.com/index.php?title=OOP_in_Lua&amp;diff=43986</id>
		<title>OOP in Lua</title>
		<link rel="alternate" type="text/html" href="https://wiki.multitheftauto.com/index.php?title=OOP_in_Lua&amp;diff=43986"/>
		<updated>2015-01-18T01:34:03Z</updated>

		<summary type="html">&lt;p&gt;Novo: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== '''This content is not concluded yet.''' ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
{{Note_box|&lt;br /&gt;
Any further questions can be discussed at the [http://google.com topic] dedicated to this tutorial.}}&lt;br /&gt;
&lt;br /&gt;
This is a scripting tutorial that teaches you how to start using an Object-Oriented developing interface with Lua.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
== Glossary ==&lt;br /&gt;
* environment: either a table or an array containing values.&lt;br /&gt;
&lt;br /&gt;
== Initialising ==&lt;br /&gt;
There is a basic and simple predefined variables we should recognize and know: '''self'''. Which refers to the environment within which we are executing a code.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
self -- &lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Novo</name></author>
	</entry>
	<entry>
		<id>https://wiki.multitheftauto.com/index.php?title=OOP_in_Lua&amp;diff=43985</id>
		<title>OOP in Lua</title>
		<link rel="alternate" type="text/html" href="https://wiki.multitheftauto.com/index.php?title=OOP_in_Lua&amp;diff=43985"/>
		<updated>2015-01-18T01:17:59Z</updated>

		<summary type="html">&lt;p&gt;Novo: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== '''This content is not concluded yet.''' ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
{{Note_box|&lt;br /&gt;
Any further questions can be discussed at the [http://google.com topic] dedicated to this tutorial.}}&lt;br /&gt;
&lt;br /&gt;
This is a scripting tutorial that teaches you how to start using an Object-Oriented developing interface with Lua.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
== Introduction ==&lt;br /&gt;
There are few basic predefined variables we should recognize and know:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
self -- refers to the environment within which we are executing a code&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Novo</name></author>
	</entry>
	<entry>
		<id>https://wiki.multitheftauto.com/index.php?title=OOP_in_Lua&amp;diff=43984</id>
		<title>OOP in Lua</title>
		<link rel="alternate" type="text/html" href="https://wiki.multitheftauto.com/index.php?title=OOP_in_Lua&amp;diff=43984"/>
		<updated>2015-01-18T01:16:24Z</updated>

		<summary type="html">&lt;p&gt;Novo: Created page with &amp;quot;{{Note_box|Any further questions can be discussed at the [http://google.com topic] dedicated to this tutorial.}}  This is a scripting tutorial that teaches you how to start us...&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Note_box|Any further questions can be discussed at the [http://google.com topic] dedicated to this tutorial.}}&lt;br /&gt;
&lt;br /&gt;
This is a scripting tutorial that teaches you how to start using an Object-Oriented developing interface with Lua.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
== Introduction ==&lt;br /&gt;
There are few basic predefined variables we should recognize and know:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
self -- refers to the environment within which we are executing a code&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Novo</name></author>
	</entry>
</feed>