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

C++ Storage in Monster? TFS 1.X

Fresh

Quack!
Joined
Oct 21, 2009
Messages
1,837
Solutions
18
Reaction score
614
Location
Poland
Hello there!
It's possible to bring back add/setStorage and getStorage into monsters?
Anyone knows how to add storage to monster in TFS 1.X, since tfs-devs removed it?
 
Solution
put this in global.lua
Lua:
MONSTER_STORAGE = MONSTER_STORAGE or {}

function Monster.setStorageValue(self, key, value)
    local cid = self:getId()
    local storageMap = MONSTER_STORAGE[cid]
    if not storageMap then
        MONSTER_STORAGE[cid] = {[key] = value}
    else
        storageMap[key] = value
    end
end

function Monster.getStorageValue(self, key)
    local storageMap = MONSTER_STORAGE[self:getId()]
    if storageMap then
        return storageMap[key] or -1
    end
    return -1
end

example
Lua:
monster:setStorageValue(29937, 1)
print(monster:getStorageValue(22937)) -- 1
put this in global.lua
Lua:
MONSTER_STORAGE = MONSTER_STORAGE or {}

function Monster.setStorageValue(self, key, value)
    local cid = self:getId()
    local storageMap = MONSTER_STORAGE[cid]
    if not storageMap then
        MONSTER_STORAGE[cid] = {[key] = value}
    else
        storageMap[key] = value
    end
end

function Monster.getStorageValue(self, key)
    local storageMap = MONSTER_STORAGE[self:getId()]
    if storageMap then
        return storageMap[key] or -1
    end
    return -1
end

example
Lua:
monster:setStorageValue(29937, 1)
print(monster:getStorageValue(22937)) -- 1
 
Last edited:
Solution
except there's 0 use to add it in source because monster storage values shouldn't be saved to database + it takes 1 minute to implement in lua
 
if anything source technically would be more optimized for memory usage, but imo not to the point where it's worth to implement it unless you're going to be throwing storages on 100,000+ monsters
1 storage on 100,000 monsters would only take up approx~ 8mb of memory if implemented in lua
that's without any optimizations
if you really wanted you could delete invalid cids in the MONSTER_STORAGE table on server save and then let lua garbage collector run to remove all unnecessary memory used from monsters that aren't ingame anymore
which if you used you could probably get that 8mb down to approx 1-2mb assuming most monsters die before server save
 
if anything source technically would be more optimized for memory usage, but imo not to the point where it's worth to implement it unless you're going to be throwing storages on 100,000+ monsters
1 storage on 100,000 monsters would only take up approx~ 8mb of memory if implemented in lua
that's without any optimizations
if you really wanted you could delete invalid cids in the MONSTER_STORAGE table on server save and then let lua garbage collector run to remove all unnecessary memory used from monsters that aren't ingame anymore
which if you used you could probably get that 8mb down to approx 1-2mb assuming most monsters die before server save

What about removing the storage putted on monster while he dies with your/c++ code? It's working?
For example: What if monster get storage and he died, whats now?
 
in c++ the storage map gets deleted automatically once the monster gets removed
in lua you can implement the same thing with onDeath creatureevent

honestly wouldn't worry that much about optimizations, if anything you'd be saving mere kilobytes of memory
 
in c++ the storage map gets deleted automatically once the monster gets removed
in lua you can implement the same thing with onDeath creatureevent

Ok thanks!
Also it's any feature in source that allow monster spawn like on RL tibia? I mean: if monster exists in spawn area he wouldn't be spawned again but if he got killed he can spawn no matter if player stay in spawn area or not
 
there's no feature like that by default, you'd have to look around on the forum using search bar or looking through the c++ codes section to see if you can find if someone already wrote it
otherwise you might get someone to write it for you after you make a thread in requests section
 
put this in global.lua
Lua:
MONSTER_STORAGE = MONSTER_STORAGE or {}

function Monster.setStorageValue(self, key, value)
    local cid = self:getId()
    local storageMap = MONSTER_STORAGE[cid]
    if not storageMap then
        MONSTER_STORAGE[cid] = {[key] = value}
    else
        storageMap[key] = value
    end
end

function Monster.getStorageValue(self, key)
    local storageMap = MONSTER_STORAGE[self:getId()]
    if storageMap then
        return storageMap[key] or -1
    end
    return -1
end

example
Lua:
monster:setStorageValue(29937, 1)
print(monster:getStorageValue(22937)) -- 1

So, lets see if i understand this chunk of code. If i use this lets say in a creaturescript or a spell, that is used by a monster creature, it will save the desired storage. My question here is, shouldnt cid be self:getUniqueId(), or getId is already unique for everyspawn monster?
Also, what usages could have this snipet? Other than making a monster "different" from others (which can be achieved on easier ways).

Thank you!
 
So, lets see if i understand this chunk of code. If i use this lets say in a creaturescript or a spell, that is used by a monster creature, it will save the desired storage. My question here is, shouldnt cid be self:getUniqueId(), or getId is already unique for everyspawn monster?
Also, what usages could have this snipet? Other than making a monster "different" from others (which can be achieved on easier ways).

Thank you!
You can save some informations on specific monsters and yes getId() is unique for every creature
 
Back
Top