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

Simple Object Oriented Programming

Started by M4sh3dP0t4t03, 26 August 2013 - 12:31 PM
M4sh3dP0t4t03 #1
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.
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.
Engineer #2
Posted 26 August 2013 - 03:04 PM
I don't really like this. That is because this doesn't explain OOP very well. Why do I have to use colons, what is self? Etc. Etc

To clearify, I do know how it works. But somebody else might ask those questions.
theoriginalbit #3
Posted 26 August 2013 - 03:07 PM
I agree, and also, just to state again what I stated in the AaP thread GravityScore made, you are breaking conventional OO paradigms by creating a setter, at least you didn't do a getter too. :P/>

Also you're not promoting good naming conventions, naming conventions are essential in programming and making sure programs are easy to extend and understand. if you don't know what I'm saying is bad, then I think you may need to read up on it yourself!
M4sh3dP0t4t03 #4
Posted 27 August 2013 - 09:57 AM
Thanks for the feedback, I will try editing it later today.

Edit: I just edited it.
MrObsidy #5
Posted 22 September 2016 - 01:54 AM
I dunno why People are Complaining. I found this very Useful and Informative.
Thanks for sharing, M4sh3dP0t4to3!

-Alex
tomtrein #6
Posted 21 February 2017 - 05:22 PM
Alex, i agree with you, that this is one of the simpler guides on object orientated programming in Lua.
It was also very straight to the point, and clear to programmers with some experience.

On the other hand, Lua isn't a very good way to start object orientated programming, seeing as it uses table to nest information into. I highly recommend for beginners to lua and / or object orientated programming to pick up a java or c# book, to introduce themselves. Afterwards this tutorial will make a lot more sense.