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
Monsters removed from table and print the table length here
Any ideas?
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?