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
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
#2
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.
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
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.
09/24/2011 (4:10 pm)
Quote: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).
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.
Quote:I see what you're saying now about finding the location of a mistyped variable. Hmm...
"isDefined()" Won't really work for me, this is for cases where I accidently mispell a variable name.
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
"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.
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.
Associate Michael Hall
Distracted...
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.