This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
billysback's profile picture

[TUTORIAL] Object Orientated in Lua [BEGINNER(ish)]

Started by billysback, 11 November 2012 - 12:57 AM
billysback #1
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:

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.
Espen #2
Posted 11 November 2012 - 02:42 AM
That's a good explanation. Unfortunately you're not talking about metatables though but about Lua's equivalent of object oriented programming.
Metatables are rather tables that contain information about other tables.
Like what happens if you try to access a table value, or use arithmetic operations on it, etc.

Some more info here (Roblox is nice to ones' eyes):
Roblox - Metatable
Roblox - Object-Oriented Programming
Lua PIL - Metatables and Metamethods
Lua PIL - Object-Oriented Programming
billysback #3
Posted 11 November 2012 - 02:59 AM
OK, thanks, didn't really know what it was called :/
edited now (replace all: metatable object, metatables object orientation)