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

I Want to put my Objects (instances of Classes) into an array, how do I do this?

Last post 4/17/2007 6:59 PM by gamevial. 2 replies.
  • 4/16/2007 7:47 PM

    I Want to put my Objects (instances of Classes) into an array, how do I do this?

    Hi All,

    I'm a dabbling Shockwave Developer, and I'm very new to C# so please humour me :). I also have poor technical jargon skills, and am thus  more than likely to be misusing it. I appologise in advance.

    I'm trying to store instances of my bullet class into an array, so that I can generate them on the cuff:

    Here is my bullet spawning code.

     

    public Bullet bullet;

    public Bullet[] myBullets;//Array for instances?

    public Bullet makeBullet(Vector3 pos,Vector3 rot)

    {

    bullet = new Bullet();

    bullet = bullet.Setup(myGens, this,pos, rot);

    myBullets[1] = bullet;//bullet;//problems!

    return (bullet);

    }

    .. And Here is my bullet class, working fine barr the fact that only one lonely bullet is currently working.

    public class Bullet

    {

    private Vector3 pos;

    private Vector3 rot;

    private Vector3 velocity;

    public Model pmod;

    generics myGens;

    Game1 myGame;

    public Bullet Setup(generics moo, Game1 game,Vector3 nupos,Vector3 nurot){

    myGens = moo;

    myGame = game;

    pmod = myGame.modLoad("Content\\Models\\bullet");

    pos = nupos;

    rot = nurot;

    velocity = myGens.cosine3D(rot) * 100;

    rot.X -= 67.5f;

    return (this);

    }

    public void UpdateMe()

    {

    pos += velocity;

    draw();

    }

    public void draw()

    {

    myGens.sussaModel(pmod, pos, rot);

    }

    }

    }

    I have this feeling that the answer will be enbaraissingly simple, but it has eluded me thus far.

    Cheers,

    James (Developer, gamevial)

  • 4/16/2007 9:04 PM In reply to

    Re: I Want to put my Objects (instances of Classes) into an array, how do I do this?

    Answer
    Reply Quote
    Hi James,

    Your array of bullets looks like it's a null pointer. Before you can start inserting bullet objects into it, you need to allocate some memory to it using new!

    myBullets = new Bullet[ 100 ];      // allocates enough space for 100 bullet objects into your bullet array

    It's also worth noting that in C#, arrays use zero-based indexing, so the line:
        myBullets[1] = bullet;
    would actually insert a bullet object into the second element of your bullet array.

    Rather than using a plain old array, you might also like to play around with the List<> generic collection, found in the System.Collections.Generic namespace. The primary benefit of using a List over a plain array is that you don't need to worry about allocating the correct amount of space up-front for how many bullets you need, as Lists can grow themselves dynamically.

    I'd change myBullets to be a:

    List<Bullet> myBullets = new List<Bullet>();

    To add a bullet:
    myBullets.Add( bullet );

    Hope this helps! :)

    Cheers
  • 4/17/2007 6:59 PM In reply to

    Re: I Want to put my Objects (instances of Classes) into an array, how do I do this?

    Thanks

    Worked first time!

Page 1 of 1 (3 items) Previous Next