I'm working on a game in Lua, and so far I have everything working with everything in one document. However, to better organize everything, I've decided to expand it into modules, and while I figure I could probably get it working more or less the same, I figure now might be an opportunity to make things a little more clear and elegant.
One example is enemies and enemy movement. I have an array called enemyTable, and here is the code in Update that moves each enemy:
for i, bat in ipairs(enemyTable) do
if bat.velocity < 1.1 * player.maxSpeed * pxPerMeter then
bat.velocity = bat.velocity + 1.1 * player.maxSpeed * pxPerMeter * globalDelta / 10
end
tempX,tempY = math.normalize(player.x - bat.x,player.y - bat.y)
bat.vectorX = (1 - .2) * bat.vectorX + (.2) * tempX
bat.vectorY = (1 - .2) * bat.vectorY + (.2) * tempY
bat.x = bat.x + (bat.velocity*bat.vectorX - player.velocity.x) * globalDelta
bat.y = bat.y + bat.velocity * bat.vectorY * globalDelta
if bat.x < 0 then
table.remove(enemyTable,i)
elseif bat.x > windowWidth then
table.remove(enemyTable,i)
end
end
This code does everything I want it to, but now I want to move it into a module called enemy.lua. My original plan was to create a function enemy.Move() inside enemy.lua that would do this exact thing, then return the updated enemyTable. Then the code inside main.lua would be something like:
enemyTable = enemy.Move(enemyTable)
What I'd prefer is something like:
enemyTable.Move()
...but I'm not sure if there's any way to do that in Lua? Does anyone have any suggestions for how to accomplish this?
table.removeon a table while traversing it withipairs. Every time you do you lose/miss an element in the original table from your loop. Try it:t = {1,2,3,4,5,6,7,8,9,10}; for i, v in ipairs(t) do print(v) if v % 2 == 0 then table.remove(t, i) end end(or just see it here). - Etan Reisneripairs).for i = #enemyTable, 1, -1 do ... table.remove(enemyTable, i) ... endis safe since you only ever shift elements you've already dealt with. The alternative is to not edit the table in-place but to copy elements you want to save to a new table as you go and then return that. The choices have trade-offs in space and cost and so, in part, depend on your table sizes, etc. - Etan Reisner