Submitted:|| 7th December, 2004
I was going through Terminal Orbit's movement engine recently and noticed this fastlooping technique I had used, and thought it might make a good quick article.
Most people know the Fastlooping-objects technique, whereby you can loop all duplicates of an object individually via spread values. If you don't know how to do this, stop reading now, because this takes that method a step further. You can read about the standard fastlooping objects method at http://www.clickteam.info/kb/start/index.php?showtopic=43
If you carry on, you'll need the Value Finder object: http://www.clickteam.info/extensions/extview.php?id=143
Advanced fastlooping, or Custom Loops as I seem to have commented it in my code, involves looping only the objects which need to be looped. If we take the standard example of tanks moving to waypoints matched by an ID, you only need to loop tanks which are moving. This is more efficient, and definitely faster if you are looping a lot of objects.
The Value Finder object can tell you how many duplicates of an object met the event's conditions. So that can tell us how many times we need to fastloop for. (You can also use the Selected Object counter to do that, but Value Finder works with qualifiers, and qualifiers make life easier in general). A little known trick with the Spread Value action can then provide the indices for fastlooping. Lessee...
: Tank: set value A to 0
: Value Finder: reset count
+ [Negate] Tank: Is Stopped
: Tank: spread value 1 in value A
: Value finder: count Tank that meet event's conditions
+ Stored Count of Value Finder > 0
: Start loop tanks for Stored Count(Value Finder) loops
+ On loop tanks
+ Value A of Tank = LoopIndex(tanks) + 1
+ Value B of MoveTarget = Value B of Tank assuming value Bs hold the IDs
: Rotate tank to MoveTarget
You will probably notice the last event looks a bit like a standard object fastloop, except with a +1. Let's break down what we did.
Event 1 ensures that all objects have a value A of 0, and that Value Finder's count is zero.
Event 2 will count how many tanks are moving, so we know how many units to fastloop. Then, it spreads 1 in value A. Something interesting happens here: the first moving tank gets a value A of 1, the second moving tank gets a value of 2, etc. etc. and any tank which is not moving still has a value A of 0. Why? Most people probably know actions only apply to objects which met the event's conditions - the same goes for the spread value action. It will spread 1 and upwards in any moving object.
Event 3 starts the fastloop, but only if there are moving objects. Notice how this is more efficient, if there are 50 tanks and none are moving, there is no fastlooping done at all. If 3 are moving, 3 fastloops are run. Compare that to the standard method of fastlooping all 50 all the time.
(Note that the fastloop has to be started on the next event and cannot be run from event 2: this is how the value finder object works, read it's docs)
Event 4 is then run once per moving tank. Since our fastloop goes 0, 1, 2... and the spread values of our moving tanks goes 1, 2, 3... we need to add 1 to the loopindex to get the value of the current moving tank. Apart from that it's your standard object loop.
Thus concludes Advanced Fastlooping/Custom Loops. Slightly more complex than a standard object loop, but you'll get extra FPS in return!