XNA Creators Club Online
Page 1 of 1 (10 items)
Sort Posts: Previous Next

Tracking multiple objects

Last post 09-02-2008 3:07 PM by Lord of Malvik. 9 replies.
  • 08-31-2008 7:19 PM

    Tracking multiple objects

    I'm making a strategy game where you (of course) can make several units of each unit. Does anyone know a way to keep track of each unit without having to create a new a new instance of an object for each unit. Would an array of the object be an option? (Tanks[ = new StrategyUnit)

    Thanks!


  • 08-31-2008 7:59 PM In reply to

    Re: Tracking multiple objects

    Do you mean that you want to have squads of tanks that act as a single unit?

    If you don't want to create a new object instance each time (and I don't see why not) you could store the position of each unit as an array and use mesh instancing to draw them (if the game's 3D, and there is a sample on this site).

    Could you explain your problem in more detail?


  • 08-31-2008 8:22 PM In reply to

    Re: Tracking multiple objects

    If you don't want to allocate at runtime, you can pre-allocate at load time.

    However, if it's a strategy game where you spawn a few new units per minute, and kill a few units per minute, then allocating them with new is not so bad. I would certainly don't mind calling new once per unit instance in a game I wrote.


    Jon Watte, Direct3D MVP kW X-port 3ds Max .X exporter kW Animation source code
  • 08-31-2008 8:48 PM In reply to

    Re: Tracking multiple objects

    No, I don't want a squad of tanks to act as a group, but several tanks to act as individual units.

    by making several instances, did you mean in an array like this:

    int tanksCount = -1 
    int oldTanksCount 
    protected override void Update(GameTime gameTime) 
        if(Keyboard.GetState().IsKeyDown(Keys.Space)) 
        { 
            tanksCount++ 
        } 
     
        if(oldTanksCount != tanksCount) 
        { 
            Tanks[tanksCount] = new TanksControl 
        } 
     
        if(tanksCount => 0) 
            { 
                for (i = 0; i < tanksCount; i++) 
                { 
                    Tanks[i].Update(); 
                } 
            } 
        oldTanksCount = tankCount; 
        base.Update(gameTime); 
    I'm not near a computer with XNA right now, so I'll won't be able to test it, but I hope you can give me feedback on the code above.

     Edit: I tried this, but it seems like I need 'Indexes' to make classes work like arrays. I read about it in the help file, but still isn't quite sure on how to use it in my programm. I would have exprimented on it, but i'll have to go to school, so I hoped anyone knew something about this. It would save me alot of time :)

    Again, Thanks


  • 09-01-2008 7:51 AM In reply to

    Re: Tracking multiple objects

    How about this...

     

    List<TanksControl> tankList = new List<TanksControl>(); 
     
    protected override void Update(GameTime gameTime) 
        if (Keyboard.GetState().IsKeyDown(Keys.Space)) 
        { 
            tankList.Add(new TanksControl()); 
        } 
     
        foreach (TankControl tank in tankList) 
        { 
            tank.Update(); 
        } 

    That should pretty much do what you said above, but is more robust. You might want to check for a single space press first though because you could end up creating many tank controls at once.
  • 09-01-2008 1:59 PM In reply to

    Re: Tracking multiple objects

    Thanks for the reply, I'll edit the post when I've tried it out. And I do check for single space presses, but I was too lazy to add them in the code.

    Edit: Everything seems to work fine, except ont thing. In the Draw method in the TanksControl class I draw from a sprite batch. I then need to feed it with a Texture a Rectangle and a Color. It looks like this:

    public void Draw(SpriteBatch Batch) 
        Batch.Draw(spriteTexture, spriteRectangle, spriteColor); 
    Both the Rectangle and the Color works fine, but the Texture gets the value 'null', when it should be Texture2D. The rest of the code releated to the Texture looks like this:

    class TanksControl 
        ... 
        private Texture2D spriteTexture; 
     
        public void Load(ContentManager Content, string Asset, Vector2 SpritePosition, Color SpriteColor) 
        { 
            ... 
            spriteTexture = Content.Load<Texture2D>(Asset); 
            ... 
        } 
        ... 
     
    class Game1 
        ... 
        protected override void LoadContent() 
        { 
            ... 
            foreach (SpriteSetup Tanks in tanksList) 
            { 
                Tanks.Load(Content, "Sprites\\Tanks", circlePosition, circleColor); 
            } 
        ... 
        } 
    ... 
  • 09-01-2008 8:33 PM In reply to

    Re: Tracking multiple objects

    The LoadContent() method is only called when the whole game is run for the first time!

    So underneath the code that adds the tank you need to call it's load method after you've added it. Alternatively, you could make the Texture2D static in the TanksControl class if every tank uses the same texture. This means you only need to load the texture once.

    Hope that helps!

  • 09-01-2008 9:01 PM In reply to

    Re: Tracking multiple objects

    I tried to move the Tanks.Load into the Update() method, in it works fine... I think, the Tanks is drawn at the same position all the time so it's impossible to see if there are more than one tanks. I'll make them go invisible after a while so I confirm that it really works, but right now I'm too tired to do anything about it right now. (I've just bought Castle Crasher and Bionic Commando: Rearmed).

    Thanks for the help everybody!

  • 09-01-2008 10:59 PM In reply to

    Re: Tracking multiple objects

    You also need to call spriteBatch.Draw() for each tank; calling it once in the draw method will only draw 1 tank.

    You need to loop over the TankList and draw each tank like you would a single one.


  • 09-02-2008 3:07 PM In reply to

    Re: Tracking multiple objects

    I know, I've already done this. Thanks anyway :)
Page 1 of 1 (10 items) Previous Next