Game Development Community

Accessing a Datablock property [Answered]

by Colin Richardson · in Torque Game Builder · 08/06/2009 (9:09 am) · 7 replies

Hello,

I've been messing with this for some time and can't seem to get it to work.

Basically i have a datablock configured that stores names.
$characters = new ScriptObject() {
   class = characters;
   character1name = "Dr. Chandler";
   character2name = "Prof. P. Phogg";
   character3name = "";
....
};

Now i have a function which shows on a GUI window what's just happened.
function FirstAidEnabled(%this,%character) {
   txtChatBox.SetText(%character @ "'s First Aid skill is available." @ txtChatBox.getText());    
}

However %character is set to be "character1" as there are 3 of these options.

So what i thought then was i could do $characters.%character @ "name"; as in the console i can type echo($characters.character1name) and i get back the correct answer "Dr. Chandler".

But that throws a scripting error.

So how can i create a local variable that will return to me the actual name, or am i (i think) going about this completely the wrong way.

Many thanks
Colin



#1
08/06/2009 (11:12 am)
Looks intricate... probably eval() would be helpful for what you want.

However, why not something cleaner, like having the character name on each character objects or datablocks?
#2
08/06/2009 (11:36 am)
Hi Novack,

eval() is a new one on me, it's not in the documentation, however it doesn't seem to work either.

For example:

echo($characters.character1name);

Shows "Dr. Chandler" - correct

Either of these throw a parse error (when i try them in console)
echo(eval($characters.character1name));
echo(eval("$characters.character1name"));

There are many ways i can do this, but i think building a string that points to another string is a very useful thing.

The names btw are an example and are entered by the player, i think though i'll go for a behaviour for this.

Thanks
#3
08/06/2009 (12:22 pm)
Just a thought, you didn't want to make the list of character names an array?
#4
08/06/2009 (12:45 pm)
Hi Drethon,

I did have a think about that, as the list of names will be pulled in from either an SQLite table or more likely a flat text file.

I've got this all working now via a new behaviour that i use to keep a track of the characters.

Thanks for the thought though, cheers!

Colin
#5
08/06/2009 (8:11 pm)
I wouldn't use eval() here. Two better options are A) TorqueScript's built-in array-like operator, or B) .getFieldValue() and .setFieldValue(). Both are going to do essentially the same thing (concatenate a string together to form a variable name), but they do it faster and without worry of bad values screwing up your script.

Example:
%obj = new ScriptObject() {
   valA = 1;
   valB = 2;
};

%var = "A";
echo(%obj.val[%var]);
echo(%obj.getFieldValue("val" @ %var));

%var = "B";
%obj.val[%var] = 5;
%obj.setFieldValue("val" @ %var, 5);

echo(%obj.valB);

Furthermore, you'll probably make your life easier by putting the index at the end of the field name rather than in the middle (i.e. "charactername1" rather than "character1name") -- otherwise you'll have to do more concatenation.
#6
08/07/2009 (1:31 am)
Greg,

Now there's my answers, that's excellent of you.

I shall go away today and give those commands a good read up.

Thank you so much, i've already converted this into a behaviour, but i may switch back as i think a datablock is much easier for my head figuring it out.

Thanks again
Colin
#7
08/07/2009 (5:58 pm)
Now thats ingenious Greg!

Couldnt find the time to get back to this thread, glad you guys sorted it out, was kind of an interesting challenge :)