Game Development Community

Is onWorldLimit thread safe?

by Eyaly · in Torque Game Builder · 07/27/2008 (9:01 am) · 5 replies

Function objKKK::CreateShips(%this)
{
%maxShip = 50;
%speed = 20;

for( %i=0;%i<%maxShip;%i++ )
{
%wpnmine = %this.cloneWithBehaviors();
%wpnmine.position = getRandom(-40,40) SPC getRandom(-30,30 );
%wpnmine.rotation = getRandom(0,360);
%wpnmine.setWorldLimit( kill,"-50 -37 50 37",true );
%wpnmine.setCollisionActive( true,true );
%wpnmine.setCollisionPhysics( false,false );
%wpnmine.visible= true;
%wpnmine.setLinearVelocityPolar( %wpnmine.rotation,%speed);
}

// txtCounter = t2dTextObject
txtCounter.text = %maxShip;
}

function objKKK::onLevelLoaded( %this )
{

%this.CreateShips();
}

function objKKK::onWorldLimit(%this, %mode, %limit)
{

%curCnt = txtCounter.text;
%curCnt = %curCnt - 1;
txtCounter.text = %curCnt;
}

#1
07/27/2008 (9:10 am)
When i run this code in %speed set to low speed like( 20 )
the code will update the counter correctly.
if i increase the %speed to 200 txtCounter.text will show some minus number
its seems that sometime onWorldLimit happend more then 1 time per one object when %speed is high.

to run this code please do:
1. place object on TGB scene and set class name to objKKK ( class name );
2. place a new text object on TGB scene and set its name to txtCounter.

copy the following code and run it.

increase the speed to 200 and you will see the problem.

thanks



Function objKKK::CreateShips(%this)
{
%maxShip = 50;
%speed = 20;

for( %i=0;%i<%maxShip;%i++ )
{
%wpnmine = %this.cloneWithBehaviors();
%wpnmine.position = getRandom(-40,40) SPC getRandom(-30,30 );
%wpnmine.rotation = getRandom(0,360);
%wpnmine.setWorldLimit( kill,"-50 -37 50 37",true );
%wpnmine.setCollisionActive( true,true );
%wpnmine.setCollisionPhysics( false,false );
%wpnmine.visible= true;
%wpnmine.setLinearVelocityPolar( %wpnmine.rotation,%speed);
}

// txtCounter = t2dTextObject
txtCounter.text = %maxShip;
}

function objKKK::onLevelLoaded( %this )
{

%this.CreateShips();
}

function objKKK::onWorldLimit(%this, %mode, %limit)
{

%curCnt = txtCounter.text;
%curCnt = %curCnt - 1;
txtCounter.text = %curCnt;
}
#2
07/27/2008 (10:16 am)
After some analyze iv discovered that the onWorldLimit function can be invoked twice because it can
collide on both sides for example TOP LEFT or TOP RIGHT and not only one side like TOP or BOTTOM...

the question is how do i prevent it to happen if kill is trigger on first collide?

thanks
#3
07/27/2008 (10:37 am)
You need to delete your object with safedelete, then it doesn't matter if its called once or twice. Actually, even if it was only called once you would still need to use safedelete since you are inside a callback.

function objKKK::onWorldLimit(%this, %mode, %limit)
{
   %this.safedelete();
}
#4
07/27/2008 (10:56 am)
Hi

Since i need to count all destroyed object's ( only once because i dont want the text object to go under 0 )
safedelete alone didnt helped, so i add this line %this.setWorldLimit(kill,"-50 -37 50 37",false);
for a moment this two lines work as i expecting to but i dont know if it a hack way or not.
i really try to avoid hack way so please tell me if i do it correctly or not.

thanks again

function objKKK::onWorldLimit( %this, %mode, %limit)
{
%this.setWorldLimit(kill,"-50 -37 50 37",false);
%this.safedelete();
}
#5
07/27/2008 (5:38 pm)
Thats fine, or just turn off everything...

%this.enabled = false, or, %this.setEnabled(false)

Will turn off rendering, collision, and probably world limit callbacks too.