Script Error - Concatenation and Strings and Variable Names oh my!
by Chris "Hyena" Vogel · in Torque Game Builder · 08/16/2009 (1:35 am) · 7 replies
Ok.. I have a Structure like this...
$GameData is a Simset that contains
$Party which is a Simset that contains
$Character1 which is a ScriptObject and
$Character2 which is also a ScriptObject
now.. in one of my functions, I want to loop through my list of characters and set text on some TextObjects (non GUI) on screen.
Here is what I'm trying that's not working...
What I need to Accomplish is
so object pName1.text will get set to $GameData.Party.Character1.name
and so on...
I've also tried
But having the $GameData.%var gives a compile error.
Can anyone tell me how to make this work?
$GameData is a Simset that contains
$Party which is a Simset that contains
$Character1 which is a ScriptObject and
$Character2 which is also a ScriptObject
$GameData.Party.Character1.name = "Bob" $GameData.Party.Character2.name = "Tom" $GameData.Partysize = 2;
now.. in one of my functions, I want to loop through my list of characters and set text on some TextObjects (non GUI) on screen.
Here is what I'm trying that's not working...
What I need to Accomplish is
pName[i].text = $GameData.Party.Character[i].name;
so object pName1.text will get set to $GameData.Party.Character1.name
and so on...
for (%i = 0; %i < $GameData.Partysize; %i++)
{
%var = "$GameData.Party.Character" @ %i+1;
%pname = "PName" @ %i+1;
%pname.text = %var.name;
}I've also tried
for (%i = 0; %i < $GameData.Partysize; %i++)
{
%var = "Party.Character" @ %i+1 @ ".name";
%pname = "PName" @ %i+1;
%pname.text = $GameData.%var;
}But having the $GameData.%var gives a compile error.
Can anyone tell me how to make this work?
#2
%var = $GameData.Party.Character @ %i+1;
%pName = %var.name;
but that didn't work eiter.
However, I got it fixed! Thanks for your help.
I didn't realize that objects named in the world editor such as item1 item2 item3 could be accessed as arrays like items named that way in script.
I was able to just do
and it worked great!
08/16/2009 (2:58 pm)
Well, I tried with the concatenation without quotes such as %var = $GameData.Party.Character @ %i+1;
%pName = %var.name;
but that didn't work eiter.
However, I got it fixed! Thanks for your help.
I didn't realize that objects named in the world editor such as item1 item2 item3 could be accessed as arrays like items named that way in script.
I was able to just do
pname[%i+1].text = $GameData.Party.Character[%i+1].name;
and it worked great!
#3
08/16/2009 (3:40 pm)
I could of swore this worked.. I just tried it when I replied and I thought it was.. now it's giving me errors. Maybe it 'doesn't' work that way. I might of been getting confused by a pre-compiled script. Grr. I'll keep working on it.
#4
seems that objects named in the editor such as Item1, Item2, Item3 cannot be accessed via script as arrays like Item[%i], Item[%i+1]. But, if you have a item named in script as an array such as Party.Character1 you CAN access it as in Party.Character[%i]
so, for my code, it works like this...
08/16/2009 (3:44 pm)
Ahh, 1 part did, 1 part didn't.seems that objects named in the editor such as Item1, Item2, Item3 cannot be accessed via script as arrays like Item[%i], Item[%i+1]. But, if you have a item named in script as an array such as Party.Character1 you CAN access it as in Party.Character[%i]
so, for my code, it works like this...
%item = "pName" @ %id; %item.text = $GameData.Party.Character[%id].name; %item.visible = true;
#5
Glad it worked out for ya!
08/16/2009 (4:09 pm)
You're right. I need to stop post too soon after waking up ;)Glad it worked out for ya!
#6
08/17/2009 (1:01 pm)
Note that for objects named in the editor you can use the nameToID function, like so:for(%i = 0; i < %numObjects; i++)
{
%item = nameToID("Item" @ i);
%item.whatever = 10;
}
#7
08/17/2009 (8:17 pm)
Woohoo. Thanks David for that awesome bit of info.
Torque 3D Owner Ted Southard
for (%i = 0; %i < $GameData.Partysize; %i++) { %var = "$GameData.Party.Character" @ %i+1; %pname = "PName" @ %i+1; %pname.text = %var.name; }You should take the quotes out from around $GameData, as it's a variable, and you can still concatenate it with %i+1 (%var stores it as a string). Once that's done, %var should hold the character reference given by $gamdata.party.character%i (%i being a number), and then when you call %pname.text = %var.name, it should insert the name into the text.
As for PName, what you probably want to do is %pName[%i] instead of %pname = "PName" @ %i; because you can still work with it as an array, since the problem that threw you off had to do with using quotes with the other variable. Hope this helps.
There are tutorials as well as a very good scripting reference that can help sort you out on these issues.