Game Development Community

Uninitialised Variables

by Jamie Crawford · in Torsion · 09/23/2011 (5:29 pm) · 4 replies

Hey,

So I have recently started using TorqueScript/Torsion I have had several hard to find problems due to simple typos, my hands are pretty stupid and don't do what they're told. Anyway I come from C++ where the strict compiler, providing I don't use overly similar variable names, kindly catchs these.
I am wondering if there is a setting somewhere eluding me that highlights or generates warnings for used but uninitialised variables?

Thanks -Jim

About the author

Recent Threads

  • Seams between tilelayers

  • #1
    09/24/2011 (11:38 am)
    I don't know about Torsion... but you can add this to your game's main.cs:
    // Show warnings about undefined or uninitialized script variables
    $Con::warnUndefinedVariables = 0;
    This will give you feedback in the console.

    You didn't say what engine you were working with, but one of the useful things added to Torque 3D was the isDefined() method which can be used as a sort of safety net in those instance where uninitialized variables could be a problem.
    #2
    09/24/2011 (3:54 pm)
    Thank you very much Michael, that is exactly what I was asking for.

    However its use raises other issues; Now my console being abused by
    "*** Accessed undefined variable '$instantGroup'"

    My googling shows this exact scenario happened a little over five years, the other guy did not get a solution to this secound issue.

    "isDefined()" Won't really work for me, this is for cases where I accidently mispell a variable name.

    #3
    09/24/2011 (4:10 pm)
    Quote:
    its use raises other issues; Now my console being abused by
    "*** Accessed undefined variable '$instantGroup'"

    My googling shows this exact scenario happened a little over five years, the other guy did not get a solution to this secound issue.
    Torque script doesn't really care if a variable is initialized, so the message is mostly console spam letting you know that variable is being accessed before it is set, in which case it results in a default of 0 or the variable is initialized to whatever just got passed to it. Usually undefined variables won't cause a problem, the problem is that in some cases your code logic will be looking for some other value or expecting it to already exist (a common trap).

    Quote:
    "isDefined()" Won't really work for me, this is for cases where I accidently mispell a variable name.
    I see what you're saying now about finding the location of a mistyped variable. Hmm...

    If Torsion doesn't have the option of parsing through a script(s) and tracking the value of a variable you would have to manually track it down. You may have to sprinkle in some trace(1)/trace(0) (true/false) statements to surround the problem code (to track program flow) and move $Con::warnUndefinedVariables closer to where the problem lies. Placing it in the main.cs results in a warning for every single un-initialized variable from startup and on.
    #4
    09/24/2011 (4:42 pm)
    That was fast!

    "Placing it in the main.cs results in a warning for every single un-initialized variable from startup and on."

    I tried that, it does get rid of warnings during initialisation however the troublesome messages appear throughout the lifetime of the app.
    You did give me an idea tho, I can surround the problem code like so.

    $Con::warnUndefinedVariables = 1;
    ***suspected badly spelled code***
    $Con::warnUndefinedVariables = 0;

    Which eliminates any background messages.

    Not a comprehensive solution but its certainly an improvement.