Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I need to have about 10 enemies in my game, can I use a list to spawn them all in?

This is what I was working on but it only spawns 1 enemy in the coordinates of the last added to the list.

Here are the parts from the main game class that I am working on:

public class Game1 : Microsoft.Xna.Framework.Game 
{
AI enemy;

List<AI> enemies = new List<AI>();

protected override void LoadContent()
{
  Model hero = Content.Load<Model>("Assets/model");

  enemies.Add(enemy = new AI(hero, new Vector3(0, 0, -550)));
  enemies.Add(enemy = new AI(hero, new Vector3(-580, 0, -400)));
}

protected override void Update(GameTime gameTime)
{
  for (int i = 0; i < enemies.Count; i++)
  {
    enemy.Update();
  }
}
}

Here is my AI class:

 public class AI
{
    public Model model;
    public Matrix world = Matrix.Identity;
    public Vector3 position;
    public Vector3 direction;
    public BoundingSphere collision;
    Matrix[] transform;

    public AI(Model m, Vector3 iPos)
    {
        model = m;
        position = iPos;
        direction = new Vector3(0, 0, -1);
        transform = new Matrix[m.Bones.Count];
        m.CopyAbsoluteBoneTransformsTo(transform);

        foreach (ModelMesh mesh in m.Meshes)
        {
            collision = BoundingSphere.CreateMerged(collision, mesh.BoundingSphere);
        }
    }

    public BoundingSphere getSphere()
    {
        return collision.Transform(world);
    }
    public void Update()
    {
        world = Matrix.CreateWorld(position, direction, Vector3.Up);
    }

    public void Draw(Matrix projection, Matrix view)
    {
        Matrix[] transforms = new Matrix[model.Bones.Count];
        model.CopyAbsoluteBoneTransformsTo(transforms);

        foreach (ModelMesh mesh in model.Meshes)
        {
            foreach (BasicEffect effect in mesh.Effects)
            {
                effect.EnableDefaultLighting();
                effect.Projection = projection;
                effect.View = view;
                effect.World = transforms[mesh.ParentBone.Index] * world;
            }
            mesh.Draw();
        }
    }
}

Thanks for any help.

EDIT

I have managed to get two enemies to spawn in but only one of them goes to the correct position, I will update again when I get it fully working.

Here is the code as it stands now, I made no changes to my AI class only on my game1 class:

public class Game1 : Microsoft.Xna.Framework.Game 
{

List<AI> enemies = new List<AI>();

protected override void LoadContent()
{
  Model hero = Content.Load<Model>("Assets/model");

  enemies.Add(new AI(hero, new Vector3(0, 0, -550)));
  enemies.Add(new AI(hero, new Vector3(-580, 0, -400)));
}

protected override void Update(GameTime gameTime)
{
  enemies[0].Update();
}

protected override void Draw(GameTime gameTime)
{
        for (int i = 0; i < enemies.Count(); i++)
        {
            enemies[i].Draw(camera.projection, camera.view);
        }
}
}

Hope this helps any one and I will edit once again when I get it fully working.

EDIT

I have posted an answer below containing the working code.

share|improve this question

closed as too localized by Byte56, Maik Semder, Patrick Hughes, bummzack, Tetrad Apr 5 '13 at 19:55

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

2  
This starts as a legitimate question, but quickly turns into "how do I fix my code?". I'm voting to close as too localized. –  Byte56 Apr 4 '13 at 17:06
    
I am only trying to get peoples advice as I can't fix it myself and I am stuck. I didn't specifically ask for code only help with regards to my problem. –  RonnieB555 Apr 4 '13 at 17:12
    
Being a beginner is fine. And asking basic questions is fine. However, make sure you ask game development related question here, and programming related questions at stackoverflow. If you find that you need to post a majority of a class, it's probably too localized. Learn to use the debugger. Read up on the FAQ. The way you're asking your question, the only way to answer it is to point out what you're doing wrong in your code. Because of course the answer to "Can I use a list to spawn enemies?" is "Yes". Other than that, you just have a "fix my code" question. –  Byte56 Apr 4 '13 at 17:25
    
