Game Development Community

Object delete order (camera class)

by Maurice Ribble · in Torque Game Builder · 12/17/2006 (5:22 am) · 2 replies

I've used the camera code described in this tutorial to make it easier to manage a camera system.
tdn.garagegames.com/wiki/Torque_2D/GenreTutorials/PlatformerCamera

The basic way it works is that the camera is a dynamically created ScriptObject. You then attach this to a target that it tracks (I attach the camera to the player. I called createCamera() in playerShip::onLoadLevel(). I call camera.update() in t2dSceneGraph::onUpdateScene().

Everything works fine except when I attach the Torsion debugger I get thousands of errors when I close the level basically saying that the camera class is still running, but that the player class has been deleted. It seems that onUpdateScene still happens long after player has been deleted. What I want to do is force the camera class to be deleted and stop calling onUpdateScene when I delete the player sprite object. How do I do that? Or is there some other solution?

#1
12/17/2006 (9:46 am)
The first thing to note is that onUpdateScene will get called on any open scenegraphs (the levelbuilder has its own too) since you are using onUpdateScene on the entire t2dSceneGraph class. You should name your own scenegraph and use onUpdateScene on that.

You could also put a boolean in somewhere that will keep track of whether or not your game is in the levelbuilder. Or, you could check if the camera exists with isObject before calling methods on it.

Hope that helps!
#2
12/17/2006 (10:58 am)
The bit about onUpdateScene getting called by any scenegraphs makes perfect sense, but that was what I was forgetting to account for. I've restructured my code it now works nicely with the debugger.

Thanks Tom. You seem to be answering all my questions. Hopefully I get the hang of this scripting thing pretty soon. I was a low level C programmer for the past 5 years (wrote OpenGL drivers). All this high level scripting sure makes coding go faster, but it takes a different mindset.

Thanks again!