OOP in Lua: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 23: Line 23:
array.example(array, example)
array.example(array, example)
</syntaxhighlight>
</syntaxhighlight>
As Lua is so cool, we're able to call a function using two methods: "''':'''" and "'''.'''". As you can see on the example above, if we use a dot we're supposed to send '''self's value''' to the function. Yes, that's right, and in case we use a colon, self's value will be the environment within which we are executing a code, i.e. '''array'''.
As Lua is so cool, we're able to call a function using two methods: "''':'''" and "'''.'''". As you can see on the example above, if we use a dot we're supposed to send '''self's value''' to the function. Yes, that's right, and in case we use a colon, self's value will be the environment within which we are executing a code, i.e. '''array'''. We can send the self value in case we want a function to override its self and, instead of working as if self was the environment within it's working, it will work over the environment we sent (example under [[#Advanced_examples|advanced examples]]).


=== Variables and further handling ===
=== Variables and further handling ===
Line 34: Line 34:
return self[key]
return self[key]
end
end
print(array:getKey("text")) -- "none"


array:setKey("text", "something")
array:getKey("text") -- returns "none"
 
array:setKey("text", "something") -- sets "text"'s value to 'something'
print(array:getKey("text")) -- "something"
array:getKey("text") -- returns "something"
</syntaxhighlight>
</syntaxhighlight>
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.
What we do here is retrieving and modifying '''text''''s value, which a variable inside array, recurring to functions inside the same environment as the variable is.


== Metatables ==
Everything you need to know about them is stupendously explained [http://nova-fusion.com/2011/06/30/lua-metatables-tutorial/ here].
== Advanced examples ==
Overriding self's value:
<syntaxhighlight lang="lua">
local array = {text = "none"}
local array2 = {text = "none2"}
function array:setKey (key, value)
self[key] = value
end
function array:getKey (key)
return self[key]
end
array.getKey(array2, "text") -- returns "none2"
array.setKey(array2, "text", "something2") -- sets array2's "text" value to 'something2'
array.getKey(array2, "text") -- returns "something"
array:getKey("text") -- returns "none"
array.getKey(array, "text") -- same as above
</syntaxhighlight>


==See Also==
==See Also==
* [http://nova-fusion.com/2011/06/30/lua-metatables-tutorial/ Metatables tutorial].
* [[OOP|MTA's built-in OO-system]].
* [[OOP|MTA's built-in OO-system]].
* [[OOP_client|MTA's built-in OO-functions]].
* [[OOP_client|MTA's built-in OO-functions]].

Revision as of 02:29, 18 January 2015

This template is no longer in use as it results in poor readability.

This is a scripting tutorial that teaches you how to start using an Object-Oriented developing interface with Lua.

Glossary

  • environment: either a table or an array containing values.

Initialising

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.

Our first environment

local array = {}
function array:example (argument)
	return "Hello"
end

What we do upon above is defining a local environment and then declaring the function example as part of it. Alright, so how should we proceed in order to call the mentioned function? As follows:

array:example()
array.example(array, example)

As Lua is so cool, we're able to call a function using two methods: ":" and ".". As you can see on the example above, if we use a dot we're supposed to send self's value to the function. Yes, that's right, and in case we use a colon, self's value will be the environment within which we are executing a code, i.e. array. We can send the self value in case we want a function to override its self and, instead of working as if self was the environment within it's working, it will work over the environment we sent (example under advanced examples).

Variables and further handling

local array = {text = "none"}
function array:setKey (key, value)
	self[key] = value
end
function array:getKey (key)
	return self[key]
end

array:getKey("text") -- returns "none"
array:setKey("text", "something") -- sets "text"'s value to 'something'
array:getKey("text") -- returns "something"

What we do here is retrieving and modifying text's value, which a variable inside array, recurring to functions inside the same environment as the variable is.

Metatables

Everything you need to know about them is stupendously explained here.

Advanced examples

Overriding self's value:

local array = {text = "none"}
local array2 = {text = "none2"}
function array:setKey (key, value)
	self[key] = value
end
function array:getKey (key)
	return self[key]
end
array.getKey(array2, "text") -- returns "none2"
array.setKey(array2, "text", "something2") -- sets array2's "text" value to 'something2'
array.getKey(array2, "text") -- returns "something"
array:getKey("text") -- returns "none"
array.getKey(array, "text") -- same as above

See Also