Jump to content




Simple Object Oriented Programming


5 replies to this topic

#1 M4sh3dP0t4t03

  • Members
  • 255 posts
  • LocationGermany

Posted 26 August 2013 - 01: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.

#2 Engineer

  • Members
  • 1,378 posts
  • LocationThe Netherlands

Posted 26 August 2013 - 02: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.


#3 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 26 August 2013 - 02: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!

#4 M4sh3dP0t4t03

  • Members
  • 255 posts
  • LocationGermany

Posted 27 August 2013 - 08:57 AM

Thanks for the feedback, I will try editing it later today.

Edit: I just edited it.

#5 MrObsidy

  • Members
  • 63 posts
  • LocationHesse, Germany

Posted 22 September 2016 - 12:54 AM

I dunno why People are Complaining. I found this very Useful and Informative.
Thanks for sharing, M4sh3dP0t4to3!

-Alex

#6 tomtrein

  • Members
  • 3 posts

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.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users