Dynamic Variables and Eval
by Deozaan · in Torque Game Builder · 11/04/2007 (4:44 pm) · 13 replies
Yep, I know there are lots of eval posts around here. No I didn't find my answer in them. Sorry.
What I'm trying to do is loop through a list of objects and compare a dynamic property with a dynamic value.
For example, let's say I have the following values:
What I'm looking to do is something to the effect of
I know that won't work how it is, thus my reason for using eval. Basically what I do is make a string:
Which should comes out to be:
Then I eval the string. This is where I get parse errors and other problems. All of the above has been a somewhat theoretical example, so here is the actual code. This code is modified from fieldTypes.ed.cs mentioned in this post about behaviors.
When I run that I get parse errors. I've tried putting the continue statement in the eval string but then it tells me it's not in a loop so it ignores it. I've run it through Torsion's debugger and whether the eval is true or false it always executes the continue statement. I'm not sure what's wrong with it.
What I'm trying to do is loop through a list of objects and compare a dynamic property with a dynamic value.
For example, let's say I have the following values:
$obj = 1457; // ID of object $property = "Team" // this is a dynamic field $value = "Killers";
What I'm looking to do is something to the effect of
if ($obj.$property $= "$value")
I know that won't work how it is, thus my reason for using eval. Basically what I do is make a string:
$testString = "if(" @ $obj @ "." @ $property @ " $= \"" @ $value @ "\")";Which should comes out to be:
If (1457.Team $= "Killers")
Then I eval the string. This is where I get parse errors and other problems. All of the above has been a somewhat theoretical example, so here is the actual code. This code is modified from fieldTypes.ed.cs mentioned in this post about behaviors.
%scenegraph = %this.owner.getSceneGraph();
%count = %scenegraph.getSceneObjectCount();
for( %i = 0; %i < %count; %i++ )
{
%sceneObject = %scenegraph.getSceneObject( %i );
%sceneObjTest = "if(" @ %sceneObject @ "." @ %type @ " !$= \"" @ %value @ "\")";
//if(%sceneObject.%type !$= "%value")
eval(%sceneObjTest);
continue;
%this.targetList.add(%sceneObject.getID());
}When I run that I get parse errors. I've tried putting the continue statement in the eval string but then it tells me it's not in a loop so it ignores it. I've run it through Torsion's debugger and whether the eval is true or false it always executes the continue statement. I'm not sure what's wrong with it.
#2
I think you shouldn't eval an "if" statement alone (I never tried but it seems like a wrong thing to do).
11/04/2007 (5:29 pm)
@DeozaanI think you shouldn't eval an "if" statement alone (I never tried but it seems like a wrong thing to do).
#3
You're right, they are completely different from each other.
Are two separate things. To be honest I'm not sure which is correct but I figure that a String Compare would require a string, and without quotation marks it's not a string, but the name of an object.
In either case, though, this is not the problem. I actually added the quotes after the parse error because of like I just said, I think a string would require quotes.
@Benjamin
So what would you suggest? If I put the continue statement in the eval, it doesn't realize it's in a loop and ignores it, but apparently by using the eval statement TGB doesn't realize it's an IF statement that needs to have an open or close.
Ooh, how about this?
11/04/2007 (5:35 pm)
@AunYou're right, they are completely different from each other.
if (1578.Team $= "Killers") if (1578.Team $= Killers)
Are two separate things. To be honest I'm not sure which is correct but I figure that a String Compare would require a string, and without quotation marks it's not a string, but the name of an object.
In either case, though, this is not the problem. I actually added the quotes after the parse error because of like I just said, I think a string would require quotes.
@Benjamin
So what would you suggest? If I put the continue statement in the eval, it doesn't realize it's in a loop and ignores it, but apparently by using the eval statement TGB doesn't realize it's an IF statement that needs to have an open or close.
Ooh, how about this?
$obj = 1578;
$property = "Team";
$value = "Killers";
$str = $obj @ "." @ $property; // $obj.$property, or in this example 1578.Team
if (eval($str) !$= $value)
continue;
#4
Thanks for the helping me think about it another way.
11/04/2007 (5:48 pm)
Okay I got it working with the following code:for( %i = 0; %i < %count; %i++ )
{
%sceneObject = %scenegraph.getSceneObject( %i );
%sceneObjTest = "if(" @ %sceneObject @ "." @ %type @ " $= " @ %value @ ") " @ %this @ ".targetList.add(" @ %sceneObject @ ".getID());";
// if (%sceneObject.%type $= %value) %this.targetList.add(%sceneObject.getID());
eval(%sceneObjTest);
}Thanks for the helping me think about it another way.
#5
11/04/2007 (7:44 pm)
Maybe I'm missing a nuance, but couldn't you just use getFieldValue?if ( %sceneObject.getFieldValue(%type) $= %value )
%this.targetList.add(%sceneObject.getID());
#6
Thanks Andy, that knowledge will make things a lot easier in the future!
11/04/2007 (7:47 pm)
Heh heh. I could but then I'd have to know about it in the first place. (c:Thanks Andy, that knowledge will make things a lot easier in the future!
#7
I would suggest taking a bit of time to look through console/simBase.cc and console/consoleFunctions.cc for ConsoleFunctions and ConsoleMethods - it'll probably help out a lot in the future
11/04/2007 (8:02 pm)
No problem - I realize there are hundreds of console functions/methods - it takes a while!I would suggest taking a bit of time to look through console/simBase.cc and console/consoleFunctions.cc for ConsoleFunctions and ConsoleMethods - it'll probably help out a lot in the future
#8
11/04/2007 (8:51 pm)
Don't forget the power of TorqueScript arrays---you can use strings as indexes!$obj.properties["Team"] = "Killers"; $obj.properties["SomeOtherIndex"] = "Foo"; $value = "Team"; $str = $obj.properties[$value]; echo($str); --> Killers
#9
11/05/2007 (12:06 pm)
Thanks for the suggestions/reminders. Very useful!
#10
Can the arrays "stack"?
For example, are any of the following examples valid?
Thanks in advance for any help.
07/13/2008 (12:24 am)
Sorry for reviving this old thread, but once again I have some questions about how to work with some dynamic variables.Can the arrays "stack"?
For example, are any of the following examples valid?
$obj.properties[%i].name = "Bob"; $obj.properties[%i]["X"] = 42; $obj.properties["grid" @ %x @ %y] = true;
Thanks in advance for any help.
#11
07/13/2008 (2:17 pm)
Examples 1 and 3 are valid. Example 2 would probably give an error, but I'm not 100% sure without trying it.
#12
The valid options:
The third is correct, but you can mess up with values if your %x and %y is going to be numbers > 10.
Example:
%obj.properties[ "grid" @ 12 @ 1 ] and it's equal to "grid121".
On second "attempt":
%obj.properties[ "grid" @ 1 @ 21 ] and it's equal to "grid121" too and by second assignment we overwriting the previously assigned value.
if you use comma to separate values:
%obj.properties["grid", %x, %y] it will build you to:
%obj.propertiesgrid_12_1 and %obj.propertiesgrid_1_21
(using 12, 1 and 1, 21 respectively)
This is something like dynamic pseudo-array, very flexible, love this feature in TorqueScript :)
Hope this helps!
07/13/2008 (2:35 pm)
Example 1 is valid only if there is an object assigned to the %obj.properties[%i], if you just use:%obj = new SimObject(); %obj.properties[0].name = "Bob"; echo(%obj.properties[0].name);it will echo empty line, as the TS trying to assign "Bob" to the "name" field of object assigned to %obj.properties[%i].
The valid options:
%obj.properties[%i, "name"] = "Bob";or:
%obj = new SimObject(); %obj2 = new SimObject(); %obj.properties[0] = %obj2; %obj.properties[0].name = "Bob"; echo(%obj.properties[0].name); echo(%obj2.name);This will work. Your second example should be written in the same manner:
%obj.properties[%i, "x"] = 42;to work, or it will give you "Parse error".
The third is correct, but you can mess up with values if your %x and %y is going to be numbers > 10.
Example:
%x = 12; %y = 1; %obj.properties["grid" @ %x @ %y] = true; echo(%obj.properties["grid" @ %x @ %y]); // Will output "1" (true) %a = 1; %b = 21; %obj.properties["grid" @ %a @ %b] = false; // Notice the different value assignment! echo(%obj.properties["grid" @ %a @ %b]); // Will output "0" (false) echo(%obj.properties["grid" @ %x @ %y]); // Will output "0" (false), not "1" (true) as assumed.This because your are building:
%obj.properties[ "grid" @ 12 @ 1 ] and it's equal to "grid121".
On second "attempt":
%obj.properties[ "grid" @ 1 @ 21 ] and it's equal to "grid121" too and by second assignment we overwriting the previously assigned value.
if you use comma to separate values:
%obj.properties["grid", %x, %y] it will build you to:
%obj.propertiesgrid_12_1 and %obj.propertiesgrid_1_21
(using 12, 1 and 1, 21 respectively)
This is something like dynamic pseudo-array, very flexible, love this feature in TorqueScript :)
Hope this helps!
#13
07/13/2008 (7:12 pm)
Yes that's very helpful! Thank you!
Torque 3D Owner Aun Taraseina
Should be something like this
Basically, I think you got the term confuse with "$value" and $value are the same which aren't.
Aun.