Log In  

I'm trying to create a game where there are multiple enemies on the screen at once, but I can't find an easy way to store the enemys' position other than to create a million variables. Is there any way to avoid this?

P#80106 2020-07-29 22:33 ( Edited 2020-07-29 23:17)

Do you know how to handle Functions? A possible Solution is: You can create a Function, which returns a completely new Table every time you call it. This Table has every Variable in it that is needed to handle Enemies, so you can grab the Table and store it in another "Uber-Table". On the right Time (like, in the _draw, or _update-Function), you can Iter through that "Uber-Table".

For Example:

-- function that returns a new table
function make_enemy(x,y)
 return {
  x=x,
  y=y,
 }
end

function _init()
 -- store all enemies in this table
 enemies = {}

 -- add some enemies
 add(enemies, make_enemy(10,20))
 add(enemies, make_enemy(30,40))
end

function _draw()
 cls(0)

 -- iter through that table and draw
 for enemy in all(enemies) do
  spr(0, enemy.x, enemy.y)
 end
end
P#80154 2020-07-31 09:22 ( Edited 2020-07-31 09:22)

Thank you for the help!

P#80219 2020-08-01 23:19

[Please log in to post a comment]

Follow Lexaloffle:          
Generated 2024-04-20 06:38:25 | 0.005s | Q:14