OOP Superclass Problems

Hey guys, I’m coding my game using tables as objects, and I am having problems with inheritance… I want to have a character superclass, and then be able to inherient the superclass in another player class. Currently I did this using superclass = class2, then changing class 2. My problem is that when I load the superclass, then the class, the class properties overwrite the superclass properties even if I’m directly calling the superclass… Does that make any sense, how I’m putting it?

Thanks!

-Jake [import]uid: 144504 topic_id: 30158 reply_id: 330158[/import]

If class2 inherits from superclass, shouldn’t you be saying “class2 = superclass” and extend from there instead of “superclass = class2”? By the way, are you using metatables?

Cheers,
Thomas [import]uid: 70134 topic_id: 30158 reply_id: 120742[/import]

Oh lol, I should have just coppied my code… -.- here is my code:

local player\_man = player\_base [import]uid: 144504 topic_id: 30158 reply_id: 120743[/import]

bump [import]uid: 144504 topic_id: 30158 reply_id: 121030[/import]

I was doing something like this in my current project then realized what it was really doing. I didn’t find a simple solution. I just ended up making a function to loop through table1 and copy it to table2.

From what I can tell referenceing a table is just referencing a memory pointer. So making one table = another table is just copying the pointer. In essence table1 and table2 are now pointing to the same data in memory.

There is table.copy but it only works until it hits a nil value or some such thing. Not much use for me.

I am not sure lua is capable of doing what you want directly. [import]uid: 56820 topic_id: 30158 reply_id: 121072[/import]

Ok, that makes sense… I came across this awhile ago, looks helpful… http://docs.davidmccuskey.com/display/docs/dmc_objects+Documentation [import]uid: 144504 topic_id: 30158 reply_id: 121100[/import]

If class2 inherits from superclass, shouldn’t you be saying “class2 = superclass” and extend from there instead of “superclass = class2”? By the way, are you using metatables?

Cheers,
Thomas [import]uid: 70134 topic_id: 30158 reply_id: 120742[/import]

Oh lol, I should have just coppied my code… -.- here is my code:

local player\_man = player\_base [import]uid: 144504 topic_id: 30158 reply_id: 120743[/import]

bump [import]uid: 144504 topic_id: 30158 reply_id: 121030[/import]

I was doing something like this in my current project then realized what it was really doing. I didn’t find a simple solution. I just ended up making a function to loop through table1 and copy it to table2.

From what I can tell referenceing a table is just referencing a memory pointer. So making one table = another table is just copying the pointer. In essence table1 and table2 are now pointing to the same data in memory.

There is table.copy but it only works until it hits a nil value or some such thing. Not much use for me.

I am not sure lua is capable of doing what you want directly. [import]uid: 56820 topic_id: 30158 reply_id: 121072[/import]

Ok, that makes sense… I came across this awhile ago, looks helpful… http://docs.davidmccuskey.com/display/docs/dmc_objects+Documentation [import]uid: 144504 topic_id: 30158 reply_id: 121100[/import]

So, I’ve been looking into this, and I’ve started using metatables… But I’m still having problems… It seems whenever I try to call my player physics objects from the subclass, it errors and says player is NIL and I can’t figure out why? Do metatables not like calls to self within their methods? Doesn’t seem like that should be the case, from what I can gather from lua.org… [import]uid: 144504 topic_id: 30158 reply_id: 121755[/import]

Hi Jake.
Well, I do not know much things about Corona API, by itself. But I am well versed in pure Lua, though.
Actually, I can’t provide much help as you didn’t provide your code.
But looking at the oiginal post, I’ll try to bring my two cents.

I’ll assume you need to build an OOP system, with inheritance feature.

This is a sample, written from scratch, for a base class, with instantiation.

superClass = {}  
superClass.\_\_index = superClass  
  
function superClass:new(...)  
 local instance = {...}  
 return setmetatable(instance,self)  
end  

Now, you can plug this function for inheritance.

function superClass:extends(...)  
 local newClass = {...}  
 newClass.\_\_index = newClass  
 return setmetatable(newClass,self)  
end  

As you might be aware of, it doesn’t relies at all with Corona’s API, that’s pure Lua.
Hope that will help, though.

