Something that would help me
by rennie moffat · in Torque Game Builder · 11/18/2009 (2:11 pm) · 31 replies
(%i = getWordCount(%this.pulseAni.animationFrames) - 1; %i >= 0; %i--)
This line of code says that %i = getWordCount of %this.pulseAni.animationFrames.
The animationFrames is a predefined set for pulseAni?
However, from here it is told to have a -1, integer I am guessing which would tell the animation to move backwards or start at frame = -1 (not sure).
%i >= 0, and %i-- say that %i can not be less than 0, however the %i-- is telling the animation to move backwards. So I am not sure why %i is >= 0.
This is my thinking when looking at this line. Please if someone can help me think through this I would much appreciate it. You guys are the best when it comes to this. Please help.
This line of code says that %i = getWordCount of %this.pulseAni.animationFrames.
The animationFrames is a predefined set for pulseAni?
However, from here it is told to have a -1, integer I am guessing which would tell the animation to move backwards or start at frame = -1 (not sure).
%i >= 0, and %i-- say that %i can not be less than 0, however the %i-- is telling the animation to move backwards. So I am not sure why %i is >= 0.
This is my thinking when looking at this line. Please if someone can help me think through this I would much appreciate it. You guys are the best when it comes to this. Please help.
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
Hope those links help.
11/18/2009 (3:28 pm)
This gets answered in the online documentation that explains Torque Script basics. The getWordCount() function is explained in the online docs, as well as the meaning of all the parts of that loop- because despite the fact that you didn't paste the whole line, those are the arguments that a for() loop takes.Hope those links help.
#3
Ren
Ah life, such is the case.
Ces la vis mon frier.
11/18/2009 (3:54 pm)
The problem is learning. I am used to being, having people explain things. No worries mate.Ren
Ah life, such is the case.
Ces la vis mon frier.
#4
This line of code:
So the best-optimized practice is to as JUST ONCE for number of elements, and put that in a local variable, like this:
11/18/2009 (4:15 pm)
IT is considered to be a bad practice in for loops to ask for "how many objects there are" in the for parenthesis.This line of code:
for(%i = getWordCount(%this.pulseAni.animationFrames) ; %i > 0; %i--){}means that every time when loop repeats you are asking for TGB to iterate again and search how menu words there are in .animationFrames vector. The same thing is with arrays (common mistake) if you have, say, 1000 elements in an array (or in your animationFrames) engine must ask again over and over again for how menu elements there are (which is also done through iteration)So the best-optimized practice is to as JUST ONCE for number of elements, and put that in a local variable, like this:
%startValue = getWordCount(%this.pulseAni.animationFrames);
for(%i = %startValue ; %i > 0; %i--){}Actually, now when i look at this code :) you shouldn't get optimization problem, since you are asking for "a number of elements" only in init part of for loop. (Since i already wrote all this stuff) bare in mind that it would be a problem if you have putted it in %i>0 part of code. ;)
#5
11/18/2009 (4:38 pm)
so is %i, in getWordCount (also getWord?) getting %i in increments of the frames of .pulseAni?
#6
Except that on the forums, you get your explanation in the same format that you get it from the online docs: text. There's nothing wrong with reading the docs and asking for clarification on it, but there's no difference between having someone type up how to use a for() loop and reading it in the online docs that someone else typed up. It's the same. Now, when you get beyond that understanding that the docs gave you and want to ask, that's when other people's typing comes in handy.
No worries. I'll stop posting references to the docs since you don't want that...
11/18/2009 (4:49 pm)
Quote:The problem is learning. I am used to being, having people explain things.
Except that on the forums, you get your explanation in the same format that you get it from the online docs: text. There's nothing wrong with reading the docs and asking for clarification on it, but there's no difference between having someone type up how to use a for() loop and reading it in the online docs that someone else typed up. It's the same. Now, when you get beyond that understanding that the docs gave you and want to ask, that's when other people's typing comes in handy.
No worries. I'll stop posting references to the docs since you don't want that...
#7
anyhow I really posted not for the for as you say but to better understand its content. The for() was irrelavent really, but thanks.
11/18/2009 (5:12 pm)
Well, anyhow I really posted not for the for as you say but to better understand its content. The for() was irrelavent really, but thanks.
#8
As the line you posted are the arguments of the for() function, it can exist only in relation with that function.
Let me explain it that way:
What does the previous line means? Tell me without guessing. I'm sure you're having a hard time to know because it means just nothing. Now let's put it in context :
In that context, it would mean something like : for 2009 christmas, the budget for the gift of the woman of my heart is 1'000 (if only ;))
But context could be:
This could mean than on 24.12.09 1'000 will be spent for medicine for the heart.
Or:
Now I'm expecting 1'000 people for a themed-party on Dec 24th, theme being the heart.
Now, let me be very serious just a second: the context is never ever irrelevant and ignoring it is a sure way to not understand what is happening.
I have to say I'm disppointing to see you're dismissing it just because it's something you ruled as irrelevant.
Because of how the line you posted is constructed and because of what each argument means, people who did help you interpreted it as part of a for() loop, which is the only rational interpretation of that line.
If you had asked for something less obvious, nobody could have been able to help because the line could have meant either nothing or too many things.
Example:
What does it mean? Nothing.
Now it means I'm scaling down the collision polygon of %this by a factor 0.3 in width and 0.7 in height (woot! I learned a new method! ;).
And now it means I'm setting the size of %this to 0.3 in width and 0.7 in height.
That is probably one of the reason some of your post get no answer: you don't give us the context. We can not understand what you mean or expect to achieve, at least us who are not mind-readers. Therefore we can't help. That what I tried to explain in your other post.
I know that doesn't help you much as far as TGB is concerned, but I'm sure it could help you to get better help.
11/19/2009 (5:35 am)
Actually the for() statement is totally relevant. The line you pasted is, as is, totally useless and means nothing.As the line you posted are the arguments of the for() function, it can exist only in relation with that function.
Let me explain it that way:
Quote:
"1'000, 24.12.09, heart"
What does the previous line means? Tell me without guessing. I'm sure you're having a hard time to know because it means just nothing. Now let's put it in context :
Quote:
Budget for the gift, date, beneficiary
1'000, 24.12.09, heart
In that context, it would mean something like : for 2009 christmas, the budget for the gift of the woman of my heart is 1'000 (if only ;))
But context could be:
Quote:
Amount, date, medicine for
1'000, 24.12.09, heart
This could mean than on 24.12.09 1'000 will be spent for medicine for the heart.
Or:
Quote:
People expected, date of party, theme of party
1'000, 24.12.09, heart
Now I'm expecting 1'000 people for a themed-party on Dec 24th, theme being the heart.
Now, let me be very serious just a second: the context is never ever irrelevant and ignoring it is a sure way to not understand what is happening.
I have to say I'm disppointing to see you're dismissing it just because it's something you ruled as irrelevant.
Because of how the line you posted is constructed and because of what each argument means, people who did help you interpreted it as part of a for() loop, which is the only rational interpretation of that line.
If you had asked for something less obvious, nobody could have been able to help because the line could have meant either nothing or too many things.
Example:
Quote:
(0.3, 0.7);
What does it mean? Nothing.
%this.setCollisionPolyScale(0.3, 0.7);
Now it means I'm scaling down the collision polygon of %this by a factor 0.3 in width and 0.7 in height (woot! I learned a new method! ;).
%this.setSize(0.3, 0.7);
And now it means I'm setting the size of %this to 0.3 in width and 0.7 in height.
That is probably one of the reason some of your post get no answer: you don't give us the context. We can not understand what you mean or expect to achieve, at least us who are not mind-readers. Therefore we can't help. That what I tried to explain in your other post.
I know that doesn't help you much as far as TGB is concerned, but I'm sure it could help you to get better help.
#9
Usually people like this (me also) have to receive a few slaps on the forum to finally get the habit to use "search" and read documentation that already answers all of your questions.
My guess is that it would be easier for Rennie Moffat if we copy/paste all the docs here, instead of just posting the link to that article (since he won't read it)
I personally have INFINITE patience with newbie users, an really love to help, but people should be willing to actually learn-how-to-learn.
11/19/2009 (11:06 am)
Like i said Rennie Moffat likes to make blogs of his TGB learning via this forum... It reminds me of me, in the early days :)Usually people like this (me also) have to receive a few slaps on the forum to finally get the habit to use "search" and read documentation that already answers all of your questions.
My guess is that it would be easier for Rennie Moffat if we copy/paste all the docs here, instead of just posting the link to that article (since he won't read it)
I personally have INFINITE patience with newbie users, an really love to help, but people should be willing to actually learn-how-to-learn.
#10
Thanks.
11/19/2009 (11:33 am)
@Seb, ok I see what you mean. Really tho I was trying to understand exactly what the contents were and less "for", but I do get what you are saying. It is kind of hard to know what something is for if you don't know what it is for.Thanks.
#11
11/19/2009 (11:59 am)
Edit: Never mind, failed to completely read...
#12
11/19/2009 (12:19 pm)
I have never seen a developer forum with so many friendly, helpful and patient people :)
#13
Lets assume %this.pulseAni.animationFrames contains the phrase: This is soup. Using the getWordCount function would return 3. Now logically this all makes sense but when it comes to programming 0 is usually the starting point. For instance in our phrase, This is soup, this would be in position 0, is would be in position 1, and soup would be in position 2. Notice that 3 is missing. So if you were to do something with 3 an error would occurr since it does not exsist. So to get the position correct the author is subtracting 1 from 3 to get 2. This explains getWordCount(%this.pulseAni.animationFrames) - 1.
With all variables, if you use the equals sign it will assign that variable to what ever you have after the equals sign. So in this case the local variable %i is assigned the value of 2. This now explains the first part %i = getWordCount(%this.pulseAni.animationFrames) - 1;
The mid section %i >= 0; says we continue to do this while %i is greater then or equal to 0. In our case we start at 2. Now we are unable to get to 0 unless we subtract from %i, which brings us to the next part of the code.
%i-- means to subtract 1 from the value in %i. Since our value contains 2 we would subtract 1 from 2 to leave us with 1.
The reason the for is important is because the for is part of the line of code you included. I think that was pretty much explained so now we will put it all together using our example with "This is soup" stored in %this.pulseAni.animationFrames.
The loop is structured in a way that we say "FOR" some variable we do this loop while/until for some a specific number of times, while each time is counted.
For (%i = getWordCount(%this.pulseAni.animationFrames) - 1; - for %i equals 3 - 1, which gives us 2. %i >= 0 do this loop until we reach 0, %i--, each loop will reduce 1 from %i.
The for loop on the first pass will see that the value is 2 and the loop will be executed, once the loop is completed the value will be reduced by 1 leaving us with 1. The loop then will compare the value in %i with the condition , %1 > = 0, since %i is still greater then 0 we do the loop again, the loop will complete and %i will be come 0, again the condition is checked. This time the %i equals 0 so the loop will execute again. Now the value in %i is reduced once again, leaving us with -1. The condition is now checked but our value is now less then 0, so the loop is not executed and the program jumps below the closing curly brace for the loop.
Hope that helps.
11/19/2009 (12:34 pm)
Rennie,Lets assume %this.pulseAni.animationFrames contains the phrase: This is soup. Using the getWordCount function would return 3. Now logically this all makes sense but when it comes to programming 0 is usually the starting point. For instance in our phrase, This is soup, this would be in position 0, is would be in position 1, and soup would be in position 2. Notice that 3 is missing. So if you were to do something with 3 an error would occurr since it does not exsist. So to get the position correct the author is subtracting 1 from 3 to get 2. This explains getWordCount(%this.pulseAni.animationFrames) - 1.
With all variables, if you use the equals sign it will assign that variable to what ever you have after the equals sign. So in this case the local variable %i is assigned the value of 2. This now explains the first part %i = getWordCount(%this.pulseAni.animationFrames) - 1;
The mid section %i >= 0; says we continue to do this while %i is greater then or equal to 0. In our case we start at 2. Now we are unable to get to 0 unless we subtract from %i, which brings us to the next part of the code.
%i-- means to subtract 1 from the value in %i. Since our value contains 2 we would subtract 1 from 2 to leave us with 1.
The reason the for is important is because the for is part of the line of code you included. I think that was pretty much explained so now we will put it all together using our example with "This is soup" stored in %this.pulseAni.animationFrames.
The loop is structured in a way that we say "FOR" some variable we do this loop while/until for some a specific number of times, while each time is counted.
For (%i = getWordCount(%this.pulseAni.animationFrames) - 1; - for %i equals 3 - 1, which gives us 2. %i >= 0 do this loop until we reach 0, %i--, each loop will reduce 1 from %i.
The for loop on the first pass will see that the value is 2 and the loop will be executed, once the loop is completed the value will be reduced by 1 leaving us with 1. The loop then will compare the value in %i with the condition , %1 > = 0, since %i is still greater then 0 we do the loop again, the loop will complete and %i will be come 0, again the condition is checked. This time the %i equals 0 so the loop will execute again. Now the value in %i is reduced once again, leaving us with -1. The condition is now checked but our value is now less then 0, so the loop is not executed and the program jumps below the closing curly brace for the loop.
Hope that helps.
#14
And yes this does help.
Thanks alot!
Renman
11/19/2009 (2:28 pm)
Quote:I still don't see how that would NEGATIVE one. I was thinking the NEGATIVE made it move backwards as the original source code, this is for the plantDie animation, opposite direction as plantGrow.
This explains getWordCount(%this.pulseAni.animationFrames) - 1.
And yes this does help.
Thanks alot!
Renman
#15
Most humans tend to number things starting with 1 and going to total. Say you have 10 items, they will be numbered Item1,Item2..Item10.
Most computer languages will instead label those ten items Item0,Item1..Item9 (I believe this is primarily because the oritinal arrays were pointers). What you see there, the last item is Item9 or 10-1 which is the same as total items-1.
If you are looping from the last item to the first item you will loop from item last-1 to item 0.
This help any?
(I've had some time working with Ada and found a few reasons to be glad C like languages usually have 0 based arrays...)
11/19/2009 (2:49 pm)
Do you understand 0 based arrays?Most humans tend to number things starting with 1 and going to total. Say you have 10 items, they will be numbered Item1,Item2..Item10.
Most computer languages will instead label those ten items Item0,Item1..Item9 (I believe this is primarily because the oritinal arrays were pointers). What you see there, the last item is Item9 or 10-1 which is the same as total items-1.
If you are looping from the last item to the first item you will loop from item last-1 to item 0.
This help any?
(I've had some time working with Ada and found a few reasons to be glad C like languages usually have 0 based arrays...)
#16
Ok I did not know that. So in an animation for instance, ... but still, it may sound dumb to ask but why start at -1? Why not 0?
11/19/2009 (4:17 pm)
Oh so I start from -1 in a loop.Ok I did not know that. So in an animation for instance, ... but still, it may sound dumb to ask but why start at -1? Why not 0?
#17
11/19/2009 (4:20 pm)
I didn't think Pubily could get any more explicit, but I guess I was wrong!
#18
Let's take your loop again:
Now let's write it in a simpler way:
What does this piece of code do? Let's take the first line:
getWordCount() will return the number of elements in an array.
Let's take an example to explain what an array is (I know it's not how you'd assign values to an array in your script, but it's just for the sake of example):
The numbers 1, 2, 3, 4 and 5 are stored in the same variable, %some_numbers[]. That means you have 5 elements in it. However, as you see, the numbers go from 1 to 5 but the array "positions" are numbered from 0 to 4.
Now let's take the following line:
Now %total_numbers is equal to 5, because there are 5 values (words) stored in the array %some_numbers.
Everything okay up to this point? Okay, now we're going back to the original code:
animationFrames is a method which, then it is called, returns the number of frames.
Let's assume that pulseAni is an animation with 10 frames.
%i will be assigned the number of frames of %this.pulseAni (that is equal 10 in our example), minus 1.
After that line of code, %i will be equal to 10 - 1, that is 9.
Now let's take the next part of the code:
That is the famous for() loop. A loop has one purpose: it execute a code until a particular condition you have defined is met. In game terms, it could mean the for() loop will deal damages until you run out of ammunitions, which, in script terms, mean the for() loop will, for example, decrease the value of the variable storing life of enemy until the variable storing your number of ammunitions is equal to 0 (or less).
A for() loop takes three arguments as you can see.
The first one is the initialization.
The second one is the condition.
The third one is the iteration.
%i is the initialization. In that part of the code, it will just return the value of %i to the for() loop as we have set the value of %i in the previous line.
%i >= 0 is the condition. It means the code inside the loop will be executed while %i is greater or equal to 0. The code inside the loop will therefore not be executed as soon as %i is lower than 0.
%i-- is the iteration. It means that each time the code inside the for() loop is executed, %i will be decreased by one.
Now you know how a for() loop works.
Let me give you an exemple if you permit (in following post as it is too long otherwise).
11/20/2009 (5:44 am)
No you don't, ren.Let's take your loop again:
for(%i = getWordCount(%this.pulseAni.animationFrames) - 1; %i >= 0; %i--) {
// code to execute goes here
}Now let's write it in a simpler way:
%i = getWordCount(%this.pulseAni.animationFrames) -1;
for(%i; %i >= 0; %i--) {
// code to execute goes here
}What does this piece of code do? Let's take the first line:
%i = getWordCount(%this.pulseAni.animationFrames) -1;
getWordCount() will return the number of elements in an array.
Let's take an example to explain what an array is (I know it's not how you'd assign values to an array in your script, but it's just for the sake of example):
%some_numbers[0] = "1"; %some_numbers[1] = "2"; %some_numbers[2] = "3"; %some_numbers[3] = "4"; %some_numbers[4] = "5";
The numbers 1, 2, 3, 4 and 5 are stored in the same variable, %some_numbers[]. That means you have 5 elements in it. However, as you see, the numbers go from 1 to 5 but the array "positions" are numbered from 0 to 4.
Now let's take the following line:
%total_numbers = getWordCount(%some_numbers);
Now %total_numbers is equal to 5, because there are 5 values (words) stored in the array %some_numbers.
Everything okay up to this point? Okay, now we're going back to the original code:
%i = getWordCount(%this.pulseAni.animationFrames) -1;
animationFrames is a method which, then it is called, returns the number of frames.
Let's assume that pulseAni is an animation with 10 frames.
%i will be assigned the number of frames of %this.pulseAni (that is equal 10 in our example), minus 1.
After that line of code, %i will be equal to 10 - 1, that is 9.
Now let's take the next part of the code:
for(%i; %i >= 0; %i--) {
// code to execute goes here
}That is the famous for() loop. A loop has one purpose: it execute a code until a particular condition you have defined is met. In game terms, it could mean the for() loop will deal damages until you run out of ammunitions, which, in script terms, mean the for() loop will, for example, decrease the value of the variable storing life of enemy until the variable storing your number of ammunitions is equal to 0 (or less).
A for() loop takes three arguments as you can see.
The first one is the initialization.
The second one is the condition.
The third one is the iteration.
for(%i; %i >= 0; %i--) {
// code to execute goes here
}%i is the initialization. In that part of the code, it will just return the value of %i to the for() loop as we have set the value of %i in the previous line.
%i >= 0 is the condition. It means the code inside the loop will be executed while %i is greater or equal to 0. The code inside the loop will therefore not be executed as soon as %i is lower than 0.
%i-- is the iteration. It means that each time the code inside the for() loop is executed, %i will be decreased by one.
Now you know how a for() loop works.
Let me give you an exemple if you permit (in following post as it is too long otherwise).
#19
Let's assume a few things:
As %i starts at 100 and decreases by one, after the first time the code inside de for() loop is executed, %i will be equal to 99. After the second time, it will be equal to 98, and so on. Then the condition %i > 0 is not met anymore, i. e. when %i is equal to 0 or less than 0, the code inside the loop will not be executed anymore.
That means this for loop will be executed a number of times equal to %i, which is in that case the value of $Player.ammo.
Each time the for() loop is executed, $Enemy.hearts will decrease by a value equal to $Player.damage, which is, in our exemple, equal to 2.
That means that each time the loop is executed, $Enemy.hearts will decrease by 2.
As the for() loop will be executed 100 times, $Enemy.hearts will decrease by 200.
As you can see, our poor player won't have enough ammo or doesn't have a powerful enough weapon to kill the enemy.
Anyway, this little code I've made up higher is not really a nice a clean code, but it should help you to understand better how it works.
As for why start at -1 in an animation, the answer is : you don't.
Back to your example:
The for() loop initialize with %i = getWordCount(%this.pulseAni.animationFrames) - 1. If pulseAni has 10 frames, %i will be equal to 10 -1, that is 9. Note that the frames of pulseAni will not be numbered from 1 to 10 but from 0 to 9.
The condition of the for() loop is that %i is greater than 0 or equal to 0.
Each time the code inside the loop is executed, %i decreases by 1.
That means that %i will go from 9 to 0. At that time, the for() loop won't be executed anymore.
That means you will be able to work with the frames of your animation pulseAni, starting by the last up to the first one.
Let me now give you an example of for() loops and arrays to help you understand:
In the second for() loop, we continued by assigning to %i the number of elements of %some_numbers - 1. That means 10 - 1, that is 9, in our exemple.
Then, we make a for() loop that starts from %i, that is 9. As long as %i as at least equal to 0, we're executing the loop and decreasing %i by one.
The for look puts the value of the %i position of %some_numbers[] and put it in the %j position of %retro_numbers[].
%i will go from 9 to 0 and %j from 0 to 9.
That means that
%some_numbers[] will have numbers from 1 to 10 in positions 0 to 9 and
%retro_numbers[] will have numbers from 10 to 1 in positions 0 to 9.
You never starting anything at -1. You just take the number of elements and remove 1 because an array with say 50 elements will go from 0 to 49.
Basically an array with X elements will go from 0 to (X-1).
I sincerely do hope that will help.
Now I will rest my little fingers ;)
11/20/2009 (5:46 am)
The exemple may seem silly or useless, but please do bear with me as it will show you what you can do with a for() loop for example. From that point you'll be able to imagine other uses...Let's assume a few things:
- $Player.ammo = 100
- $Enemy.hearts = 300
- $Player.damage = 2
//This fragment of code should:
// 0. Be called when the player is shooting on the enemy.
// 1. Decrease the life of the enemy according to the damages the player is dealing
// 2. Decrease the remaining ammunitions of the player
// First, we will retrieve the number of ammunitions that the player still has.
%i = $Player.ammo;
// Now, we will decrease the life of the enemy as long as the player still has ammunitions.
// To do that, we will use a for() loop.
for(%i; %i > 0; %i--;) {
// Before the following code is executed, %i is equal to $Player.ammo, that is 100.
// We will first damage the enemy.
// Life loss for enemy should be equal to the amount of damage caused by the player.
$Enemy.hearts -= $Player.damage;
// Now we decrease the number of ammunitions of the player by 1.
$Player.ammo--;
// Now, if %i (which started at 100) is greater than 0, %i will decrease by one and the code inside the loop will execute again.
}As %i starts at 100 and decreases by one, after the first time the code inside de for() loop is executed, %i will be equal to 99. After the second time, it will be equal to 98, and so on. Then the condition %i > 0 is not met anymore, i. e. when %i is equal to 0 or less than 0, the code inside the loop will not be executed anymore.
That means this for loop will be executed a number of times equal to %i, which is in that case the value of $Player.ammo.
Each time the for() loop is executed, $Enemy.hearts will decrease by a value equal to $Player.damage, which is, in our exemple, equal to 2.
That means that each time the loop is executed, $Enemy.hearts will decrease by 2.
As the for() loop will be executed 100 times, $Enemy.hearts will decrease by 200.
As you can see, our poor player won't have enough ammo or doesn't have a powerful enough weapon to kill the enemy.
Anyway, this little code I've made up higher is not really a nice a clean code, but it should help you to understand better how it works.
As for why start at -1 in an animation, the answer is : you don't.
Back to your example:
for(%i = getWordCount(%this.pulseAni.animationFrames) - 1; %i >= 0; %i--) {
// code to execute goes here
}The for() loop initialize with %i = getWordCount(%this.pulseAni.animationFrames) - 1. If pulseAni has 10 frames, %i will be equal to 10 -1, that is 9. Note that the frames of pulseAni will not be numbered from 1 to 10 but from 0 to 9.
The condition of the for() loop is that %i is greater than 0 or equal to 0.
Each time the code inside the loop is executed, %i decreases by 1.
That means that %i will go from 9 to 0. At that time, the for() loop won't be executed anymore.
That means you will be able to work with the frames of your animation pulseAni, starting by the last up to the first one.
Let me now give you an example of for() loops and arrays to help you understand:
%i = 0;
for (%i; %i <= 9; %i++) {
%some_numbers[%i] = %i + 1;
}
// The code above will be executed 10 times. At first time, %i will be equal to 0 and then will increase up to 9.
// The array %some_numbers[] will be assigned at each position from 0 to 9 the value of %i + 1.
// That means that %some_numbers will contain numbers from 1 to 10, in positions 0 to 9.
%i = getWordCount(%some_numbers) - 1;
%j = 0;
for (%i; %i >= 0 ; %i--) {
%retro_numbers[%j] = %some_numbers[%i];
%j++;
}In the second for() loop, we continued by assigning to %i the number of elements of %some_numbers - 1. That means 10 - 1, that is 9, in our exemple.
Then, we make a for() loop that starts from %i, that is 9. As long as %i as at least equal to 0, we're executing the loop and decreasing %i by one.
The for look puts the value of the %i position of %some_numbers[] and put it in the %j position of %retro_numbers[].
%i will go from 9 to 0 and %j from 0 to 9.
That means that
%some_numbers[] will have numbers from 1 to 10 in positions 0 to 9 and
%retro_numbers[] will have numbers from 10 to 1 in positions 0 to 9.
You never starting anything at -1. You just take the number of elements and remove 1 because an array with say 50 elements will go from 0 to 49.
Basically an array with X elements will go from 0 to (X-1).
I sincerely do hope that will help.
Now I will rest my little fingers ;)
#20
getWordCount only returns the number of words in a string.
It's not magic. It's not special. It knows nothing about animations. It knows nothing about arrays. It's simply the number of words in a string.
%i = getWordCount( "This is a string" );
The local variable "%i" will now equal 4 because getWordCount only returns the number of words in a string.
I'm sorry, Seb, but your final example won't work because "%some_numbers" isn't a string. (Technically it will be an empty string, but your example will start %i at -1.)
11/20/2009 (1:20 pm)
I'm absolutely amazed that there are 19 posts to explain something as mundane as getWordCount and for-loop. Because I don't want anybody to be misled:getWordCount only returns the number of words in a string.
It's not magic. It's not special. It knows nothing about animations. It knows nothing about arrays. It's simply the number of words in a string.
%i = getWordCount( "This is a string" );
The local variable "%i" will now equal 4 because getWordCount only returns the number of words in a string.
I'm sorry, Seb, but your final example won't work because "%some_numbers" isn't a string. (Technically it will be an empty string, but your example will start %i at -1.)
Torque 3D Owner Drethon
This could also have been written as:
(%i = getWordCount(%this.pulseAni.animationFrames) ; %i > 0; %i--)
Though this is assuming %i is not used later as it will now be off by one but loop the same number of times.