• 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= {}.
 
Lib:



TalkAction (insert monster on table)



TalkAction (retrieve table value, this works!)


Creaturescripts(table is nil)
You are setting the wavesReferences to nil.
"wavesReferences = nil"

If you want to scan the table and remove something from it you shouldn't use ipairs. If you want to remove them and keep its indexes ordered you should start from the end of the table and remove the values with table.remove, however you can use pairs and set the key to nil if you want.

Example (removing all values greater than 10 in a table):
Code:
local t = {1, 12, 3, 14, 5, 66, 77, 7}

for i = #t, 1, -1 do
    if t[i] > 10 then
        table.remove(t, i)
    end
end

print(unpack(t)) -- 1 3 5 7
 
You are setting the wavesReferences to nil.
"wavesReferences = nil"

If you want to scan the table and remove something from it you shouldn't use ipairs. If you want to remove them and keep its indexes ordered you should start from the end of the table and remove the values with table.remove, however you can use pairs and set the key to nil if you want.

Example (removing all values greater than 10 in a table):
Code:
local t = {1, 12, 3, 14, 5, 66, 77, 7}

for i = #t, 1, -1 do
    if t[i] > 10 then
        table.remove(t, i)
    end
end

print(unpack(t)) -- 1 3 5 7

But even before the statement to nil the value, the first line printing the table value is 0. It would make sense if at least the print(table.getn(wavesReferences)) on second line was right.

I also removed the statement wavesReferences = nil to see if it was influencing, but no.

Code:
function onKill(cid, target, lastHit)
    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
 
But even before the statement to nil the value, the first line printing the table value is 0. It would make sense if at least the print(table.getn(wavesReferences)) on second line was right.

I also removed the statement wavesReferences = nil to see if it was influencing, but no.

Code:
function onKill(cid, target, lastHit)
    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
Yep it seems that each event has its own environment in tfs 0.4-, so you actually cant do this with tables in the lib. :/

I'd suggest you to do it with onDeath, then you just have to register this event to the creature, but if you need to use onKill than you probably need to edit the source.

Or you could try to use storages and globalstorages.
 
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.
As intelligent as you think you are, you obviously don't know the 1st thing about functions... this isn't c++ where a method or function can pass data by reference or by value, its lua where the data is passed by value...
 
As intelligent as you think you are, you obviously don't know the 1st thing about functions... this isn't c++ where a method or function can pass data by reference or by value, its lua where the data is passed by value...
You are wrong.

Code:
function test(t, i)
    i = i or 1
    if t[i] then
        t[i] = t[i]*2
        return test(t, i+1)
    end
    return true
end

local tab = {1, 2, 3, 4, 5}
test(tab)
print(unpack(tab)) -- 2  4  6  8  10

Data is not passed to the function, only the reference of the table.

Please, read:
https://www.lua.org/pil/2.5.html
http://lua-users.org/wiki/TablesTutorial
https://www.lua.org/manual/5.3/manual.html#2.1

Table values are references

"When you pass a table to a function or store it in a new variable, etc. a new copy of that table is not created. Tables do not act like numbers in these cases. Instead the variable or function becomes a reference to the original table. This is much like a pointer in the C Language. For example:"

-----------------------

"Tables, functions, threads, and (full) userdata values are objects: variables do not actually contain these values, only references to them. Assignment, parameter passing, and function returns always manipulate references to such values; these operations do not imply any kind of copy."
 
Last edited:
.....

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.
Keep making a fool out of yourself, if you indeed where programming longer than I'm alive you certainly had a better attitude towards people who can obviously teach you one ore more things and admit when you're not beeing right when people even backup there stuff with solid proof.
I'm sorry for all of this offtopic but I can't stand people who behave like that.
Lib:



TalkAction (insert monster on table)



TalkAction (retrieve table value, this works!)


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
Are you talking about talkactions/lib/talkactions.lua as in form of lib? because 0.3.x doesn't share lua states so the only way to make this work is to put it into lib/000-constant.lua file or any other which are located there, or if your server is even older you can put it into global.lua that works aswell.
 
Keep making a fool out of yourself, if you indeed where programming longer than I'm alive you certainly had a better attitude towards people who can obviously teach you one ore more things and admit when you're not beeing right when people even backup there stuff with solid proof.
I'm sorry for all of this offtopic but I can't stand people who behave like that.

Are you talking about talkactions/lib/talkactions.lua as in form of lib? because 0.3.x doesn't share lua states so the only way to make this work is to put it into lib/000-constant.lua file or any other which are located there, or if your server is even older you can put it into global.lua that works aswell.

No, the lib files that I meant are located on data/lib folder.
 
Lua states aren't shared between the different script interfaces, that's the case in 0.3.x series, 0.4 series, I don't know if this changed in 1.x series but I doubt.

Global libs just means is a common lib along the different script interfaces but it doesn't mean the state is global, a variable can be global inside it owns scripting interface ( you can share variables between scripts of the same interface ) but it won't be shared between the different states.

The reason why global lib and "{interface}/lib" (eg: talkactions/libs) exist is because sometimes there are functions/variables that are common for the different script interfaces in that case they go to the global lib, but sometimes there are functions/variables that are only needed / used exclusively by a script interface (the best example for this case is the spells lib)

There been multiple attemps to share the lua states in TFS in the past, you can find those attempts by searching in the forum, but each attempt had small problems/bugs.

There are workarounds to have real "global variables":
- you could use a global storage value as string (in 0.3.x series was possible to set string values to storages, but apparently they removed this on 1.x series)
- you could use a database table ( I highly dont recommend this, big waste )
- you could use tmp files and I/O functions ( I highly dont recommend this, big waste )

but the best solution is to do it via sources ( you could have a std map somewhere in sources and link it to lua functions, since is in sources it will be shared along all the lua states [same way how global storages work] ) if your user title is real this solution shouldn't be hard at all to implement.

good luck
 
Lua states aren't shared between the different script interfaces, that's the case in 0.3.x series, 0.4 series, I don't know if this changed in 1.x series but I doubt.

Global libs just means is a common lib along the different script interfaces but it doesn't mean the state is global, a variable can be global inside it owns scripting interface ( you can share variables between scripts of the same interface ) but it won't be shared between the different states.

The reason why global lib and "{interface}/lib" (eg: talkactions/libs) exist is because sometimes there are functions/variables that are common for the different script interfaces in that case they go to the global lib, but sometimes there are functions/variables that are only needed / used exclusively by a script interface (the best example for this case is the spells lib)

There been multiple attemps to share the lua states in TFS in the past, you can find those attempts by searching in the forum, but each attempt had small problems/bugs.

There are workarounds to have real "global variables":
- you could use a global storage value as string (in 0.3.x series was possible to set string values to storages, but apparently they removed this on 1.x series)
- you could use a database table ( I highly dont recommend this, big waste )
- you could use tmp files and I/O functions ( I highly dont recommend this, big waste )

but the best solution is to do it via sources ( you could have a std map somewhere in sources and link it to lua functions, since is in sources it will be shared along all the lua states [same way how global storages work] ) if your user title is real this solution shouldn't be hard at all to implement.

good luck
It did change in 1.x. everything is global (with exceptions) and about the topic I've said already the best would be editting the source but if you can't do that just use globalstorages with strings and serialize the table you want there.
 
Back
Top