Variable @ issues
by Chris Gray · in Torque Game Builder · 09/18/2010 (10:06 am) · 4 replies
Hi guys, new dev here to bug you.
I have an object created with dynamic fields, and want to access them by making use of @ but am getting a parse error when using the following:
It's called using something like speciesSplit(object, h, min, max);, with the object containing fields set up like
hMale = 50;
hFemale = 50;
The problem is with "%this.%species @ "Female". I want it to be compiled as "%this.hFemale", but am obviously doing it wrong somewhere. The right data is being passed, so all i can think of is it's looking at %this and trying to find %species, instead of substituting.
Any help or am I stuck with switch/if style logic?
Thanks in advance.
I have an object created with dynamic fields, and want to access them by making use of @ but am getting a parse error when using the following:
function events::speciesSplit(%this, %species, %minMod, %maxMod)
{
%fLose = 0;
%mLose = 0;
%fLose = getRandom(%minMod, %maxMod);
%mLose = getRandom(%fLose, %maxMod);
//female 0 check
if(%this.%species @ "Female" > 0)
%this.%species @ "Female" -= %fLose;
else
%this.%species @ "Female" = 0;
//Male 0 check
if(%this.%species @ "Male" - %mLose > 0)
%this.%species @ "Male" -= %mLose;
else
%this.%species @ "Male" = 0;
}It's called using something like speciesSplit(object, h, min, max);, with the object containing fields set up like
hMale = 50;
hFemale = 50;
The problem is with "%this.%species @ "Female". I want it to be compiled as "%this.hFemale", but am obviously doing it wrong somewhere. The right data is being passed, so all i can think of is it's looking at %this and trying to find %species, instead of substituting.
Any help or am I stuck with switch/if style logic?
Thanks in advance.
#2
Let's say that previous you had something defined like
Then you'll need:
Then, when you call "events.speciesSplit( dog, 2, 4 )", the code will "change" into
Of course, this assumes Patrick's second example is what you mean. If it isn't, then the code will need to change.
09/18/2010 (7:43 pm)
If you variables end with "Female" or "Male", you'll need to use the eval function.Let's say that previous you had something defined like
new ScriptObject(events)
{
dogFemale = 10;
dogMale = 10;
catFemale = 10;
catMale = 10;
}Then you'll need:
eval( "%females = %this." @ %species @ "Female;" ); if( %females > 0 ) eval( "%this." @ %species @ "Female -= %fLose;" ); else eval( "%this." @ %species @ "Female = 0;" ); ... and so on ...
Then, when you call "events.speciesSplit( dog, 2, 4 )", the code will "change" into
%females = %this.dogFemale; if( %females > 0 ) %this.dogFemale -= %fLose; elese %this.dogFemale = 0;
Of course, this assumes Patrick's second example is what you mean. If it isn't, then the code will need to change.
#3
09/19/2010 (1:06 pm)
Thanks to you both, double to you William, exactly what I needed. %species contains the actual text of the variable, I needed to get that at run time and slot it in to make a second var. I assume eval is like a run time compile, and I put the line in there? I can't find a lot of docs on it(that one post contains more info than an hours googling)
#4
Not that there's much more, but you can keep a reference here: tdn.garagegames.com/wiki/TorqueScript_Console_Functions_5
09/19/2010 (5:01 pm)
Exactly... eval simply converts the string to TorqueScript and executes it.Not that there's much more, but you can keep a reference here: tdn.garagegames.com/wiki/TorqueScript_Console_Functions_5
Torque Owner RollerJesus
Dream. Build. Repeat.
The former:
function events::speciesSplit(%this, %species, %minMod, %maxMod) { %fLose = 0; %mLose = 0; %fLose = getRandom(%minMod, %maxMod); %mLose = getRandom(%fLose, %maxMod); //female 0 check if(%species @ "Female" > 0) %species @ "Female" -= %fLose; else %species @ "Female" = 0; //Male 0 check if(%species @ "Male" - %mLose > 0) %species @ "Male" -= %mLose; else %species @ "Male" = 0; }The latter:
function events::speciesSplit(%this, %species, %minMod, %maxMod) { %fLose = 0; %mLose = 0; %fLose = getRandom(%minMod, %maxMod); %mLose = getRandom(%fLose, %maxMod); //female 0 check if(%this.species @ "Female" > 0) %this.species @ "Female" -= %fLose; else %this.species @ "Female" = 0; //Male 0 check if(%this.species @ "Male" - %mLose > 0) %this.species @ "Male" -= %mLose; else %this.species @ "Male" = 0; }You may need to use the eval function (sorry, don't have time to test...), but first just try one of the above options first. If it doesn't work (or if it does) post back!
`Patrick