Game Development Community

SimObjects can't be deleted in script anymore?

by William Hilke · in Torque 2D Beginner · 02/12/2013 (10:42 pm) · 5 replies

In the new codebase there is a guard added to SimObject::deleteObject()
// Sanity!
    AssertISV( getScriptCallbackGuard() == 0, "SimObject::deleteObject: Object is being deleted whilst performing a script callback!" );

the guard is set/unset in Con::execute() with this:

object->pushScriptCallbackGuard();

      SimObject *save = gEvalState.thisObject;
      gEvalState.thisObject = object;
      const char *ret = ent->execute(argc, argv, &gEvalState);
      gEvalState.thisObject = save;

      object->popScriptCallbackGuard();

Isn't that basically setting a guard around every script function called? How are we to delete SimObjects now?

#1
02/13/2013 (4:48 am)
They can still be deleted. That guard was put into place because a script function will still execute even when its object has been deleted. That would cause a crash every time. Running in debug mode will throw the Assert to let you know that is about to happen, so you can fix your script code.
#2
02/13/2013 (9:07 am)
So what is the correct way to delete SimObjects from TorqueScript then?
#3
02/13/2013 (9:37 am)
I understand what's going on now, not sure why I wasn't seeing it before.

The guard is only on if trying to delete a SimObject from within the execution of a script function of that same SimObject.

You can ignore my other question :).

#4
02/13/2013 (9:50 am)
Cool. Glad you got it.
#5
02/13/2013 (6:17 pm)
I guess if a SimObject needs to suicide, you should schedule a call to delete. That way the delete will happen outside the function call, when the event queue is evaluated.