Game Development Community

Localization and scripting. How to insert a global variables in a dynamic field?

by shadowcode · in Torque Game Builder · 07/01/2010 (7:49 am) · 12 replies

Hi! I hope I can explain my problem correctly.

I have a script file it.cs with many global variables like this:
$ship = "Ship";
$house = "House";

I include this file at the start of the game. Then I created static sprites in the game builder and give them a dynamic field called ObjectId with the content $ship or $house.

And here is what I want. If I pick an object in the scene I want read the dynamic field ObjectId and set the text of an TextField to the string stored in the global variable with the same name as it is stored in the dynamic field? Something like this:

%textfield.setText( %selectedObj.ObjectId );

There %selectedObj.ObjectId should be resolved as the global field $house or $ship?
In other words. In my text field should now stored "ship" or "House".

Is this possible or is there a better way?

regards

#1
07/01/2010 (8:31 am)
I don't know if this is possible, but its certainly weird. I'm not sure if I even understand what you want to accomplish with this approach. The better way would be to create all of your objects in script using classes. The standard operating procedure is to create your prototype using the editor, then copy the values (such as collisionPolyList) into the class field, then delete the object out of the editor. The only scene objects I leave behind in the editor are GUI elements. That is because they are created once anyway. But I digress...
#2
07/01/2010 (9:15 am)
Hi Kevin.
My approach is some kind of "try to localize objects names in a scene". If I have a dynamic field in the level editor that points to a global variable in the script, then the level designer can set the object name in the editor. And I does not need to touch any object in the script just to change its name in the level.

off topic:
Okay, I think using the editor for prototyping class and later creating them at runtime is good approach for dynamic objects creation in a scene. I don't do it this way, because in my scene all objects are created only once, when the level is created, and they only gets destroyed after certain conditions. But if I make a level with dynamic object creation I will follow your hint. :)
#3
07/01/2010 (9:43 am)
My problem in understanding this is the idea of changing names of objects dynamically. If I name an object (rarely, but it happens) that means I want to be able to reference it by name. Changing names would kind of make that pointless. In fact, it would break my code. Sorry I can't be of more help.
#4
07/01/2010 (10:52 am)
Hi Kevin, it's okay and thx for your help.

I don't want change the script name of an object dynamically, I only want to change the "describing name" of an object in an textfield.. depending on which language is selected. Example: I have two language files it.cs, en.cs

content of it.cs:
$ship = "nave";
$house = "casa";

content of en.cs:
$ship = "Ship";
$house = "House";

At the start of a game I read in a config file which specified which language file should be loaded. Maybe an int value of 0 for italian, 1 for english.
All my objects in my Scene has one dynamic field with the name ObjectName.
Scene example: In the editor one object has the value $ship in the dynamic field ObjectName and an other one has the value $house in the dynamic field.
In the game I have a textfield in the left corner of the screen. If I click on one object in the game, the textfield should get the describing name of the selected object.
If I click on the ship object, the text field should show "nave" or "Ship" depending on what language was set in the config file.

I hope this description is better. :-S
#5
07/01/2010 (10:53 am)
What you want to do is use the eval() function. eval() takes in any expression and prints out the string equivalent.

so for example,

%textfield.setText( eval(%selectedObj.ObjectId) );

would be the same as

%textfield.setText( $ship ); // assuming %selectedObj.ObjectId is "$ship"

EDIT: Not sure if it works QUITE that way, you might need to break it up like this:

%myVariable = eval(%selectedObj.ObjectId);
%textfield.setText( %myVariable );
#6
07/01/2010 (11:18 am)
Hi Daniel...

Hmmm if I call eval like this:
%myVariable = eval(%selectedObj.ObjectId);

I always get "parse error" as an output in the console. :(
#7
07/01/2010 (11:39 am)
Augh, I got the usage wrong again. try this:

%myvar="%textfield.setText(" @ %selectedObj.ObjectId @ ");";
eval(%myvar);


That might work... assuming I'm parsing that correctly.
Eval must be used in its own line because it has to evaluate the full string before executing. Putting it in the middle of an assignment or calculation won't work.
#8
07/01/2010 (11:53 am)
Hi Daniel...

Yes it works! Thx a lot! You saved my day. :D
#9
07/01/2010 (12:17 pm)
Interesting. I've seen eval in code examples, but I never understood what it was doing.
#10
07/01/2010 (12:37 pm)
It's used to execute script(s) when you don't have them created and stored already in an external file. It's the cornerstone for any dynamic scripting.

You can even eval() methods on objects without knowing the exactly waht the method even is. It's really great, but gets clunky up until the final eval() line (as evidenced by my above post).
#11
07/01/2010 (12:54 pm)
Hmmm.. I going off topic.

With eval, would it be possible to write a program that evolve itself? I think.. at runtime the program could write new scripts with fileIO and they could included/exec by eval. Or am I wrong? :)
#12
07/01/2010 (1:08 pm)
Of course, but it'd be better to do it in source, regardless.

For example, you have a creature that can "learn" to find nuts in his environment, but only has a certain number of verbs: pickup nut, move, climb. It can walk around randomly and find nuts, but if it finds 10 nuts using a particular method, it would decide to set aside the methods used to find the nut and make that a subroutine for any of his fellow creatures that share his genetic makeup. By the end of the simulation, all of the creatures will be able to climb trees, forage for nuts under trees, or even steal other animals stores of nuts - all because those routines were successful x number of times.

Now, "climb tree" may not be a routine you planned on that creature using, so you wouldn't be able to code it. So, that creature would keep that tourinte handy for whenever it's hungry.

Maybe after it fails 10 times, it will dump that routine and go back to foraging randomly. This way, writing that "climb trees" routine manually and premeditated won't just be wasted time because he can't find anymore.

Just some ideas for basic "learning" AI