You can also find on github two of my libraries for OOP.
30log, which is very small, yet with tons of features and very simple to cope with. And also Lua-Class-System, quite outdated, but more functionnalities though. [import]uid: 142361 topic_id: 30158 reply_id: 121875[/import]

Thanks :slight_smile: The way I’m doing it now is object = { properties } then just putting all the methods in there… This has to change now, because I would like object inheritance… With this, do you need to put return superClass at the end of every file? Also, what is __index?
Thanks!
[import]uid: 144504 topic_id: 30158 reply_id: 121995[/import]

With this, do you need to put return superClass at the end of every file?

Not sure about what you’re asking here… Can you be more explicit, please ?

Also, what is __index?

\_\_index is a methamethod that Lua uses when you ask for a missing key in a table while this table has a metatable having the key \_\_index.
That’s the core for OOP and inheritance mechanisms.

Say that you have a base class, with methods, and an instance from this class.
As you might be aware of, an instance must share all its class methods.

class = {...}  
class.foo = function(...) ... end  
  
instance = {...}  
instance.foo --\> nil  

Now, if instance has table class as its metatable, and class implements \_\_index key, we have a different behaviour:

class = {...}  
class.\_\_index = class  
class.foo = function(...) ... end  
  
instance = setmetatable({...},class)  
instance.foo --\> valid  
instance.foo[/code] is still [code]nil[/code], as function [code]foo[/code] does not exists inside table [code]instance[/code]. But, instead of yielding [code]nil[/code], Lua tries to lookup inside instance's metatable, which is [code]class[/code]. This metatable has a key named [code]\_\_index[/code], which means. Lookup where [code]\_\_.index[/code] redirects. So Lua looks inside table [code]class[/code], finds method [code]foo[/code] and then returns the result of the call [code]foo(...)

There’s a great tutorial here, about metatables.
PiL is the reference, though. [import]uid: 142361 topic_id: 30158 reply_id: 122072[/import]

Sorry for getting back so late… Currently I have all my objects in all their own class files… So currently in the level files, I need to import them with require('class1'), and at the end of the class files I return the table of the object…

local class1 = { properties }  
  
...methods here  
  
return class1  

Do I still need to do this with metatables? Also, what does ... do? I see you have it there and I can’t tell if it references anything or if it’s a place holder…
Thanks! [import]uid: 144504 topic_id: 30158 reply_id: 122326[/import]

Well, if you do have each class and methods in their own single file, metatables won’t change anything about that. You definitey have to use require to call these classes.

Consider ... as a placeholder. It’s my way to say etcetera.
[import]uid: 142361 topic_id: 30158 reply_id: 122331[/import]

So, I’ve been looking into this, and I’ve started using metatables… But I’m still having problems… It seems whenever I try to call my player physics objects from the subclass, it errors and says player is NIL and I can’t figure out why? Do metatables not like calls to self within their methods? Doesn’t seem like that should be the case, from what I can gather from lua.org… [import]uid: 144504 topic_id: 30158 reply_id: 121755[/import]

Yes, but at the end, does there need to be a return class1?

So then I don’t put … in the program?

Thanks! [import]uid: 144504 topic_id: 30158 reply_id: 122428[/import]

Hi Jake.
Well, I do not know much things about Corona API, by itself. But I am well versed in pure Lua, though.
Actually, I can’t provide much help as you didn’t provide your code.
But looking at the oiginal post, I’ll try to bring my two cents.

I’ll assume you need to build an OOP system, with inheritance feature.

This is a sample, written from scratch, for a base class, with instantiation.

superClass = {}  
superClass.\_\_index = superClass  
  
function superClass:new(...)  
 local instance = {...}  
 return setmetatable(instance,self)  
end  

Now, you can plug this function for inheritance.

function superClass:extends(...)  
 local newClass = {...}  
 newClass.\_\_index = newClass  
 return setmetatable(newClass,self)  
end  

As you might be aware of, it doesn’t relies at all with Corona’s API, that’s pure Lua.
Hope that will help, though.

You can also find on github two of my libraries for OOP.
30log, which is very small, yet with tons of features and very simple to cope with. And also Lua-Class-System, quite outdated, but more functionnalities though. [import]uid: 142361 topic_id: 30158 reply_id: 121875[/import]