EDIT: SOLVED PLEASE DO NOT RESPOND
I am adding to this rather than creating a new thread as it relevant to this.
So my problem is that I have successfully implemented my own container (a list) of a template type T (in this case it is a class Bullet), and I am able to access the nodes within this list but for some reason I am unable to edit the data :s
So at the moment, I have a ListOfT<Bullet> bulletRegister; in my Ship.h. When the fire() function is called, it does the following:
1 2 3 4 5 6 7 8 9 10
|
void Ship::fire() //fire a new bullet to the screen
{
canShoot = 0; //reset fire counter
cout<<"Shot has been fired!"<<endl;
//add bullet to register
Bullet b(xPos,yPos,xSpeed, ySpeed, true);
cout<<b.getXSpeed()<<"\t"<<b.getYSpeed()<<endl; //debug
bulletRegister.insertAfter(b);
}
|
Every time the game loop is run, the following is run:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
void Ship::calculatePosition()
{
...
//check through bullet register and update/delete where approriate
if (bulletRegister.size() != 0)
{
//cout<<"Size: "<<bulletRegister.size()<<endl;
bulletRegister.setToFirst(); //sets the list iterator to the first element
while(bulletRegister.currentForwardable()) //while there is a next object in the list
{
//check whether to update
checkBullet();
bulletRegister.currentForward(); //move iterator one node forward
}
checkBullet(); //check for this last bullet in register
}
}
|
So the above loops through the nodes in the list and calls the checkBullet function:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
//check if current bullet in list needs to be deleted
void Ship::checkBullet()
{
bulletRegister.getCurrent().updatePosition();
//the following three lines are debug to prove it does not update the position
cout<<"prev: "<<bulletRegister.getCurrent().getXpos();
bulletRegister.getCurrent().setXpos(bulletRegister.getCurrent().getXpos()+5);
cout<<" after altering: "<<bulletRegister.getCurrent().getXpos()<<endl;
//cout<<"Now checking: "<<bulletRegister.getCurrent().getTime()<<endl;
if (bulletRegister.getCurrent().shouldDelete())
{
bulletRegister.eraseCurrent();
cout<<"deleted\n";
}
}
|
Now bulletRegister.getCurrent().updatePosition(); should update the bullets position and time (and it does within the actual call) but as soon as control returns to the caller function (checkBullet() ), it does not seem to register that the data was changed.
To prove this further I manually altered the x position of the bullet and cout'ed the bullets position before and after. As you can see in the code there should be a difference of +5 in the after cout. There is not :(
I do not understand why this is not updating, I must be accessing the correct Bullet datatype because it cout's the correct position and speed of the bullet (before altering) but it is not actually altering the data itself.
Any ideas?
EDIT: SOLVED PLEASE DO NOT RESPOND
I solved by making a copy of each of the bullets variables, storing into a Bullet b object and using the list.setCurrent(b). Not the best and most practical way to do it but it works.