I got two enemies spawning now, but only 1 spawns in the correct position. I will edit my original post to show anyone else that stumbles upon this thread what to do. –  RonnieB555 Apr 4 '13 at 17:35
    
If you have an answer to your question, you post it as an answer. Don't update the question with an answer. –  Byte56 Apr 4 '13 at 17:37
show 2 more comments

3 Answers 3

Take a look at:

enemies.Add(enemy = new AI(hero, new Vector3(0, 0, -550)));
enemies.Add(enemy = new AI(hero, new Vector3(-580, 0, -400)));

Now imagine if I wrote the same thing like this:

enemy = new AI(hero, new Vector3(0, 0, -550));
enemies.Add(enemy);
enemy = new AI(hero, new Vector3(-580, 0, -400));
enemies.Add(enemy);

See your problem?

You're setting enemy to a new enemy. Then you're adding it to the list. Then you're changing enemy to make another new enemy, and adding it to the list again.

Now you have two of the same object in the list. Try this:

enemies.Add(new AI(hero, new Vector3(0, 0, -550)));
enemies.Add(new AI(hero, new Vector3(-580, 0, -400)));

Then, your for loop is nonsense. You're just updating the same enemy every time.

Then you can iterate over all your enemies like this:

foreach(AI enemy in enemies) {
    enemy.update();
}
share|improve this answer
    
Doing it that way means that it wont update the way that I currently have it. It needs to run enemy.update(); for it to be drawn but by not creating an enemy it has no value. I hope I explained that correctly –  RonnieB555 Apr 4 '13 at 17:03
    
That foreach does the same, it only spawns in the last one that was inputted to the list. –  RonnieB555 Apr 4 '13 at 17:09
1  
@RonnieB555 I think you need to take a look at some basic programming tutorials again. Learn a bit more about control flow. Specifically how loops work, and what objects are. Your issues are very basic programming issues. They don't have anything to do with game development. –  Byte56 Apr 4 '13 at 17:12
    
Sorry I should have stated that I am a student and still learning, I am not very familiar with this and was just looking for some general help with regards to my problem. –  RonnieB555 Apr 4 '13 at 17:15
add comment

In your update method you are calling Update() on enemy. Now I don't know the syntax of C#, but you need something like enemies[i].Update()

share|improve this answer
    
When I tried this it only spawns in one enemy, using the coordinates from the last one inputted into the list. –  RonnieB555 Apr 4 '13 at 17:06
    
This did end up helping thanks for the answer :) I would upvote you but I don't have the rights to since creating this thread :( –  RonnieB555 Apr 4 '13 at 17:59
add comment

Ok I got it working, I was forgetting to update for every enemy.

Here is the final code:

public class Game1 : Microsoft.Xna.Framework.Game 
 {

   List<AI> enemies = new List<AI>();

   protected override void LoadContent()
    {
     Model hero = Content.Load<Model>("Assets/model");

     //ADD MORE ENEMIES HERE
     enemies.Add(new AI(hero, new Vector3(0, 0, -550)));
     enemies.Add(new AI(hero, new Vector3(-580, 0, -400)));
    }

    protected override void Update(GameTime gameTime)
    {
     //HERE UPDATE FOR EVERY NEW ENEMY
     enemies[0].Update();
     enemies[1].Update();
    }

    protected override void Draw(GameTime gameTime)
    {
      for (int i = 0; i < enemies.Count(); i++)
      {
        enemies[i].Draw(camera.projection, camera.view);
      }
    }
}
share|improve this answer
    
thats what vince said already. im sure this answer will be real useful if anyone wrote exactly the same code as you –  GameDev-er Apr 4 '13 at 17:50
    
Yes it was very helpful but initially it didn't fit with what I had, I would upvote him if I could but since creating this thread I have lost the ability to do so :( –  RonnieB555 Apr 4 '13 at 17:58
    
A simpler way of updating each enemy without calling each one is doing this: foreach(AI newAi in enemies) { newAi.Update(); } –  Battle_Pasture Apr 4 '13 at 22:24
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.