Game Development Community

Declaring Variables

by Vern Jensen · in Torque Game Builder · 08/30/2006 (1:40 pm) · 3 replies

Okay, this may seem like a somewhat bizarre request, but the programmer in me would like the option to declare variables before they're used. The main purpose is debugging, and solid code. I can't count the number of times I've done something like:

%myValue

when I meant to use

$myValue

or I mistyped one letter, so the variable I was accessing didn't exist prior to my reading it. Of course, Torque gives no errors, compile-time or run-time for things like this, because it's perfectly legal to introduce a new variable at any point.

I'm not asking you to force this on all users. I'm asking for an *option* where, if I specify, then the engine *requires* variables to be declared before they're used, and gives an error if they're not. So for globals:

Variable $myGlobal;

and for locals:

function myFunction()
{
Variable %myLocal, %n, %row, %col;

// Do stuff with those variables here.
}

Then when I mis-type $myLocal instead of %myLocal, or I mis-type one letter, I get a compile-time error.

I hope this request doesn't seem like a waste of time. It would be SO helpful when debugging, and it would also be very useful to have all my globals declared in one place, without having to assign them values. Just declaring they exist.

#1
08/30/2006 (2:07 pm)
$Con::warnUndefinedVariables = 1; //warn us when we access undefined variables

:)
#2
08/30/2006 (2:40 pm)
Oh wow, now thats a good tip to remember too.
#3
08/31/2006 (12:03 pm)
Hmm, that would *seem* to be pretty handy in theory, but in practice, there seem to be quite a few problems in scripts that come with Torque that cause my log window to be flooded with errors not related to my project.

The most common and annoying is

*** Accessed undefined variable '$instantGroup'

This gets flooded in the window at the very start of my program, and continues to get flooded as I run it. Another one I saw was:

*** Accessed undefined variable '$fps::real'

Another was:

Variable referenced before assignment: %caption

There are others as well, but really, the main trouble-maker is $instantGroup, as that continues to be flooded the entire time, making it very difficult to see when any of *my* variables are in error, and making this a pretty much useless debugging tool. If only $instantGroup got fixed, that would perhaps make this helpful.

OR, alternatively, if I could specify WHICH files errors were reported for (i.e. my own only, and not Torque's), then that would also make this feature useful. But as it is, Torque's errors are flooding the log so I can't see my own easily.

This is on Mac 1.1.0.