setWord and a complicated (for me) string.
by rennie moffat · in Torque Game Builder · 12/04/2009 (12:53 pm) · 3 replies
%growAnim = setWord(%growAnim, %j, getWord(%this.dieAnim.animationFrames, %i)); %j++;
Hi this string (minus the %j++) is a but confusing to me, I am not sure exactly what it says.
In TDN set word returns a new value to a word in a string,
so setWord(%field, 1, 388)
could make %field = 3, 5, 7
to %field = 3, 388, 7
but in the TDN it (setWord) only has 3 conditions/variables(?) which are field, word number, and new value. So what is that original line I posted (%growAnim) all about?
Thanks.
About the author
My thanks to Garage Games and the Garage Games Community combined with owned determination I got one game up, Temple Racer and I am looking to build more interesting, fun games for the mass market of the iOS app store.
#2
my apology.
thanks.
12/04/2009 (2:25 pm)
I apologise, I was looking at this line%growAnim = setWord(%growAnim, %j, getWord(%this.dieAnim.animationFrames, %i));like it had 4 variables, not three.
my apology.
thanks.
#3
There are several variables, which I will estimate definitions for:
%this.dieAnim - An animation which make the plant look like it is dying. %this.dieAnim.animationFrames will contain a string which represents the sequence of frames which make the plant look like it is dying.
%growAnim - The string which represents the sequence of frames which make the plant look like it is growing.
%j - The location in the %growAnim string that we want to replace.
%i - The location in the %this.dieAnim string that we want to look at.
After the first line
Next, we want to build a new string that reverses these numbers.
On the next line
The next lines should be taken together
First, the engine will call
In essense, the code now looks like
The first time through the for-loop, "%i" will equal 2, meaning we want to look at word "2" in the string. So the inside of the for-loop looks like this
The variable "%growAnim" has not been used before, therefore it will be assigned the value "" (known as the empty string). Substituting again, we get
It looks silly, but the code can handle this. There are no words yet, but if you set word 0 to the string "5", you'd get the string "5". Now we have
The final "%j++" will turn "%j" from "0" to "1".
Now, again, through the for-loop, we first call "%i--", turning "%i" from "2" to "1". We then check "%i >= 0", which is true, so we go again through the for-loop.
The final "%j++" will turn "%j" from "1" to "2".
Now, again, through the for-loop, we first call "%i--", turning "%i" from "1" to "0". We then check "%i >= 0", which is true, so we go again through the for-loop.
The final "%j++" will turn "%j" from "2" to "3".
Now, again, through the for-loop, we first call "%i--", turning "%i" from "0" to "-1". We then check "%i >= 0", which is false, so we stop!
Now we've turned the string "5 3 1" into a new string with "1 3 5"!
12/04/2009 (5:02 pm)
There are only 3 variables, but it might make more sense to run through the entire example:%this.dieAnim = %this.owner.getAnimation();
%j = 0;
for (%i = getWordCount(%this.dieAnim.animationFrames) - 1; %i >= 0; %i--)
{
%growAnim = setWord(%growAnim, %j, getWord(%this.dieAnim.animationFrames, %i));
%j++;
}So the user has created an animation to represent dying and has assigned it to an object. Then then put the "PlantGrowthBehavior" on the same object.There are several variables, which I will estimate definitions for:
%this.dieAnim - An animation which make the plant look like it is dying. %this.dieAnim.animationFrames will contain a string which represents the sequence of frames which make the plant look like it is dying.
%growAnim - The string which represents the sequence of frames which make the plant look like it is growing.
%j - The location in the %growAnim string that we want to replace.
%i - The location in the %this.dieAnim string that we want to look at.
After the first line
%this.dieAnim = %this.owner.getAnimation();the variable "%this.dieAnim" will contain the animation made for death. The variable "%this.dieAnim.animationFrames" may contain a string which looks like the following (without the quotes):
"1 3 5"
Next, we want to build a new string that reverses these numbers.
On the next line
%j = 0;we've set up a "%j" to be 0. "%j" represents the location in the "%growAnim" that we want to replace.
The next lines should be taken together
for (%i = getWordCount(%this.dieAnim.animationFrames) - 1; %i >= 0; %i--)
{
%growAnim = setWord(%growAnim, %j, getWord(%this.dieAnim.animationFrames, %i));
%j++;
}First, the engine will call
getWordCount(%this.dieAnim.animationFrames) - 1which gets translated to (in my example)
getWordCount( "1 3 5" ) - 1As there are 3 words in "%this.dieAnim.animationFrames", this piece of code will return the number "2".
In essense, the code now looks like
for (%i = 2; %i >= 0; %i--)
{
%growAnim = setWord(%growAnim, %j, getWord(%this.dieAnim.animationFrames, %i));
%j++;
}The first time through the for-loop, "%i" will equal 2, meaning we want to look at word "2" in the string. So the inside of the for-loop looks like this
%growAnim = setWord( %growAnim, 0, getWord( "1 3 5", 2 ) ); %j++;
The variable "%growAnim" has not been used before, therefore it will be assigned the value "" (known as the empty string). Substituting again, we get
%growAnim = setWord( "", 0, "5" ); %j++;
It looks silly, but the code can handle this. There are no words yet, but if you set word 0 to the string "5", you'd get the string "5". Now we have
%growAnim = "5"; %j++;
The final "%j++" will turn "%j" from "0" to "1".
Now, again, through the for-loop, we first call "%i--", turning "%i" from "2" to "1". We then check "%i >= 0", which is true, so we go again through the for-loop.
%growAnim = setWord( %growAnim, 1, getWord( "1 3 5", 1 ) ); %j++; // Turns into... %growAnim = setWord( "5", 1, "3" ); %j++; // Turns into... %growAnim = "5 3"; %j++;
The final "%j++" will turn "%j" from "1" to "2".
Now, again, through the for-loop, we first call "%i--", turning "%i" from "1" to "0". We then check "%i >= 0", which is true, so we go again through the for-loop.
%growAnim = setWord( %growAnim, 2, getWord( "1 3 5", 0 ) ); %j++; // Turns into... %growAnim = setWord( "5 3", 2, "1" ); %j++; // Turns into... %growAnim = "5 3 1"; %j++;
The final "%j++" will turn "%j" from "2" to "3".
Now, again, through the for-loop, we first call "%i--", turning "%i" from "0" to "-1". We then check "%i >= 0", which is false, so we stop!
Now we've turned the string "5 3 1" into a new string with "1 3 5"!
Community Manager Michael Perry
ZombieShortbus
%originalString = "How Now Brown Cow"
0 How
1 Now
2 Brown
3 Cow
New Word
%newWord = "Chicken"
Syntax
setWord(string to search, # of which word to set, replacement word)
Returns new string
Example
%newString = setWord(%originalString, 3, %newWord);
New String Value
"How Now Brown Chicken"