MarkForDelete/Kill resolve not working with projectile array
by rwillis · in Torque X 2D · 08/31/2007 (9:06 am) · 0 replies
I'm creating a projectile every time the player shoots and I store each one in an array. But when one does a kill resolve and I pause the game and then unpause it, then a lot of times at least one of the projectiles doesn't continue on moving. If I turn off Kill resolve then it works every time though. Also if I use MarkForDelete in a collision delegate instead of using TXB kill resolve, then it still messes up. I think it has something to do with killing the projectile which maybe makes it so cloning it messes it up or something (because I noticed there's an entry "CloneIgnore" for when you do MarkForDelete).
Here's my code:
The way I can reproduce this easily:
Shoot one projectile which hits enemy and calls Kill resolve and destroys itself. Then shoot 2nd projectile, pause game while projectile is in midair, and then unpause game. That 2nd projectile will now be stuck in midair without it's velocity being reset so that it can continue on.
Note: If I don't use Kill resolve or MarkForDelete then everything works fine (but then of course my projectiles don't disappear when they hit something).
Has anyone else had this problem?
Edit: It looks like when I delete a projectile, the next projectiles information gets moved down a index in the array or something. Anyone else have a technique for creating projectiles that can be paused?
Edit: Right now I have a quick solution which is to just set the projectile to "visible = false" and "collisionsEnabled = false". But I'd still need to know how to fix them problem which is that: it looks like an index in the array is getting deleted when using "markfordelete" on it causing all the other indexes to slide forward somehow or something.
Here's my code:
public void Fire()
{
if (i == 10)
{
i = 0;
}
projectile[i] = TorqueObjectDatabase.Instance.CloneObject<T2DSceneObject>("laserTemplate");
projectile[i].Position = SceneObject.Position;
projectile[i].Rotation = SceneObject.Rotation;
projectile[i].Layer = SceneObject.Layer + 1;
projectile[i].Physics.VelocityX = projectileVelocity * (float)Math.Cos(MathHelper.ToRadians(projectile[i].Rotation));
projectile[i].Physics.VelocityY = projectileVelocity * (float)Math.Sin(MathHelper.ToRadians(projectile[i].Rotation));
TorqueObjectDatabase.Instance.Register(projectile[i]);
i++;
}
static public void Pause()
{
for (int e = 0; e < 10; e++)
{
if (projectile[e] != null)
{
Debug.WriteLine("Pause");
tempX[e] = projectile[e].Physics.VelocityX;
tempY[e] = projectile[e].Physics.VelocityY;
projectile[e].Physics.VelocityX = 0;
projectile[e].Physics.VelocityY = 0;
}
}
}
static public void UnPause()
{
for (int a = 0; a < 10; a++)
{
if (projectile[a] != null)
{
Debug.WriteLine("UnPause");
projectile[a].Physics.VelocityX = tempX[a];
projectile[a].Physics.VelocityY = tempY[a];
}
}
}The way I can reproduce this easily:
Shoot one projectile which hits enemy and calls Kill resolve and destroys itself. Then shoot 2nd projectile, pause game while projectile is in midair, and then unpause game. That 2nd projectile will now be stuck in midair without it's velocity being reset so that it can continue on.
Note: If I don't use Kill resolve or MarkForDelete then everything works fine (but then of course my projectiles don't disappear when they hit something).
Has anyone else had this problem?
Edit: It looks like when I delete a projectile, the next projectiles information gets moved down a index in the array or something. Anyone else have a technique for creating projectiles that can be paused?
Edit: Right now I have a quick solution which is to just set the projectile to "visible = false" and "collisionsEnabled = false". But I'd still need to know how to fix them problem which is that: it looks like an index in the array is getting deleted when using "markfordelete" on it causing all the other indexes to slide forward somehow or something.
About the author