Sign in to follow this  

XNA Firing and Storing Values in Lists

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

hi again. i am a little confused with adding items to Lists. I am writing a 2D Fortress game where the user controls a turret and fires bullets at incoming Sprites. at the moment I am trying to write the Game logic for bullets, here is an outline of what I want to do. 1/ Create a new bullet at the location of the barrel of the gun 2/ Set the Bullet shooting off in the direction the barrel is facing(i know the rotation) seemed easier than it was for me; here is what i am trying:
if(enough time has passed since you last fired)
{
    create a new Bullet at the position of the barrel
    get the rotation of the barrel and transform a vector to the currant rotation
    set the bullet moving in the direction at a given speed.
    !!** Add this bullet to the BulletList **!! //This is what I'm confused with
}
struct Bullet
{
      public Vector2 Pos;
      public Vector2 Dir;
      public void Update()
      {
          Pos = new Vector2(Pos.X + Dir.X, Pos.Y + Dir.Y);
      }
}

Bullet[] bullet;
List<Bullet[]> Bulletlist = new List<Bullet[]>();
private void FireBullet()
{            
   if (timeScinceFire > FireDelay)
   {
         bullet[BulletList.Count + 1].Position = //some code to get the position 
         bullet[BulletList.Count + 1].Direction= //Some code to get the vect & transform
         BulletList.Add(bullet[BulletList.Count + 1]);
   }
}
private void DrawBullets()
{
    foreach(Bullet bullet in Bulletlist)
    {
         bullet.Update();
         spriteBatch.Draw(tBullet, bullet.Pos, Color.White)
    }
}
However this will not work as it think it should. Problems: 1/ Bullet does not Update() and move forward; 2/ first bullet draws at (1,1) ALWAYS Can you guys see any problems?
bullet[BulletList.Count + 1] 
I don't like this bit. Im not sure why I just don't think it looks right

Share this post


Link to post
Share on other sites
Again, from the last topic, why are you using arrays here?

Bullet[] bullet;
List<Bullet[]> Bulletlist = new List<Bullet[]>();



Why not just a list of Bullets?

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



Your FireBullets method would then be, for example:

private void FireBullet()
{
if (timeScinceFire > FireDelay)
{
Bullet bullet;
bullet.Position = //some code to get the position
bullet.Direction= //Some code to get the vect & transform
BulletList.Add(bullet);
}
}



This isn't so much an XNA question as it is a general C# question around the usage of lists.

Share this post


Link to post
Share on other sites
Quote:
Original post by Andy474
However this will not work as it think it should. Problems:
1/ Bullet does not Update() and move forward;
2/ first bullet draws at (1,1) ALWAYS

Can you guys see any problems?


1) I would suspect this is because you have a List of Array items (kind of a monster of a multi-dimensional list). When you do the following:


foreach(Bullet bullet in Bulletlist)
{
bullet.Update();
...



It looks like it's calling .Update() on the array, not on each array item. I think you would need another loop going through the items of that, bah! Just use a single list like adt7 mentions. List<Bullet>.

2) Unsure of this but it would be somewhere in this code:


bullet[BulletList.Count + 1].Position = //some code to get the position
bullet[BulletList.Count + 1].Direction= //Some code to get the vect & transform



You would need to give us the code for setting the position but all you should need to do is set Bullet.Position = new Vector2<x, y> to set it somewhere. I would also suggest making sure to use normalized amounts otherwise you'll get some wonky speeds at different angles.

Share this post


Link to post
Share on other sites
Quote:
Original post by adt7
This isn't so much an XNA question as it is a general C# question around the usage of lists.


thanks :) I usually program in C++ i only started using C# a few weeks ago so I am a bit of a 'noob' you might say with Lists. I am a little sketchy with the .NET framework;

i removed the List of arrays as you suggested. now I have the problem that my bullet still wont Update(); properly as the vector is always the same


struct Bullet
{
public Vector2 Pos;
public Vector2 Dir;
public void Update()
{
Pos = new Vector2(Pos.X + Dir.X, Pos.Y + Dir.Y);
}
}
private void FireBullet()
{
if (timeScinceFire > FireDelay)
{
Bullet bullet;
bullet.Pos = new Vector2(myTurret.PosVect.X - 88, myTurret.PosVect.Y);
bullet.Dir = new Vector2(-2, 0);
myTurret.Ammo--;
Bulletlist.Add(bullet);
}
}

is this because of my code in Bullet::Update() or the way i am firing the bullets in FireBullet()?

Share this post


Link to post
Share on other sites
I can't see where the problem lies, although I'd personally write your update method as:

this.Pos += this.Dir;



The this keywords are probably redundant, they just help my thought process.

You'd be better off wraping up your Pos and Dir in properties, but it's probably a better to get it working before you think about that.

I wish I could help more, but I'm separated from my dev tools for the weekend so I'm unable to quickly code up something to test this.

Share this post


Link to post
Share on other sites
thanks for that :) looks much neater
this.Pos += this.Dir


i cannot see why this isn't working I have run it and added a breakpoint at this point of code and it executes. so the only conclusion I can see if that this.Dir is a value of (0,0) for some reason.

EDIT :: Although i wonder why i cant do this


private void DrawBullets()
{
foreach(Bullet bullet in Bulletlist)
{
bullet.Pos += new Vector2(-2, 0); //Error
spriteBatch.Draw(tBullet, bullet.Pos, Color.White);
}
}

Error 1 Cannot modify members of 'bullet' because it is a 'foreach iteration variable'

Share this post


Link to post
Share on other sites
aah thanks, ok so both pos, and Dir have the correct values. although i notice that bullet.Pos ALWAYS has the same value thats'd be my problem

Share this post


Link to post
Share on other sites
Try switching bullet to a class instead of a struct. Not ideal, but structs don't play nice with foreach loops and different solutions are needed.

Edit:

If that works, switch it back to a struct and try the following:

for (int i = 0; i < bulletList.Count; i++) 
{
Bullet bullet = bulletList[i];
bullet.Update();
spriteBatch.Draw(tBullet, bullet.Pos, Color.White);
bulletList[i] = bullet;
}


That is just off the top of my head, hopefully it will compile and run.

Share this post


Link to post
Share on other sites

public class Bullet
{
public Vector2 Pos;
public Vector2 Dir;
public void Update()
{
this.Pos += this.Dir;
}
}


I changed it to a class this has fixed the issue. I was just playing on my xBox and it occured to me you can edit member varibles in a foreach iteration now it works just great thanks a lot atd7

Share this post


Link to post
Share on other sites
Glad it worked out for you.

The problem with foreach iteration with over structs is that they are a value type, and so only the value of the variable is stored within the loop and any modifications don't effect the original.

It should be noted that structs will generally give you better performance than classes for things such as bullets, but it's unlikely to matter unless you are creating a collosal amount of them.

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this