Posted 11 November 2012 - 01:57 AM
When creating my new art API I ran in to a lot of trouble whilst trying to create the object orentation;
So not only for others but also for my own reference I am going to create a basic tutorial on how to create object orentation,
I will not, however, be going in to a lot of depth in to why or what things do, just what to do with them.
So, firstly, just create your table:
now after every defined value you must put a "," after it has been initialized, so for variable (which I advise being initialized at the top of the table) you would simply put:
However, for functions it is multi-lined, so you place the "," after the end. Like so:
The self parameter does not have to be a parameter when calling the function though, if you are including a self in a function then to call it just do:
Now to use the self parameter you just use it as if it were the table you were editing, so lets say you had the variable "name" inside your object, with the value "Billy", then you print that you could make a function like so:
If a function does not require any information from inside the object, or if you are getting a variable from the object, you use "." so that you do not tell the program to insert the table in to the parameters as self.
That's pretty much it, object's (and object orientation in lua) are/is rather simple once you understand how they are made and the rules surrounding them.
So not only for others but also for my own reference I am going to create a basic tutorial on how to create object orentation,
I will not, however, be going in to a lot of depth in to why or what things do, just what to do with them.
So, firstly, just create your table:
table = {
}
now after every defined value you must put a "," after it has been initialized, so for variable (which I advise being initialized at the top of the table) you would simply put:
variable = value,
However, for functions it is multi-lined, so you place the "," after the end. Like so:
function-name = function(self, parameters)
<code>
end,
now, one thing you will notice about initializing this function is that it has the "self" parameter at the front, this is not compulsory but if you want to access information (like variables) inside the object, you must include this.The self parameter does not have to be a parameter when calling the function though, if you are including a self in a function then to call it just do:
table:function-name(parameters)
notice the ":" as appose to ".", what this does is places the self in for you, it is essentially
table.function-name(table, parameters)
but by replacing the "." with a ":" you are telling the program to insert the table in to the function as the self parameter (not sure if this parameter must be the first one or not, I would make it the first one just in case)Now to use the self parameter you just use it as if it were the table you were editing, so lets say you had the variable "name" inside your object, with the value "Billy", then you print that you could make a function like so:
printName = function(self)
print(self.name)
end,
So this entire object (presuming there is nothing else inside it) would look like:
table = {
name = "Billy",
printName = function(self)
print(self.name)
end,
}
If a function does not require any information from inside the object, or if you are getting a variable from the object, you use "." so that you do not tell the program to insert the table in to the parameters as self.
That's pretty much it, object's (and object orientation in lua) are/is rather simple once you understand how they are made and the rules surrounding them.