Game Development Community

Goto in TorqueScript

by Samaursa · in General Discussion · 07/22/2009 (8:37 am) · 3 replies

First, I would like to say that I know goto statements have their pros and cons.

I use goto statements in only a few rare scenarios, and one of them is cleanup. In a function, if I am creating new objects, I have a few lines of cleanup code which deletes the objects after the function has been executed.

When there are errors caught by the function, it returns (-1 for example) and cleans up any objects it has created. Without the goto statement, I'll either have to create another function (which will clutter my other functions) or copy and paste the cleanup code at every error catch OR I use goto statements to go to the cleanup part of the code. Is there something similar to goto in torquescript?

Thank you.

#1
07/22/2009 (11:34 am)
Nope, no goto sentence that Im aware of. And although you seem to know what you are doing, I think is always better not to use such thing.
#2
07/22/2009 (7:26 pm)
The alternative that I've always used in pretty much any language is a do-while loop with a false condition at the end. Then you use break statements to exit out. For TorqueScript, this would be as follows:

%success = false;
do {
   %error = somefunc();
   if (%error) break;

   %error = otherfunc();
   if (%error) break;

   %success = true;
} while (false)

if (!%success) {
    doCleanup();
}
#3
07/23/2009 (8:08 am)
That is a very good alternative, thanks!