Hi all,
I’m trying to learn object oriented programming in lua. I understand why it’s useful and have gone through several tutorials on OOP in Lua. But I do have a few questions.
In this code from develephant, what does this mean? I know this is a function to create a new character but I am a little lost as to what is happening inside of the function.
function CharacterClass:new( o ) local o = o or {} setmetatable( o, self ) self.\_\_index = self return o end
Also, for adding images and physics bodies, would it be better to have that done inside the main.lua or the class file?
And in this code:
playerClass = {} function playerClass:new( startX, startY ) local player = display.newImage( "images/player.png" ) player.x = startX player.y = startY return player end return playerClass
what does the “return player” line do? If I take it out, I get all kinds of errors. But when I code without using OOP, I don’t do the “return” line when I create new objects. What does this line do and why have I not had to use it before learning OOP?
I would greatly appreciate any help anyone can give.
Thanks!