Registering object already registered
by rwillis · in Torque X 2D · 09/01/2007 (8:46 pm) · 5 replies
I'm getting a weird occurence of this error when causing an explosion to happen once a missile hits (the explosion animation is mounted to the missile). I've tried hours debugging this and I still can't figure it out. Here's my code:
The first time the missile hits an enemy everything works fine. But the Second time a missile hits an enemy, I get the "Registering object that's already registered" error. The error occurs right when the program tries to register the explosion object the second time around. The weird thing is that the program thinks explosion is not registered (because it gives me the debug message above with value "= false") but yet it still gives me the error that it's already registered when trying to register it. I hope someone is following this. Anyone have an idea on what may be occuring?
Note: I was thinking it may have something to do with the mounting, so I took off mounting and it still happened
static public void Explosion()
{
explodeData = (T2DAnimationData) TorqueObjectDatabase.Instance.FindObject("StrikeAnimation");
explosion.AnimationData = explodeData;
explosion.OnAnimationEnd = OnAnimationEnd;
explosion.Visible = true;
explosion.Size = new Vector2(5,5);
explosion.Layer = 0;
if (explosion.IsRegistered == false)
{
Debug.WriteLine("projectile.IsRegistered = " + projectile.IsRegistered);
TorqueObjectDatabase.Instance.Register(explosion);
}
explosion.Mount(projectile, "rocketExplosion", true);
explosion.PlayAnimation(explodeData);
}The first time the missile hits an enemy everything works fine. But the Second time a missile hits an enemy, I get the "Registering object that's already registered" error. The error occurs right when the program tries to register the explosion object the second time around. The weird thing is that the program thinks explosion is not registered (because it gives me the debug message above with value "= false") but yet it still gives me the error that it's already registered when trying to register it. I hope someone is following this. Anyone have an idea on what may be occuring?
Note: I was thinking it may have something to do with the mounting, so I took off mounting and it still happened
About the author
#2
09/02/2007 (12:00 pm)
When the animation ends, I call MarkForDelete on explosion. Yea I also tried unRegister but it still thinks it's registered for some reason. So did you find a way around this? I was thinking about just making a particle effect instead of using an animation.
#3
09/02/2007 (12:04 pm)
I would recommend having an explosion template object and then cloning it when an explosion occurs. Then the clone can be deleted whenever you like and you do not need to register, unregister, and reregister the object, as this does not seem to be supported. When you register an object it only register it if it has no object id (which is then assigned), however, unregistering an object does not clear the object id field, hence, you can't unregister and then reregister an object. At least this is one reason, I was looking at the code where objects are registered and there are quite a few asserts. Also if you ever want more than one explosion at a time, templates + cloneing is the way to go.
#4
So I just changed it to the code below and am using a template object created using TXB instead and just cloning it, and it works fine.
So this works:
09/02/2007 (12:14 pm)
Thanks James.So I just changed it to the code below and am using a template object created using TXB instead and just cloning it, and it works fine.
So this works:
static public void Explosion()
{
Debug.WriteLine("Explosion");
explosion = TorqueObjectDatabase.Instance.CloneObject<T2DAnimatedSprite>("explosion");
explosion.Visible = true;
explosion.Size = new Vector2(5, 5);
explosion.Layer = 0;
explosion.OnAnimationEnd = OnAnimationEnd;
if (explosion.IsRegistered == false)
{
Debug.WriteLine("explosion.IsRegistered = " + explosion.IsRegistered);
TorqueObjectDatabase.Instance.Register(explosion);
}
explosion.Mount(projectile, "rocketExplosion", true);
explosion.PlayAnimation();
}
#5
Since C# is a managed language, you never actually have direct control of true "object deletion"--all you can do is mark the object to be considered for deletion by the C# garbage collection mechanism. In James' example, you aren't concerned with re-using a previously deleted/unregistered object, so you can bypass this "it's not really deleted until C# says so" consideration.
09/02/2007 (12:23 pm)
Just a touch of background theory to explain why James' version is best practice:Since C# is a managed language, you never actually have direct control of true "object deletion"--all you can do is mark the object to be considered for deletion by the C# garbage collection mechanism. In James' example, you aren't concerned with re-using a previously deleted/unregistered object, so you can bypass this "it's not really deleted until C# says so" consideration.
Torque Owner Frederic
I got the same problem and tried MarkForDelete = true and a direct Unregister call, but both do not seem to allow re-registration.
I am wondering whether the references are being kept in the database, thereby preventing garbage collection, or if it is just a bug/feature that keeps an object id and prevents object re-usage because the object might need some additional initialization after being unregistered/deleted in Torque X.