noYou are doing this in c++
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
noYou are doing this in c++
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
why? Isnt it better in c++?no
Pet will inherit the Creature class using the index metamethod, call is the constructor for the "class" (aka using Pet())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
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
if you wanted to create a new userdata for a pet, you wouldwhy? 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.
because although you can implement this in lua, the implicit self variable for your objects is just a table, not a monster userdataWhy I can't execute methods? Like return Pet('Name'):getId()
do you can show me an exemple of the usage of this ?no
Pet will inherit the Creature class using the index metamethod, call is the constructor for the "class" (aka using Pet())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
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