• 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!

Lua [TFS 1.2] OOP Classes

keilost

Member
Joined
Aug 4, 2012
Messages
63
Reaction score
12
Location
Brazil
Hello,

What is the right way to create classes in LUA?

I want to create a class named Pet, it extends Creature class...
 
Solution
You are doing this in c++
no

Lua:
Pet = setmetatable({}, {
        __index = Creature,
        __call = function(self, ...)
            local obj = {
                -- set stuff here
            }
            return setmetatable(obj, {__index = Pet})
        end
    }
)

function Pet.someMethod(self)
    print("random method for pet class")
    print("working object:", self)
end
Pet will inherit the Creature class using the index metamethod, call is the constructor for the "class" (aka using Pet())
of course if you want to actually work with userdata then you'd have to implement this in source, but you can store a creature userdata inside of the table you return as your "object" and call it within your methods when you want to...
You are doing this in c++
no

Lua:
Pet = setmetatable({}, {
        __index = Creature,
        __call = function(self, ...)
            local obj = {
                -- set stuff here
            }
            return setmetatable(obj, {__index = Pet})
        end
    }
)

function Pet.someMethod(self)
    print("random method for pet class")
    print("working object:", self)
end
Pet will inherit the Creature class using the index metamethod, call is the constructor for the "class" (aka using Pet())
of course if you want to actually work with userdata then you'd have to implement this in source, but you can store a creature userdata inside of the table you return as your "object" and call it within your methods when you want to work with it
 
Solution
no

Lua:
Pet = setmetatable({}, {
        __index = Creature,
        __call = function(self, ...)
            local obj = {
                -- set stuff here
            }
            return setmetatable(obj, {__index = Pet})
        end
    }
)

function Pet.someMethod(self)
    print("random method for pet class")
    print("working object:", self)
end
Pet will inherit the Creature class using the index metamethod, call is the constructor for the "class" (aka using Pet())
of course if you want to actually work with userdata then you'd have to implement this in source, but you can store a creature userdata inside of the table you return as your "object" and call it within your methods when you want to work with it
why? Isnt it better in c++?
 
why? Isnt it better in c++?
if you wanted to create a new userdata for a pet, you would
however he's asking in lua, this is the way to do it
i'm assuming this pet class would just be extra variables tied within a monster object that way he can control some shit
not sure why he wants to inherit creature class because pets are monsters not npcs or players, so i'd change __index = Monster but that's up to him.
 
if you wanted to create a new userdata for a pet, you would
however he's asking in lua, this is the way to do it
i'm assuming this pet class would just be extra variables tied within a monster object that way he can control some shit
not sure why he wants to inherit creature class because pets are monsters not npcs or players, so i'd change __index = Monster but that's up to him.

Sorry, I want to extend monster and not creature hahaha...

The better way to do this is in C++ or LUA?
 
"better" depends on what you want
Lua (not LUA) is easier and more flexible, but you have to use tables which isn't guaranteed to be immutable
C++ is less flexible and harder but you get to use userdata which you can implement read-only variables
i'd say in your case use lua since pets are just a "masked" class per se, for monsters
 
Why I can't execute methods? Like return Pet('Name'):getId()
because although you can implement this in lua, the implicit self variable for your objects is just a table, not a monster userdata
you could create custom methods yourself to work with the monster, but at that point there's no reason for inheritance because you can't create a monster userdata with just a name, so i have no clue how you're planning on implementing something like this
why do you specifically need a pet class when they're literally just summons?
 
no

Lua:
Pet = setmetatable({}, {
        __index = Creature,
        __call = function(self, ...)
            local obj = {
                -- set stuff here
            }
            return setmetatable(obj, {__index = Pet})
        end
    }
)

function Pet.someMethod(self)
    print("random method for pet class")
    print("working object:", self)
end
Pet will inherit the Creature class using the index metamethod, call is the constructor for the "class" (aka using Pet())
of course if you want to actually work with userdata then you'd have to implement this in source, but you can store a creature userdata inside of the table you return as your "object" and call it within your methods when you want to work with it
do you can show me an exemple of the usage of this ?
i'm trying to understand, but without success
thanks
 
I just want a code pretty and organized

Instead of create a class like this Monster/creature:petEnvolve(), i want to do this Pet:envolve(), but I was believing that extending the class would work fine the userdata...

Is that right?

function Pet:envolve()
self = Creature(self.something)
end
 
Back
Top