using for if, to grow (solved)
by rennie moffat · in Torque Game Builder · 02/13/2011 (8:43 am) · 9 replies
Hi there,
I am relatively new to coding and have begun to use "for statements" successfully. For instance I use it to clear objects of a certain group on level end etc. What I am wondering is if I can use "for" to make things grow? like make an object grow in size or, make the camera zoom in smoothly?
I have been trying to achieve this but with no success yet. The following, just makes the object, instantly hit the target size, it does not grow over time as I would like.
I am relatively new to coding and have begun to use "for statements" successfully. For instance I use it to clear objects of a certain group on level end etc. What I am wondering is if I can use "for" to make things grow? like make an object grow in size or, make the camera zoom in smoothly?
I have been trying to achieve this but with no success yet. The following, just makes the object, instantly hit the target size, it does not grow over time as I would like.
///trying to make an object grow
for(%i = 0; %i < %this.targetSize.x; %i++)
{
%currentSizex = %this.owner.size.x;
if(%currentSizex < %this.targetSize.x)
{
%scale = %currentSizex * 0.25;
%newSizex = %scale + %currentSizex;
%this.owner.setSize(%newSizex, %newSizex);
}
}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
I was actually studying that behavior yesterday but a few things irked me and I was hoping I could find a simpler method, ie, using a for if statement.
When you say, while my for-loop is running the game is not, and therefore rendering is not happening, I do not quite follow.
Is the mouse over grow behavior the best example I could have as to have an object grow? And can this be applied to zooms? Is the for-loop not right for growing and zooming?
Cheers
02/13/2011 (10:31 am)
Thanks William, I was actually studying that behavior yesterday but a few things irked me and I was hoping I could find a simpler method, ie, using a for if statement.
When you say, while my for-loop is running the game is not, and therefore rendering is not happening, I do not quite follow.
Is the mouse over grow behavior the best example I could have as to have an object grow? And can this be applied to zooms? Is the for-loop not right for growing and zooming?
Cheers
#3
02/13/2011 (10:49 am)
The for loop runs until it reaches its end condition - which means the script engine is using all of the processor time during the loop. The rest of your game logic is on hold while this happens. Normally the script engine time-shares with the rest of the game engine, but in this case the script holds on until the for loop completes. You can run a quick trial by creating a for loop that just counts from 0 to 10000000 and see what your game does when this script is run.
#4
1) Scripts are run.
2) Game physics update.
3) Game is drawn to the screen.
4) Repeat from step (1).
You'll see that while you are growing the size of the object you are still in step (1). Only when you reach the end of your script can step (2) happen. And then finally step (3) where the game is drawn.
Your script doesn't do a little bit of growing and then exit. It grows it the whole way and then exits.
02/13/2011 (11:19 am)
A for-loop can be written that will change the size little-by-little, but you won't be able to see that. Pretend that this happens over and over:1) Scripts are run.
2) Game physics update.
3) Game is drawn to the screen.
4) Repeat from step (1).
You'll see that while you are growing the size of the object you are still in step (1). Only when you reach the end of your script can step (2) happen. And then finally step (3) where the game is drawn.
Your script doesn't do a little bit of growing and then exit. It grows it the whole way and then exits.
#5
is that right?
02/13/2011 (1:12 pm)
So then for all intensive purposes, for-loops are instantaneous, at least, are not meant to "animate" things, simply to clear, create, calculate.is that right?
#6
02/13/2011 (2:17 pm)
that is right. Always consider your script to be executing at a moment of time, not over a period of time.
#8
02/14/2011 (1:19 pm)
You might use a behavior such that your object increases in size by a given increment unless it has reached maximum size. I'm not certain of this - I haven't tried it. Basically you should be able to write the check and grow without the for loop. If I'm right and behaviors are executed each frame then this should work. If I'm wrong, it won't.
#9
02/14/2011 (1:23 pm)
Trick is, I wanted it to grow... as in appear to grow. This, as is will do with out. Thanks.
Associate William Lee Sims
Machine Code Games
tdn.garagegames.com/wiki/TGB/Behaviors/Mouse_Over_Grow
While your scripts are running, the rest of the game is not running. So while your for-loop is running, the part of the engine that renders the game is not running. It won't know about the size changes until the for-loop is done.