Game Development Community

Checking if an object exists or stopping onUpdateScene (solved)

by Ben Shive · in Torque Game Builder · 07/31/2006 (6:29 pm) · 7 replies

I've got a short little function as follows, which works perfectly well even if not the most efficient.

function t2dSceneGraph::onUpdateScene(%this)
{
if($zapper && $detShapes[$detShapes:currentShape]) {
%dist_x = $zapper.getPositionX() - $detShapes[$detShapes:currentShape].getPositionX();
%dist_y = $zapper.getPositionY() - $detShapes[$detShapes:currentShape].getPositionY();
%angle = mRadToDeg(mAtan(%dist_y, %dist_x)) - 90;
$zapper.rotateTo(%angle, 25);
}
}

The problem is when I run the level and then stop it, this function still keeps firing! Since the current shape no longer exists, it spams errors into the console log until the system runs out of memory or it crashes if I don't quit the program.

I either need to be able to check if the objects involved actually exist still, or to halt/clear the onUpdateScene when the level is stopped. Might not be a huge deal, but it's frustrating to always have to quit TGB.

#1
07/31/2006 (6:49 pm)
That happened to me as well when I was echoing anything within "onUpdateScene". Seems like it's constantly running even after you quit your application. There's nothing wrong with the code, it's the 'onUpdateScene' bit. I'm not sure how to stop onUpdateScene running without closing TGB, but I'll see if I can find anything.
#2
07/31/2006 (7:31 pm)
You can use isObject(%obj) to determine if an object still exists.
#3
08/01/2006 (3:15 am)
Thanks Ben V., using isObject for the if statement solved the problem.

I'm wishing again for a better search on these forums and TDN!
#4
08/01/2006 (3:40 am)
I also use game states to control what happens. A simple variable set to "menu", "pause", "play", "quit" will allow to specify events based on the state of the game.
#5
08/01/2006 (5:57 am)
By using isObject, isn't the computer still checking whether the object exists every update? If so, wouldn't this CPU usage accumulate after a while anyway? Or have I misunderstood?
#6
08/01/2006 (6:18 am)
There are a couple of things you can do depending on how your game is structured:
// psuedo coude
t2dscenegraph::onUpdateScene(%this)
{
  if (scenegraph == gameScene)
  {
    switch gameState
    {
     case "play":
       // do checks for stuff
     }
  }
}
That way you skip all of the checks if you're displaying a different scenegraph, and then only run the checks if the game is playing. Shouldn't cause too much of a hit on the CPU.
#7
08/03/2006 (12:04 pm)
Thanks for the suggestions guys, I dumped in an isObject call for now but I'm sure I'll be using the gameState suggestions when I get more than a test level going.