How to refrence specific instances of classes

I am new to developing for the playdate and on Lua, so forgive me if this is a stupid question. I have a class “enemy” and a class “player”. when the player attacks i want it to damage all enemies within a 1 block distance. I am having trouble with referencing specific instances of the enemy class. Because I have multiple enemies I would need to specifically damage the enemies within a one tile distance. How would i do this. (if you need to see my code you have to ask for it individually)

edit: If anyone is making a turn-based tile-based game (aka Broughlike) and would like to let me see how they handled this that would be greatly appreciated)

One thing you could do is keep a table of all enemies (if you don’t already) and loop through the table to check the distance of each one. If there are tons of enemies you might want some other efficiencies, but I’m guessing your grid’s not that big.

You don’t have to worry about empty “slots” left in the table when enemies are destroyed—just don’t use a numeric index to loop. Assuming that each enemy has an x and a y:

enemies = {enemy1, enemy2, enemy3} --Fill the table by whatever means

for i, thisEnemy in ipairs(enemies) do
    --Do something with the x,y location
    print (thisEnemy.x)
    print (thisEnemy.y)
end

(Index i isn’t used for anything here, but it’s there if you want a unique ID # for each enemy in the table. You can then refer to enemies[i].)

interesting i’ll try this when i fix the simulator

this worked I was able to damage each enemy

1 Like