How do I .add(%newgui) with a variable parent name?
by Flybynight Studios · in Torque Game Engine · 07/18/2008 (10:05 pm) · 11 replies
Example:
container_""@%counter@"slots".add(%tempgui);
Why do this? I am writting a very complex (well to me) and dynamic gui system with literally hundreds of dynamically generated buttons (it's a hyper-complex player inventory system) and I really need to be able to do a container.add using variable container IDs..
Any thoughts? I've kind of exhausted my limited skills on this :/
Thanks in advance.
container_""@%counter@"slots".add(%tempgui);
Why do this? I am writting a very complex (well to me) and dynamic gui system with literally hundreds of dynamically generated buttons (it's a hyper-complex player inventory system) and I really need to be able to do a container.add using variable container IDs..
Any thoughts? I've kind of exhausted my limited skills on this :/
Thanks in advance.
#2
The badly phrased question was that the compiler doesnt parse variablesin the parent object when Im calling the.add function. so Container_%contid.add(%stuff) wasnt working.
I think Eval is just the ticket.
Gratzie.
07/18/2008 (10:59 pm)
I think it just might be Peter :) I'm going to go play with it but I think you nailed it. Thanks a million.The badly phrased question was that the compiler doesnt parse variablesin the parent object when Im calling the.add function. so Container_%contid.add(%stuff) wasnt working.
I think Eval is just the ticket.
Gratzie.
#3
08/04/2008 (9:35 pm)
I have the same problems with some of my GUIs, and the answer has indeed been eval(). eval() is one of those wierd functions, where a lot of people will say if you have to use it, you are probably doing something wrong 99% of the time. However, I think it's more like 50% of the time, because I run into problems ALL THE TIME that are solved by eval(), mainly dealing with variable variable names like this.
#4
I am sure Eval may be a bad thing but as you said, when it comes to nested variables, what are your options? It's when I started using nested varaibles that I unlocked a lot of doors and really started get get some powerful functionality out of my scripts.
08/04/2008 (11:11 pm)
Well in fairness to the torque script parser, I couldve gone with the built in array system but it's pretty clunky and definitely not as powerful as a proper C++ array system. I am sure Eval may be a bad thing but as you said, when it comes to nested variables, what are your options? It's when I started using nested varaibles that I unlocked a lot of doors and really started get get some powerful functionality out of my scripts.
#5
Maybe it already exists somewhere....
Just an idea I had, since other similar functions/methods exist that also allow you to avoid evals in other cases, like for example...
Can be done without an eval by using SimObject::setFieldValue
08/04/2008 (11:49 pm)
Well you "could" avoid an eval in this case if there existed a consoleFunction like "findObject( stringName )"...findObject( "container_" %counter @ "slots" ).add( %tempgui );
Maybe it already exists somewhere....
Just an idea I had, since other similar functions/methods exist that also allow you to avoid evals in other cases, like for example...
eval( "%obj.\"field\" @ %num = %val;" );
Can be done without an eval by using SimObject::setFieldValue
%obj.setFieldValue( "field" @ %num, %val );
#6
The logic for my workflow is that a player has a dynamic number of containers and each of those containers has a dynamic number of slots.. For example the player may have 3 bags or he may have 8 bags. Each one of those bags could have capacity for 4 items up to 200 items. It was a simple loop that ran an eval..
That allowed me to dynamically create arrays for the containers, the slots and all of the item information in about 10 lines of script.
Is there a better logic I should have used for that?
Thanks in advance.
08/05/2008 (8:16 am)
Im sure it's just lack of sleep that I dont understand this James but I still don't get how that would allow nested, multiple variables..The logic for my workflow is that a player has a dynamic number of containers and each of those containers has a dynamic number of slots.. For example the player may have 3 bags or he may have 8 bags. Each one of those bags could have capacity for 4 items up to 200 items. It was a simple loop that ran an eval..
eval("$container"@%counter@"slot"@%j@".add(%itemid,%itemquant);");That allowed me to dynamically create arrays for the containers, the slots and all of the item information in about 10 lines of script.
Is there a better logic I should have used for that?
Thanks in advance.
#7
Or it looks like you could use a two dimensional array..
Eg.
$container0 through $container8 are the bags.
$container0_0 through $container0_200 are the items in the first bag.
08/05/2008 (12:00 pm)
Keep in mind I totally made up that findObject function, it doesn't exist to my knowledge.eval("$container"@%counter@"slot"@%j@".add(%itemid,%itemquant);");
// Could become...
findObject( $container @ %counter @ "slot" @ %j ).add( %itemid, %itemquant);Or it looks like you could use a two dimensional array..
$container[%counter][%j].add(%itemid, %itemquant);
Eg.
$container0 through $container8 are the bags.
$container0_0 through $container0_200 are the items in the first bag.
#8
It works for me, but I'm not sure if there's a downside to it.
08/05/2008 (12:11 pm)
The options dialog (example games) does it like this:("container" @ %counter @ "slot" @ %j).add(%itemid,%itemquant);It works for me, but I'm not sure if there's a downside to it.
#9
08/05/2008 (12:33 pm)
That is fantastic guys. Thank you very much for the lesson in syntax :) I'll try and learn from it.
#10
08/05/2008 (12:48 pm)
Wow who knew you could just put it in parenthesis...
#11
IMO this particular feature of Torque Script is one of the most powerful, flexible, and useful features I've ever encountered.
08/05/2008 (4:07 pm)
You can also stick the name of the (variable) parent object in a local variable and call the method on the assembled object name as if the local variable contained a reference to the object. This works because (remember) that an object name (or ID) is an object reference in Torque Script.%parentGUI = "container_" @ %counter @ "slots"; %parentGUI.add(%tempgui);I have done this many times in many different places in my code. Storing the object name in a local variable also gives you cleaner syntax if you need to reference the parent GUI more than once.
IMO this particular feature of Torque Script is one of the most powerful, flexible, and useful features I've ever encountered.
Torque 3D Owner Peter Simard
Default Studio Name
eval("container_" @ %counter @ "slots.add(" @ %tempgui @ ");");