• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

Solved Questions about Inheritance & Metatables

Omni Cloud

Banned User
Joined
May 31, 2016
Messages
308
Reaction score
149
I am not exactly certain how a metatable is constructed but also how it inherits from one metatable to another.

Although this code works, I am not sure how it works if that makes any sense.
Code:
Creature = {}
Creature.__index = Creature

setmetatable(Creature, {
    __call = function(cls, ...)
        return cls.new(...)
    end,
   })

function Creature:whoami()
    return "I am a Creature"
end


Player = getmetatable(setmetatable({}, Creature))
print(Player:whoami())
Code:
I am a Creature

Also can anyone point me to a few decent tutorials on the subject :)
 
I remember having a hard time learning this, the tutorials overall are pretty hard to understand, so you have to do alot of experiments to learn it yourself.

However tutorials in lua-users wiki are probably the ones you should look:
http://lua-users.org/wiki/ObjectOrientationTutorial
http://lua-users.org/wiki/InheritanceTutorial
http://lua-users.org/wiki/MetamethodsTutorial
http://lua-users.org/wiki/LuaClassesWithMetatable
http://lua-users.org/wiki/SimpleLuaClasses
http://lua-users.org/wiki/MetatableEvents

Good reading :)
 
Back
Top