Finding out if a variable exists
by Ian Omroth Hardingham · in Torque Game Engine · 04/06/2002 (7:31 am) · 8 replies
Is there a function which tells you whether a variable already exists in the current namespace? (script, obviously).
i.e.
function blah()
{
%a = 1;
echo(%a.exists());
}
function blah2()
{
echo(%a.exists());
}
so blah would have 1 echo-ed whereas blah2 would have 0 echo-ed.
i.e.
function blah()
{
%a = 1;
echo(%a.exists());
}
function blah2()
{
echo(%a.exists());
}
so blah would have 1 echo-ed whereas blah2 would have 0 echo-ed.
About the author
Designer and lead programmer on Frozen Synapse, Frozen Endzone, and Determinance. Co-owner of Mode 7 Games.
#2
works.
I believe, I can't remember correctly, that if you have a variable that is not assigned anything that it just contains 0.
So either a boolean check or an > 0 check will work.
04/06/2002 (7:59 am)
No there isn't but I believeif(%a > 0) bla();
works.
I believe, I can't remember correctly, that if you have a variable that is not assigned anything that it just contains 0.
So either a boolean check or an > 0 check will work.
#3
04/06/2002 (8:09 am)
But what if the variable you wanted to see if it existed actually did and was purposely set to zero at the time.
#4
example:
In this exampe it is the programmers responsibility to be aware of what variables he is using for what types of data because the script is not strongly typed -- meaning a variable can contain pretty much any data you want to give it.
Now check out the code below. This is really the only way I can have two variables with relatively the same name, but even then I must supercede the one with %this. a since it is part of the script object.
04/06/2002 (9:30 am)
Like I said I'm not sure. But variables with % are local to a function anyway, so really it is the programmers responsibility to make sure he doesnt declare 2 variables with the same name in that function -- which is impossible with the script anyway.example:
function foo()
{
%a = true;
if(%a)
bla();
else
bla2();
%a = "Torque Gaming System"; // oops
if(%a)
bla();
}In this exampe it is the programmers responsibility to be aware of what variables he is using for what types of data because the script is not strongly typed -- meaning a variable can contain pretty much any data you want to give it.
Now check out the code below. This is really the only way I can have two variables with relatively the same name, but even then I must supercede the one with %this. a since it is part of the script object.
if(!isObject(Example))
{
new ScriptObject(Example)
{
class = FilterHud;
a = false;
};
}
function Example::bla(%this)
{
%a = "Hello world!";
%this.a = true;
// prints "Hello world!" to console
echo(%a);
// prints true to console
echo(%this.a);
}
function Example::bla2(%this)
{
// prints nothing to the console
echo(%a);
// prints true to the console
echo(%this.a);
%this.a = false;
}
#5
04/06/2002 (1:33 pm)
I used locals becuase it was an easy example. Globals are the problem. And passed locals are just as much of a menace.
#6
04/07/2002 (9:44 pm)
Undefined variables are the empty string when evaluated in a string context. So just check for $= "" unless you might intentionally be setting variables to the empty string.
#7
======== MyExtensions.cc ============
#include "platform/platform.h"
#include "console/console.h"
#include "console/consoleInternal.h"
#include "console/ast.h"
#include
#ifdef LOG_INPUT
#include
#include
#endif
ConsoleFunction(globalVariableExists, bool, 2, 2, "globalVariableExists(variableName)")
{
argc;
StringTableEntry name = StringTable->lookup(argv[1]);
return gEvalState.globalVars.lookup(name);
}
=========================
Now, in the console window (or in a script) you can enter:
echo(globalVariableExists("$Test"));
the result is 0.
$Test="My test";
echo(globalVariableExists("$Test"));
the result is 1.
Does that help any?
04/07/2002 (10:49 pm)
I don't know about using the existing namespace, but using the global namespace, you can implement the following function in C++. As usual, my examples are in MyExtensions.cc, but you can put them where appropriate.======== MyExtensions.cc ============
#include "platform/platform.h"
#include "console/console.h"
#include "console/consoleInternal.h"
#include "console/ast.h"
#include
#ifdef LOG_INPUT
#include
#include
#endif
ConsoleFunction(globalVariableExists, bool, 2, 2, "globalVariableExists(variableName)")
{
argc;
StringTableEntry name = StringTable->lookup(argv[1]);
return gEvalState.globalVars.lookup(name);
}
=========================
Now, in the console window (or in a script) you can enter:
echo(globalVariableExists("$Test"));
the result is 0.
$Test="My test";
echo(globalVariableExists("$Test"));
the result is 1.
Does that help any?
#8
04/07/2002 (10:58 pm)
Good thinking Tony!
Associate Melv May
The engine code "console\compiledEval.cc" (Line 319)
Uncomment the lines ...
... and recompile. If it's not there then add the code as the last lines in the function "ExprEvalState::setCurVarName(...)".
This at least will tell you at runtime whether you are attempting to reference a variable before assignment.
Hope this helps.