Table of enemies

In my game I would like several enemies to target the player character.
Each enemy will have it’s own attributes (type, x/y pos, life, speed, direction etc.) all of which I realise will need to be stored in a table. What I’m having difficulty figuring out though is how to specify which sprite to use for each enemy type, and then how to display it on the screen.

I’m not sure if I’ve explained that very well so if I haven’t I’ll try again, but any help would be appreciated.

Thanks
[import]uid: 7841 topic_id: 13241 reply_id: 313241[/import]

Hey there, it would look something like this;

[lua] local enemyTable = {}

local enemyRed = {}
enemyRed.image = “enemyRed.png”
enemyRed.x = 10
enemyRed.y = 10
enemyRed.speed = 20
table.insert(enemyTable, enemyRed)

local enemyBlue = {}
enemyBlue.image = “enemyBlue”
enemyBlue.x = 10
enemyBlue.y = 10
enemyBlue.speed = 20
table.insert(enemyTable, enemyBlue)[/lua]

Then you’d do something like this to spawn a random enemy - although obviously you can change it to non-random as you wish :slight_smile:

[lua]function getEnemy()
enemyProp = enemyTable[math.random(1, #enemyTable)]
enemy = display.newImage(enemyProp.image)
enemy.x = enemyProp.x
enemy.y = enemyProp.y
return enemy
end
getEnemy()[/lua]

Obviously you’d be using sprites rather than images but it’s the same basic idea :slight_smile: [import]uid: 52491 topic_id: 13241 reply_id: 48561[/import]

Hi Peach,
Thanks for your super speedy response, it really is appreciated. It took me a short while to understand your code (my fault not yours). The idea I had though was as a new enemy was spawned it would create a random entity and add it to a single enemy table, for example…

enemies={} -- create enemy table  
  
function create\_enemy()  
 local ent = math.random(1,10) -- random enemy type  
 local enx = 108 -- x pos  
 local eny = 108 -- y pos  
 local ens = math.random(1, 5) -- speed  
 local endir = 1 -- direction  
 local engfx = ("enemy" + ent + ".png" -- image (not sure if the syntax is correct on this one  
  
 table.insert(enemies, species=ent, img=engfx, x=enx, y=eny, speed=ens, direction=endir)  
end  
  

When I then call the “update_enemy” function I would like it to loop through the enemy table and update each existing enemies (x/y pos, check to see if still alive etc.). The other thing I’m not sure of is how to actually display the enemies. I know this is all really basic stuff but I just seem to have hit a brick wall with it.

[import]uid: 7841 topic_id: 13241 reply_id: 48583[/import]

Hey again Appletreeman,

This isn’t (in my opinion) “really basic stuff” and there’s no shame in not getting it all right away; good things take time :wink:

Ultimately you’re asking a big question, or a lot of small ones - I’d encourage you to have a bit of a study of this; http://blog.anscamobile.com/2011/06/understanding-lua-tables-in-corona-sdk/

It’s really easy to understand compared to a lot of info out there and should help you with tables, including understanding how to add to them, access them, etc.

ALSO - above, you are missing a closing ) on the image, there :wink: [import]uid: 52491 topic_id: 13241 reply_id: 48752[/import]

Many thanks for that link Peach, it’s a well written article and made it so much easier to understand.
I love this place and the fact that so many people are willing to help each other out so much.
My only gripe is that I don’t think it’s particularly well laid out in terms of finding things. There’s stuff on here that I’ve read that I now want to reference but just cannot find them, such as string handling, converting numbers to strings etc.

Anyway, like I said, many many thanks for your invaluable help, it really is appreciated. [import]uid: 7841 topic_id: 13241 reply_id: 49011[/import]

Appletreeman in your create_enemy() function, you are just putting the filename of the enemy in your table. You still have to actually load that graphic.

One of the really, and I mean really cool features of Corona SDK is that you can simply add new attributes to existing Corona objects.

Reconsider this:

enemies={} -- create enemy table  
maxEnemies = 10  
  
for i=1,maxEnemies do  
 enemies[i] = nil  
end  
   
function create\_enemy()  
 local ent = math.random(1,10) -- random enemy type  
 local enemySprite = display.newImage("enemy" .. ent .. ".png")  
 enemySprite.x = 108  
 enemySprite.y = 108 -- the object already has X and Y  
 enemySprite.speed = math.random(1,5) -- add your own attributes  
 enemySprite.dir = 1  
 return enemySprite  
end  
-- later when you want to create a new enemy  
--  
local newEnemy = create\_enemy() -- gives you a new image on screen.  
myDisplayGroup:insert(newEnemy) -- if needed, but recommended  
  
for i=1,maxEnemies do  
 if enemies[i] == nil then -- empty slot  
 enemies[i] = newEnemy  
 end  
end  
  
-- later when you kill an enemy  
  
enemies[i]:removeSelf() -- take it out of the group and free the memory  
enemies[i] = nil -- makes sure garbage collection frees it up but  
 -- more importantly, you can reuse this slot in  
 -- the table to refer to another enemy  
  

Now I could have used #enemies to get the current number of entries, but when you kill an enemy, its good to reuse the slot so your table doesn’t grow in size. Its must faster to iterate over a 10 entry table than it is a 1000 entry one if you keep adding to the end and nil’ing out things in the middle for when you want to move all your objects, check for collisions, etc. [import]uid: 19626 topic_id: 13241 reply_id: 49046[/import]

Hi Robmiracle, Cheers for the tip there. It’s taken me about half an hour or so to read and re-read several times to get my head around what you’re doing, but it’s finally clicked (I think) and, yes, it does make sense to do things that way. I didn’t realise that when you removed an entry from the middle of the table there would just be a gap there, but of course it makes sense that that is what would happen. Now I just have to sit and think about how many enemies I’m likely to have when the game starts to hot up a bit :slight_smile:

This place rocks :slight_smile: [import]uid: 7841 topic_id: 13241 reply_id: 49063[/import]

In my space shooter, OmniBlaster I think I allow 8 enemies on the screen at once at low levels and up to 15 at the level’s I’ve never seen without cheat mode being on! Now as soon as I blow something away, I generate a new one so it ends up visually being a lot of targets and a never ending stream of them. But a lot will depend on your game. For mine, I started at 10, and then ended up tweeking it as i got feed back from testers and my own testing.
[import]uid: 19626 topic_id: 13241 reply_id: 49071[/import]