OOP Superclass Problems

Yes.
That was implied. :stuck_out_tongue: [import]uid: 142361 topic_id: 30158 reply_id: 122478[/import]

haha Ok… Sorry for all the questions, but what is the advantage of metatables over tables? [import]uid: 144504 topic_id: 30158 reply_id: 122493[/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]

Update: Ok, so I’ve been messing around, and I don’t see how to use the extends function… I got it working just fine by doing this though…

superClass.lua

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

subClass.lua

require("superClass")  
  
subClass = characterBase:new()  
subClass.\_\_index = subClass  
  
return subClass  

So whats the point in having a superClass:extends(...) function?

Thanks! [import]uid: 144504 topic_id: 30158 reply_id: 122674[/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]

For the advantage that metatables offer, I suggest you go through the links I gave before (post #5)
They are pretty straightforward to understand, and will provide lots of feedback on the topic.

Now, about your snippet, it’s totally normal that this works.
The purpose of extends is to create a derived class from a base class. Inheritance, in other words.

Actually what you are doing to create your subclass is similar to what extends(...) does.

Note that actually, you don’t need the return statement at the end of superClass.lua, as superClass table is declared global. If it was local within its chunk (I’d prefer, though), a return statement would have been required. [import]uid: 142361 topic_id: 30158 reply_id: 122869[/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]

Yes.
That was implied. :stuck_out_tongue: [import]uid: 142361 topic_id: 30158 reply_id: 122478[/import]

haha Ok… Sorry for all the questions, but what is the advantage of metatables over tables? [import]uid: 144504 topic_id: 30158 reply_id: 122493[/import]

Ohhh haha ok, thanks!!! That really helped… And I was making the table local… That was just what I had seen before, but it makes sense to make it global… Really helps alot… lol Thanks again! [import]uid: 144504 topic_id: 30158 reply_id: 123086[/import]

Update: Ok, so I’ve been messing around, and I don’t see how to use the extends function… I got it working just fine by doing this though…

superClass.lua

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

subClass.lua

require("superClass")  
  
subClass = characterBase:new()  
subClass.\_\_index = subClass  
  
return subClass  

So whats the point in having a superClass:extends(...) function?

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

For the advantage that metatables offer, I suggest you go through the links I gave before (post #5)
They are pretty straightforward to understand, and will provide lots of feedback on the topic.

Now, about your snippet, it’s totally normal that this works.
The purpose of extends is to create a derived class from a base class. Inheritance, in other words.

Actually what you are doing to create your subclass is similar to what extends(...) does.

Note that actually, you don’t need the return statement at the end of superClass.lua, as superClass table is declared global. If it was local within its chunk (I’d prefer, though), a return statement would have been required. [import]uid: 142361 topic_id: 30158 reply_id: 122869[/import]

Ohhh haha ok, thanks!!! That really helped… And I was making the table local… That was just what I had seen before, but it makes sense to make it global… Really helps alot… lol Thanks again! [import]uid: 144504 topic_id: 30158 reply_id: 123086[/import]