• 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 Store monster reference

Stellow

C++/C#/PHP/LUA
Joined
Oct 23, 2008
Messages
1,106
Reaction score
214
Location
Germany
GitHub
eubrunomiguel
What would be an effective way to store monster references? For example, to summon x monsters, and keep track of them to know when you kill one of them. I tried inserting it's GUID on a global Lua table, but seems that Lua easily lose data from it, for example, on a global lib file, store monsters= {}.
 
that shouldn't be the case, I'm ever since using that way and it works just fine and it's also the most efficient way
a prime example would be this thread from me https://otland.net/threads/tfs1-0-ingame-manager-administrating-players-and-such.217924/
EDIT: You just have to make sure that you are not reloading that file in any case else you'd ofcourse lose the data
You don't lose the data on reload if you place this in the lib file:
Code:
if not MONSTERS then
    MONSTERS = {}
end
 
You don't lose the data on reload if you place this in the lib file:
Code:
if not MONSTERS then
    MONSTERS = {}
end
I certainly wouldn't recommend that, because if you want to reload a file then certainly you want to reload everything, if not then this shouldn't be located in that file
but that's actually just my thinking I like to structure my stuff very organized so I would rather split them into files then to do it that way.
 
I certainly wouldn't recommend that, because if you want to reload a file then certainly you want to reload everything, if not then this shouldn't be located in that file
but that's actually just my thinking I like to structure my stuff very organized so I would rather split them into files then to do it that way.
There is no reason you shouldn't do it, if you don't want to erase the previous data in the table when reloading the lib.
Its already done in the globalstorage table for example
https://github.com/otland/forgottenserver/blob/master/data/lib/core/game.lua#L58-L60
 
I certainly wouldn't recommend that, because if you want to reload a file then certainly you want to reload everything, if not then this shouldn't be located in that file
but that's actually just my thinking I like to structure my stuff very organized so I would rather split them into files then to do it that way.

The only time a global/local table becomes nil is if it is originally nil or assigned nil, this means that if the table regardless of its scope is loaded within the file then when you reload the file the table resorts back to its default value.

However if it is stored somewhere else which does directly access or manipulate the table but rather references it, then and only then will the table retain its non-default value, however this does not apply to server restarts.

Edit:
If you want to retain values, create a new table in the database and reference them via a custom function.
 
There is no reason you shouldn't do it, if you don't want to erase the previous data in the table when reloading the lib.
Its already done in the globalstorage table for example
https://github.com/otland/forgottenserver/blob/master/data/lib/core/game.lua#L58-L60
If you don't ever want to erase while having the server on even tho you reload it then it's fine but if you need to reload it, it's not advisable.
The only time a global/local table becomes nil is if it is originally nil or assigned nil, this means that if the table regardless of its scope is loaded within the file then when you reload the file the table resorts back to its default value.

However if it is stored somewhere else which does directly access or manipulate the table but rather references it, then and only then will the table retain its non-default value, however this does not apply to server restarts.

Edit:
If you want to retain values, create a new table in the database and reference them via a custom function.
Just a quick example to let you know what I meant (as you might have misunderstood me)
Code:
test = {1,2,3,4,5}
for i = 1, #test do
print(test[i]) -> 1 2 3 4 5
end
test = {} -- if we reload we exactly do this as the initial table was empty
for i = 1, #test do
print(test[i]) -> prints nothing ofcourse, even tho everything is still in memory if the garbage collector didn't catch it yet as we re asign the pointer of the initial table which made everything inside inaccessable
end
 
why would you want to do that, monster id's are not steady once they die they get a new id, same goes for server restart, keep all of sql connection to a min you don't want to end up flooding it.
 
If you don't ever want to erase while having the server on even tho you reload it then it's fine but if you need to reload it, it's not advisable.

Just a quick example to let you know what I meant (as you might have misunderstood me)
Code:
test = {1,2,3,4,5}
for i = 1, #test do
print(test[i]) -> 1 2 3 4 5
end
test = {} -- if we reload we exactly do this as the initial table was empty
for i = 1, #test do
print(test[i]) -> prints nothing ofcourse, even tho everything is still in memory if the garbage collector didn't catch it yet as we re asign the pointer of the initial table which made everything inside inaccessable
end
If a table has an initial volume of values, and then you reassign the table as {} it is deleting the contents of the table, it is the same thing as saying table = nil, although the table no longer is considered a table at this stage if it is assigned nil, what it is considered is just nil.

This is not necessarily a rebuttal but rather to make things clear.
 
If a table has an initial volume of values, and then you reassign the table as {} it is deleting the contents of the table, it is the same thing as saying table = nil, although the table no longer is considered a table at this stage if it is assigned nil, what it is considered is just nil.

This is not necessarily a rebuttal but rather to make things clear.
nope it does not delete it's content, it just makes the content unreachable thus the garbage collector (if you even use him) picks it up somewhere on the way and clears the memory.
 
