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

Need Help, creating multiple objects in List, with XML templated object

Last post 6/12/2009 4:18 PM by Warren Liang. 2 replies.
  • 6/12/2009 9:30 AM

    Need Help, creating multiple objects in List, with XML templated object

    I have a list of bullets that get shot from one side of the screen to the other. Whenever i "shoot" the cannon,
    i use XML calls (reader, writer, ...) to create a ball from file, and add it to my list

                    GameObject newBall;
                    newBall = Content.Load<GameObject>(@"Data\\Ball");
                    newBall.Alive = true;
                    newBall.Position = robot.Position - newBall.Center;
                    newBall.Velocity = new Vector2(
                       (float)Math.Cos(robot.Rotation),
                       (float)Math.Sin(robot.Rotation)) *10.0f;               
                    cannonShots.Add(newBall);   // cannonShots is my List that contains the cannon Balls

    I do it this way, so i can set the properties of each specific type of ball, weight, sound effects, etc,
    without having to hard code them into my code. (Little balls, big balls, etc) (little bang, big bang, explosions, etc)

    What i have noticed is that, ALL the cannonballs are pointing to the same memory location.
    I may have multiple "cannonballs" in my list, but they are all pointing to the exact same instance.
    So each time it updates, depending on how many instances there are in the list, it will update that
    much faster. So if there are 3 cannonballs in the list, it updates 3 times.

    And when my draw function draws them onto the screen, there is only 1 ball that gets drawn, despite my list
    having multiple instances of cannonballs.

    Note, i am learning XML, and applying it towards the 2D tutorial found on the site.
    I know my XML reader writer, etc is working fine, because everything compiles, and the data loads properly, when
    the content.load function gets called.

    How can i load unique objects from XML, and also make sure they aren't all pointing to the same object?

    Thanks so much for your help
    If it ain't broke, it doesn't have enough features yet - Scott Adams, The Dilbert Principle
  • 6/12/2009 3:26 PM In reply to

    Re: Need Help, creating multiple objects in List, with XML templated object

    Content.Load only loads assets once and returns a reference to the same instance for every subsequent Load call. You can override this behavior by subclassing the ContentManager to not use reference counting (see here for how to do that) or you can build a cloning option into your game object that copies the properties into a new instance.
  • 6/12/2009 4:18 PM In reply to

    Re: Need Help, creating multiple objects in List, with XML templated object

    Thanks
    If it ain't broke, it doesn't have enough features yet - Scott Adams, The Dilbert Principle
Page 1 of 1 (3 items) Previous Next