Posted 26 August 2013 - 02:31 PM
Welcome to my tutorial about Object Oriented Programming in Lua.
So what is OOP actually? OOP is a programming paradigm that includes "objects", which are data fields that include variables and functions. A "class" is basically a blueprint for an object. For example, if you're body was a program that uses OOP there would probably be a class called hand, which would contain a few other classes like "fingers" and variables like "position" and also a few functions like "makeFist" and "move". There would be probably two objects of the type hand, which would be called "leftHand" and "rightHand".
So, how do we actually use OOP in Lua? Well first we have to make a simple table that we use as our class.
Now we have a table, but how do we make it an actual class? Well… the first thing we need is a constructor(the function that is called when an object of the class is getting created) that returns an object of the class. Let's call it "new":
But why "self" as an argument? The self is there to let you access variables from the class itself inside your functions.
And how do you use it? Let me show it to you:
Ok, this seems like a nice thing, but can't we add a little bit more functionality to it? No problem:
Now we just use them like this:
This was my tutorial about simple OOP in Lua, have fun with using this stuff! I will add a some code that is a little bit more useful soon and also explain some more stuff like inheritance.
So what is OOP actually? OOP is a programming paradigm that includes "objects", which are data fields that include variables and functions. A "class" is basically a blueprint for an object. For example, if you're body was a program that uses OOP there would probably be a class called hand, which would contain a few other classes like "fingers" and variables like "position" and also a few functions like "makeFist" and "move". There would be probably two objects of the type hand, which would be called "leftHand" and "rightHand".
So, how do we actually use OOP in Lua? Well first we have to make a simple table that we use as our class.
Class = {}
Now we have a table, but how do we make it an actual class? Well… the first thing we need is a constructor(the function that is called when an object of the class is getting created) that returns an object of the class. Let's call it "new":
Class = {
new = function(self)
local new = {}
setmetatable(new, {__index = self})
return new
end}
Great! You just made your very first class in Lua!But why "self" as an argument? The self is there to let you access variables from the class itself inside your functions.
And how do you use it? Let me show it to you:
object = Class.new(Class)
That wasn't to hard, was it? But do I really have to write the name of the class as an argument every time I want to call a function from it? Nope, actually you can just use colons instead of a period so it does that automatically for you:object = Class:new()
Ok, this seems like a nice thing, but can't we add a little bit more functionality to it? No problem:
Class = {
new = function(self)
local new = {}
setmetatable(new, {__index = self})
return new
end,
setVar = function(self, var)
self.var = var
end,
printVar = function(self)
print(self.var)
end
}
Now we have two new functions in this class! One that sets a variable and another one that prints it!Now we just use them like this:
Class = {
new = function(self)
local new = {}
setmetatable(new, {__index = self})
return new
end,
setVar = function(self, var)
self.var = var
end,
printVar = function(self)
print(self.var)
end
}
object1 = Class:new()
object2 = Class:new()
object1:setVar(io.read())
object2:setVar(io.read())
object1:printVar()
object2:printVar()
This was my tutorial about simple OOP in Lua, have fun with using this stuff! I will add a some code that is a little bit more useful soon and also explain some more stuff like inheritance.