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

TFS 1.X+ TFS 1.5 8.6 Nekiro Downgrade

Itutorial

Excellent OT User
Joined
Dec 23, 2014
Messages
2,326
Solutions
68
Reaction score
999
So I am running into a really weird problem with storing data in lua tables.

Pretty much I am creating monsters with a loop and adding them into the table. Then im removing the monsters one at a time with an onKill code.

When I print the length of the table its completely wrong. It should be: 4, 3, 2, 1, 0. I get random values each time for example: 3, 3, 2, 2, 0, 0.

Here is the code involved.

Monsters added to table here
Lua:
for i = 1, #dungeon.monsters do
        if dungeon.monsters[i].count then
            for x = 1, dungeon.monsters[i].count do
                local pos = Position(math.random(dungeon.area.min.x, dungeon.area.max.x), math.random(dungeon.area.min.y, dungeon.area.max.y), math.random(dungeon.area.min.z, dungeon.area.max.z))
                
                while not isPosClear(pos) do
                    pos = Position(math.random(dungeon.area.min.x, dungeon.area.max.x), math.random(dungeon.area.min.y, dungeon.area.max.y), math.random(dungeon.area.min.z, dungeon.area.max.z))
                end
                
                local monster = Game.createMonster(dungeon.monsters[i].name, pos)
                DUNGEON_TABLES[uid].monsters[#DUNGEON_TABLES[uid].monsters + 1] = monster:getId() -- MONSTERS ADDED TO TABLE HERE--
            end
        else
            local pos = Position(math.random(dungeon.area.min.x, dungeon.area.max.x), math.random(dungeon.area.min.y, dungeon.area.max.y), math.random(dungeon.area.min.z, dungeon.area.max.z))
                
            while not isPosClear(pos) do
                pos = Position(math.random(dungeon.area.min.x, dungeon.area.max.x), math.random(dungeon.area.min.y, dungeon.area.max.y), math.random(dungeon.area.min.z, dungeon.area.max.z))
            end
                
            local monster = Game.createMonster(dungeon.monsters[i].name, pos)
            DUNGEON_TABLES[uid].monsters[#DUNGEON_TABLES[uid].monsters + 1] = monster:getId() -- MONSTERS ADDED TO TABLE HERE--
        end
    end

Monsters removed from table and print the table length here
Lua:
local monsters = dungeonTable.monsters
    if monsters then
        for i = 1, #monsters do
            local monster = Monster(monsters[i])
            if monster and monster:getId() == target:getId() then
                monsters[i] = nil
                isNormalMonster = true
                break
            end
        end
    end
    
    print(#monsters)

Any ideas?
 
Solution
you are removing from random ranges and "#" works only for ordered arrays
Lua:
local t = {1, 2, 3}
print(#t) -- prints 3

local t2 = {[1] = 1, [3] = 3}
print(#t2) -- prints 0

solution is to iterate the table to count elements or keep your numbers somewhere in variable
you are removing from random ranges and "#" works only for ordered arrays
Lua:
local t = {1, 2, 3}
print(#t) -- prints 3

local t2 = {[1] = 1, [3] = 3}
print(#t2) -- prints 0

solution is to iterate the table to count elements or keep your numbers somewhere in variable
 
Solution
Back
Top