• 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 table as param

silveralol

Advanced OT User
Joined
Mar 16, 2010
Messages
1,480
Solutions
9
Reaction score
211
Hello, I'm trying using a table as parameter to use in another function ...
Code:
above the function ofc ...
local playerTable = {}
for i = 1, #playerPos do
        local playerTile = Tile(playerPos[i]):getTopCreature()
        if playerTile and playerTile:isPlayer() then
            table.insert(playerTable, playerTile.uid)
            playerTile:teleportTo(some room)
        end
    end
then I want to pass it to another function ...
Code:
addEvent(checkPlayers, 1 * 60 * 1000, playerTable) -- how pas it ?
the function is just to check the ID of the players...
Code:
local function checkPlayers(playerTable)
         local spectators = Game.getSpectators(Position(), false, false, 5, 5, 5, 5)
         for i = 1, #spectators do
             local spectator = spectators[i]
             if playerTable ~= nil then
                  if isInArray(playerTable, spectator.uid) then
                    spectator:teleportTo(somewhere)
                  end
             end
       end
end
so, in the case...
player A was insert in playersTable using the .uid, so in the function only player A should be teleported somewhere
but if has a player B not insert in playersTable, he will got teleported out of the room
I try use this function from orts as model
Code:
local function clearBossRoom(playerId, bossId,
        centerPosition,
        rangeX, rangeY, exitPosition)
    local spectators, spectator = Game.getSpectators(centerPosition, false, false,
        rangeX,
        rangeX, rangeY, rangeY)
    for i = 1, #spectators do
        spectator = spectators[i]
        if spectator:isPlayer() and spectator.uid == playerId then
            spectator:teleportTo(exitPosition)
            exitPosition:sendMagicEffect(CONST_ME_TELEPORT)
        end
        if spectator:isMonster() and spectator.uid == bossId then
            spectator:remove()
        end
    end
end
So folks, how I can pass tables as parameters and it work how I expect?
 
the magic thing about functions is that they are similar to boxes.

X enters the box, stuff happens and Y leaves the box.

When we talk about parameters, they are just "signatures" that will be replaced when the function is called by the values that are sent, so knowing this you don't necessarily need to adapt anything, you just need to build the stuff that happens assuming X is a table.

Lua:
function processTable(X)
    for i = 1, #X do
        print(i, X[i])
    end
end


In the above example I just assume X is a table and I iterate its values printing index and value in the position.
 
You should have tried first, what you did is fine, tables are like any other variable in lua, to execption that these cannot be copied with the assignment operator, this means that you can pass a table as a parameter and modify it elsewhere, since these are passed as references, however, you must respect the names of the global variables and the local variables and the environments
1627104064053.png
 
bump
I didn't got it working ...
I'll explain better the use of the function ...
I want to make a bossRoom System ...
exemple:
team A enter in the room, so I store their UID to check in the function that will clean the room....
team A leave the room before the function be called...
team B enter in the room, the function shouldn't remove team B of the room
I can't figure how use table in the right way to make it
thank you guys that respond, but I'm in dark with this
 
You can stop the event two ways.

First way:
Code:
eventName = addEvent(cleanRoom, delay, ...)
stopEvent(eventName)

Second way:
Code:
globalSessionId = 0
addEvent(cleanRoom, delay, globalSessionId, ...)

in cleanRoom:
Code:
if sessionId ~= globalSessionId then
return
end

when the clean is called earlier
Code:
globalSessionId = globalSessionId+1
 
Last edited:
Back
Top