• 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 Question about class extentions.

Codinablack

Dreamer
Content Editor
Joined
Dec 26, 2013
Messages
1,375
Solutions
7
Reaction score
525
Ok so my question is aimed at 1.4 and up, for lua, I know we can extend the classes provided for us, with our own custom functions like

Lua:
function Player.isUsingOtClient(self)

I have used this many times and its great, but what I was curious of, is there a way to create new variables for the class as well?

something like

Lua:
Monster.Level = 0

and then be able to use and manipulate the data like so


Lua:
function Monster.getLevel(self)
return self.Level
end

Is there any way to do this without having to create a new sub-class of the original class?

My question is intended for all the variable types, string, number, boolean, and table.

If this is easily possible, could someone please provide a small example sample for me of each type?
 
Solution
Monster/Player/Npc and other Lua objects are created in C++ over and over again (with every 'C++ to Lua' function call like onUse(player ...)).
If you assign any extra attribute to these objects, it will be deleted after function execution (onUse(player ...) etc.).

Only way to make it work is to create table in Lua indexed by creature UID. It will leak memory, as there is no event executed when monster is removed from RAM, so we can't easily clear unused values from it (level of already dead monsters).
Code:
local monsterLevels = {}
function Monster.getLevel(self)
    -- default level = 0
    return monsterLevels[self:getId()] or 0
end
function Monster.setLevel(self, newLevel)
    monsterLevels[self:getId()] = newLevel
end...

Gesior.pl

Mega Noob&LOL 2012
Senator
Premium User
Joined
Sep 18, 2007
Messages
2,443
Solutions
51
Reaction score
1,962
Location
Poland
GitHub
gesior
Monster/Player/Npc and other Lua objects are created in C++ over and over again (with every 'C++ to Lua' function call like onUse(player ...)).
If you assign any extra attribute to these objects, it will be deleted after function execution (onUse(player ...) etc.).

Only way to make it work is to create table in Lua indexed by creature UID. It will leak memory, as there is no event executed when monster is removed from RAM, so we can't easily clear unused values from it (level of already dead monsters).
Code:
local monsterLevels = {}
function Monster.getLevel(self)
    -- default level = 0
    return monsterLevels[self:getId()] or 0
end
function Monster.setLevel(self, newLevel)
    monsterLevels[self:getId()] = newLevel
end
I did NOT TEST if it works fine.
 
Solution
OP
OP
Codinablack

Codinablack

Dreamer
Content Editor
Joined
Dec 26, 2013
Messages
1,375
Solutions
7
Reaction score
525
I appreciate the answer, although I have the same concerns as you for the storing the monsters in that way, and it would ofc use up more memory than my intended way. I honestly didn't think there was a way to do exactly what I was looking for, the monster levels were just an example, I was hoping to be able to manipulate all the classes in such a manner, I suppose the solutions at hand are the ones I will have to utilize for different circumstances, ie, using a table or creating a sub-class... as for monsters, I will probably just do "tiers" instead of levels, and use skulls to indicate the creatures tier, would be easier to manage I think.
 
Top