nope it does not delete it's content, it just makes the content unreachable thus the garbage collector (if you even use him) picks it up somewhere on the way and clears the memory.
um whatever you say.. if i can't access the data because i wrote over it with an empty table, then the data has been deleted, next thing you will be saying is this is how all variables, arrays, etc.. behave.
 
um whatever you say.. if i can't access the data because i wrote over it with an empty table, then the data has been deleted, next thing you will be saying is this is how all variables, arrays, etc.. behave.
I've been working long enough with lua to be able to claim something which is certainly the case.
Please do your homework next time before you spout such words I'm not mentaly retarded neither are you, thanks.
http://lua-users.org/wiki/GarbageCollectionTutorial
 
um whatever you say.. if i can't access the data because i wrote over it with an empty table, then the data has been deleted, next thing you will be saying is this is how all variables, arrays, etc.. behave.
Thats not the case, you did not wrote over with an empty table you just referenced the variable to another place, but if you still have references left to that table it will not be collected and will still be in the memory.

Try this:

Code:
function test(tab)
    print(unpack(tab))
    addEvent(test, 1000, tab)
end

function onSay(player)
    local t = {1, 2, 3}
    test(t) -- will print 1 2 3 forever, you wont be able to remove the reference, the only way is if you replace the function test to stop the addevent
    t = nil
    print(t) -- prints nil
    return false
end
 
.....
Thats not the case, you did not wrote over with an empty table you just referenced the variable to another place, but if you still have references left to that table it will not be collected and will still be in the memory.

Try this:

Code:
function test(tab)
    print(unpack(tab))
    addEvent(test, 1000, tab)
end

function onSay(player)
    local t = {1, 2, 3}
    test(t) -- will print 1 2 3 forever, you wont be able to remove the reference, the only way is if you replace the function test to stop the addevent
    t = nil
    print(t) -- prints nil
    return false
end
I know how functions behave, you are passing the data to the function not the container... yes if test were stuck in an infinite loop it would continually be passing the data to itself... no matter the value of t is after the data has been passed.

You can't trip me up.. i don't know why you try to... keep writing code I will keep explaining to you I already know how this works.

@Evil Hero, I've been programming longer than you have been alive, I do know a lot more languages than just lua.

Edit:
I've asked for help twice on this forum.. you people have nothing to teach me beyond that, k.
 
.....

I know how functions behave, you are passing the data to the function not the container... yes if test were stuck in an infinite loop it would continually be passing the data to itself... no matter the value of t is after the data has been passed.

You can't trip me up.. i don't know why you try to... keep writing code I will keep explaining to you I already know how this works.

@Evil Hero, I've been programming longer than you have been alive, I do know a lot more languages than just lua.

Edit:
I've asked for help twice on this forum.. you people have nothing to teach me beyond that, k.

You are just to good.
 
.....

I know how functions behave, you are passing the data to the function not the container... yes if test were stuck in an infinite loop it would continually be passing the data to itself... no matter the value of t is after the data has been passed.

You can't trip me up.. i don't know why you try to... keep writing code I will keep explaining to you I already know how this works.

@Evil Hero, I've been programming longer than you have been alive, I do know a lot more languages than just lua.

Edit:
I've asked for help twice on this forum.. you people have nothing to teach me beyond that, k.
No you don't know how functions behave, when you pass a table to a function you are not passing the data to the function, you are passing the reference. Stop acting like you know about everything and start listening to people when they are trying to explain you something that you clearly don't know.
 
It is acting a bit weird, I insert a value on the table, and then when I try to print on talkaction its size, works, but when I try to use on creaturescripts doesn't work.
 
Last edited:
It is acting a bit weird, I insert a value on the table, and then when I try to print on talkaction its size, works, but when I try to use on creaturescripts doesn't work.

I think is because table is store really the reference value, how to just insert a copy of it, so when monsters dies, its guid still there?
Where is the table in your code? If you can provide your talkaction and creaturescript.
 
Where is the table in your code? If you can provide your talkaction and creaturescript.
Lib:

if not wavesReferences then
wavesReferences = {}
end

TalkAction (insert monster on table)

local func = doCreateMonster
local ret = func(t[1], position)
if(tonumber(ret) == nil) then
effect = CONST_ME_POFF
doPlayerSendDefaultCancel(cid, (not ret and RETURNVALUE_NOTPOSSIBLE or RETURNVALUE_NOTENOUGHROOM))
else
table.insert(tableMonster, ret)
end

TalkAction (retrieve table value, this works!)
function onSay(cid, words, param, channel)
print(table.getn(wavesReferences))
return true
end

Creaturescripts(table is nil)
Code:
print(table.getn(wavesReferences))
    for i,monster in ipairs(wavesReferences) do
        print(monster)
        print(target)
        if monster == target then
            print("true")
            doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, table.getn(wavesReferences) .. " creatures left from this wave")
            wavesReferences[i] = nil
        end
    end
    return true
end
 
Last edited:
Back
Top