Public Group

# Enemy Array

This topic is 5176 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Could someone explain how I would go about making an enemy array. For example if I have 5 enemies walking forward and one bumps into the wall, the rest of them keep going. I realize I could just check each enemy but that's a WHOLE LOT OF WORK! I'm sure there's a much simpler way. Could someone explain from top to bottom. Note: "Using SDL"

##### Share on other sites
There are many ways to do what you are trying to do. Here is a complete working example thanks to Cone3d tut6

Cone3d Tut 6

##### Share on other sites
I've reviewed that and it doesn't help much. I don't want to keep adding the enemies off screen. I want them to have there own coordinates like Super Mario for example.

##### Share on other sites
"that's a WHOLE LOT OF WORK" - Not really.

Reading 'masters of Doom', it surprised me to learn that John Carmack would often try the most obvious solution first, and then realize a better way to do it later.

Also, the neat thing about software, once it works like you want, you dont have to deal with it anymore. In other words, the 'whoole lotta work' only needs done once by you.

And finally, I use an array of objects in a game im working on, and it isnt an extreme amount of work to get each to play along nicely.

There are more elegant solutions, and ironically, using a computer to find them is the wrong way (IMHO) to go about it. Try a piece of paper and a pencil instead.

Hope this helps :)

-Jason

##### Share on other sites
im not 100% sure im reading from the same page as you, but i'll say something based on what i think your talking about.

couldnt you make an 'enemy' structure, with the coordinates etc within it, and make an array of the enemy structures? Checking each enemy wouldnt be a whole lot of work since all you would need is a simple for loop.

for(i=0; i<MAX_ENEMIES; i++;)
{
if(enemyarray.right <= wall.left)
{
enemyarray.walk = false;
}
}

or something similar using your code and variables. I may have got completely the wrong idea about what your asking, if so, just let me know.

##### Share on other sites
Quote:
 Original post by AntiGuyFor example if I have 5 enemies walking forward and one bumps into the wall, the rest of them keep going. I realize I could just check each enemy but that's a WHOLE LOT OF WORK! I'm sure there's a much simpler way. Could someone explain from top to bottom.

that's pretty much how you do it:

void Engine::Update( float dt ){    for (int i = 0; i < numEnemies; ++i)    {        myEnemies.Update(dt);    }}

update each enemy every frame. update includes: locomotion updates, collision detection, AI, etc. some things, like AI can be timesliced so that it's not updated every single frame, but your physics and collision detection should be updated every frame. you'd be surprised at the amount of stuff a computer can do each frame...

-me

##### Share on other sites
Quote:
 Original post by AntiGuyFor example if I have 5 enemies walking forward and one bumps into the wall, the rest of them keep going. I realize I could just check each enemy but that's a WHOLE LOT OF WORK! I'm sure there's a much simpler way. Could someone explain from top to bottom.

nope, that's pretty much how you do it:

void Engine::Update( float dt ){    for (int i = 0; i < numEnemies; ++i)    {        myEnemies.Update(dt);    }}

update each enemy every frame. update includes: locomotion updates, collision detection, AI, etc. some things, like AI can be timesliced so that it's not updated every single frame, but your physics and collision detection should be updated every frame. you'd be surprised at the amount of stuff a computer can do each frame...

-me

##### Share on other sites
Ok, one way would be to make a struct or class (personally I use classes) and assuming the class is called enemy do somthing like:

class cEnemy{int x;int y;int velocity;};cEnemy  ObjectName[10];

And then you have 10 enemies that are completely seperate from each other and can have individual locations and speed.

If this isn't what you are talking about let me know, I'll try again ;)

I got it! Thx!

##### Share on other sites
just make sure you try and encapsulate the code as good as possible. what i mean is, let all your object's only care about themselves, and not about outside world. at the very least make the data variables private. for example, have your Enemy class have functions like this:

Update()
Render()
Do_Collision()

etc. i would personally try to keep Update() the only interface the world has to the object, IE, call Render() and Do_Collision() from inside of Update(). then you just call Update() each frame like the way Palidine showed.

1. 1
Rutin
34
2. 2
3. 3
4. 4
5. 5

• 12
• 14
• 9
• 9
• 9
• ### Forum Statistics

• Total Topics
633331
• Total Posts
3011398
• ### Who's Online (See full list)

There are no registered users currently online

×