Game Development Community

Torquescript-only based callbacks

by Ty Schacht · in Torque Game Builder · 06/30/2005 (7:18 am) · 0 replies

I am wishing to create some torquescript-only based callbacks. In other words, I want to define a method on a scriptobject which arbitrarally attempts to call a callback function on objects passed into it. Then, i want to create groups of scriptobject's which may or may not implement the callback function. If they contain the callback function, it will execute it. If they don't contain the callback function, the call is not made. The problem i'm running into is if my scriptobject does not implement the callback function, i get an error in the console stating something to the effect that simobject method does not exist (which is correct, i didn't define the callback method on that particular object). As I see it, there are 4 possible approaches to this solution:

1) In the logic that fires the callback method, do some 'reflection/introspection' on the target scriptobject to determine if the callback function is defined on that object. If so, call it. If not, ignore it. This is my desired solution, as i'd only have to code the 'reflection/introspection' in the places that actually call the method. However, i've searched and not found a clean way to test for function existence on a scriptobject.

2) Have all my game scriptobjects inherit from a base class scriptobject where all callbacks are defined, but are empty. Then, if I override the callback method in a child object, that method will be the one called. If i don't override it, the parent's empty method will happily be called. The downside to this is that due to namespace restrictions, you can only have 3 levels of inheritance in base torquescript (IIRC from reading the huge post by Bryan Edds and company). This would essentially waste a level of that inheritance.

3) Define a boolean on each simobject that says if the callback function exists. So, if I had a callback function named 'onColorMerge()', I would also have a boolean attribute named 'onColorMergeExists' set to true. Then the logic would be:

if (%obj.onColorMergeExists) {
     %obj.onColorMerge(..........);
}

I don't like this solution as it feels kludgy to have to hang extra attrs on objects implementing the callback, and could be easy to forget to set the boolean on new objects.

4) Code each empty callback into all relevent scriptobjects. Then, the method could be called but would be empty. This is too much extra hassle.

Anyone have any thoughts ideas. I'd especially like to hear an elegant solution to implement option '1' above.

Thanks in advance!