Game Development Community

Engine crashes on .delete(); function.

by Jimmy Spencer · in Technical Issues · 02/19/2007 (10:23 am) · 12 replies

As the title states, when I tell an object to delete(); it crashes the engine. Why and how can I resolve this?

I use the code like this:

function AlienRifle::OnCollision(%this, %col, %obj)
{
if(%col = %obj.client.player)
{
%this.delete();
$bullets++;
}
}

#1
02/19/2007 (12:56 pm)
Your deleting an object while the engine is using it... your deleting AlienRifle while Torque is calling your onCollision call. Instead do this...

function AlienRifle::OnCollision(%this, %col, %obj)
{
if(%col = %obj.client.player)
{
%this.schedule( 10, delete );
$bullets++;
}
}

... which will delay the deletion of the rifle till the next tick.
#2
02/19/2007 (1:37 pm)
Hm,
i think deleting an object inside a script OnEvent method via %this.delet() is okay,
so if this fixes things something else may be weird..
#3
02/19/2007 (6:31 pm)
For some reason, it still crashes, thanks for the idea anyway. Do you think it may be something else?
#4
02/20/2007 (10:38 am)
What is the assert? There are numerous asserts in SimObject::deleteObject, which is what you are calling in both cases.
#5
02/20/2007 (11:10 am)
Well even if I use AlienRifle.delete(); the engine still crashes. I'm not sure what the assert is. Here is the whole code:

datablock ItemData(AlienRifle)
{
category = "Weapons";
shapeFile = "~/data/shapes/stuff/AlienRifle.dts";
mass = 2;
friction = 2;
};

function AlienRifle::OnCollision(%this, %col, %obj)
{
if(%col = %obj.client.player)
{
%this.delete();
$bullets++;
}
}

... that's it.
#6
02/20/2007 (11:14 am)
Youre deleting the datablock. is that really what you want to do?
#7
02/20/2007 (12:04 pm)
^ What he said.
#8
02/20/2007 (3:35 pm)
Oh...wait... no I don't. How would I delete just the object that I collide with?
#9
02/21/2007 (1:45 am)
I'm not really sure but I'm guessing you want to pass the object along as a parameter in OnCollision(). Maybe %obj is that object already?
#10
02/21/2007 (12:24 pm)
Firstly, your OnCollison callback has a logic error. '=' is the assignment operator, so your if block will always evaluate to true unless the value of %obj.client.player is 0, or "".

Next, the function signature for the OnCollision callback is:
OnCollision( %this, %obj, %col )
%obj will be the object which has collided with %col, which has datablock %this. Knowing this, your function should look like the following:
function AlienRifle::OnCollision( %this, %obj, %col )
{
   if( %obj.client.player /* has a rifle already [your code does not do this check] */ )
   {
      %col.delete();
      $bullets++;
   }
}
That being said, you don't want to implement this functionality this way at all. Since this code gets executed on the server, $bullets will be constantly overwritten any time any player collides with an alien rifle. I highly recomend looking at 'starter.fps' and the scripts in 'server/scripts'.
#11
02/21/2007 (12:50 pm)
Thanks for the help. The code works perfect, and I understand what those mean now.
#12
02/21/2007 (2:45 pm)
Cool, glad it worked for you!