Code: Select all
for i = 1, #self.activeEnemies, 1 do
self.activeEnemies[i]:draw()
end
Code: Select all
for i = 1, #self.activeEnemies, 1 do
self.activeEnemies[i]:draw()
end
The only other snippets that i think could contribute to the issue is:
Code: Select all
if Mouse:key_down(1) then
local spawndEnem = self.enemList[1]
table.insert(self.activeEnemies, spawndEnem)
spawndEnem:spawn(self.mouseInf.mouseWorldPosX, self.mouseInf.mouseWorldPosY, self.world)
print(#self.activeEnemies)
end
The best part of Lua: pairsMummyTheTiger wrote: ↑Thu Aug 15, 2024 6:31 pmCould you explain this more? I am new to Love and Lua
Code: Select all
for index, activeEnemy in pairs (self.activeEnemies) do
activeEnemy:draw()
end
Code: Select all
for index, activeEnemy in ipairs (self.activeEnemies) do
activeEnemy:draw()
end
Also see: https://www.lua.org/manual/5.2/manual.html#3.4.6
Regular for loop will not work as the length operator (#) has similar issues as ipairs in your case.Note that a table like
{10, 20, nil, 40}
is not a sequence, because it has the key 4 but does not have the key 3. (So, there is no n such that the set {1..n} is equal to the set of positive numeric keys of that table.)
Thanks for the explanation! I have always been trying to avoid pairs but that seems pretty simple. I went back and changed the pool of tables from integer index to use strings, easier for me to call to and now they wont be random like you mentioned. However i still have the same issue, only the last enemy is being called. I checked my table of active enemies with print in it is getting populated with index 1,2,3.. etc.darkfrei wrote: ↑Fri Aug 16, 2024 10:37 am The best part of Lua: pairs
It iterates all and each element in the table "self.activeEnemies", even not numerical indices. But the ordering will surprise you.Code: Select all
for index, activeEnemy in pairs (self.activeEnemies) do activeEnemy:draw() end
The second best part of Lua is ipairs:
The code iterates index from 1 to n, where n+1 is first not existing index. All other indices will be ignored.Code: Select all
for index, activeEnemy in ipairs (self.activeEnemies) do activeEnemy:draw() end
Code: Select all
---- spawn enemy
local spawndEnem = self.enemList['enemy1']
table.insert(self.activeEnemies, spawndEnem)
spawndEnem:spawn(self.character.x + spwn.mx, self.character.y + spwn.my, self.world)
Code: Select all
function GameRun:requireDirectory( dir , intable) ----directory folder and table to insert all children
dir = dir or ""
local entities = love.filesystem.getDirectoryItems(dir)
for k, ents in ipairs(entities) do
trim = string.gsub( ents, ".lua", "")
intable[trim] = require(dir .. "/" .. trim)
for index, data in pairs (intable) do
print("[LOADED]",index, data)
end
end
end
Code: Select all
----- update enemies
for index, actEne in pairs (self.activeEnemies) do
actEne:update(dt)
end
Code: Select all
----- draw enemies
for index, actEne in pairs (self.activeEnemies) do
actEne:draw()
end
Code: Select all
local spawndEnem = self.enemList['enemy1']
table.insert(self.activeEnemies, spawndEnem)
Its fixed! This was exactly the problem, it wasn't truly being copied and instead added/moved to the table it was already in. I also found this helpful https://stackoverflow.com/questions/640 ... e-by-valuepgimeno wrote: ↑Sun Aug 18, 2024 10:28 am Your problem could be caused by all enemies referencing the same object. This looks suspicious:Note that the first assignment does not make a copy of the enemy1 table: it "points" to the table. So, the self.activeEnemies table ends up containing a single copy of the same enemy many times.Code: Select all
local spawndEnem = self.enemList['enemy1'] table.insert(self.activeEnemies, spawndEnem)
It's hard to give tips on how to fix it without an idea of how you want it to behave, as you've given very little code, but in OOP it's important to differentiate between class (the type of an object) and object (one instance of that class; there could be many). I'm not sure if each element in enemyList should be a different class or they could all be the same class; that depends on your design. But for sure, each element in the self.activeEnemies table should be a different object.
Code: Select all
---- spawn enemy
local spawndEnem = tableShallowCopy(self.enemList['enemy1'])
table.insert(self.activeEnemies, spawndEnem)
spawndEnem:load(self.character.x + spwn.mx, self.character.y + spwn.my, self.world)
Code: Select all
function tableShallowCopy(t)
local t2 = {}
for k,v in pairs(t) do
t2[k] = v
end
return t2
end
Users browsing this forum: Amazon [Bot], Bing [Bot], Google [Bot] and 9 guests