Summary

This article explains what Loops are, how they work, and the difference between Event Loops and Fastloops. It should prove extremely useful as Fastlooping is one of the major essential things every Clicker should know! Also there's a bit about event loops and how Click product's runtime engines work, so its worth a read even if you are experienced.


Event Loops

So you've heard of Fastlooping. Everyone has, now. But what about event loops? All click products from KNP to TGF have an event loop, which is how running the events in your games works. For the sake of explaining it, let's use MMF, although this applies to all Click products. MMF will read through your event list 50 times a second max, as most people know. Each of these 50 'Event reads' is called an MMF loop or Event loop. So, one event loop takes about 0.02 of a second.

Quick note
Since an event loop is 0.02 of a second, the following conditions do exactly the same thing: Always, Every 0.00, Every 0.01, Every 0.02. MMF can't go any faster than an event loop per 0.02 of a second, so all of those do exactly the same thing.

So now we know what an event loop is, the continual loop of MMF running through the events.

A quick moment to explain...
If you've seen the Event List editor, MMF will basically start at the top of that, and go through every event and action in the order they appear in that list, top to bottom. The order of actions can be very important. Say every 1 second you set a counter A to counter B's value, then set counter B to 0. If you got it the wrong way round counter B will be set to 0 then counter A gets set to 0 as well because thats what counter B is. MMF starts at the top of your events, and goes down them one by one executing the actions one after the other. This is important to think about in tricky coding, ESPECIALLY with fastloops, as you'll see in a second.


FastLoops

An event loop runs every 0.02 of a second. So what is a Fastloop?
Fastloops essentially repeat events. If an event has 'On Loop' or 'Loop Trigger' at the top of it, it will only be executed in a Fastloop.
So if on start of frame you start a fastloop for 50 loops, and every loop trigger / on loop you add 1 to a counter, instantly on start of frame the counter will equal 50. Why? The Fastloop repeats the event marked with a Loop Trigger 50 times BEFORE the event loop goes on to the NEXT action or event. It effectively pauses the event loop, repeats that event the number of times you specified, then carries on the event loop from WHERE IT LEFT OFF. But, the good bit: Fastloops are NOT limited to 0.02 of a second per loop! They happen so incredibly quickly, you need tens of thousands of fastloops to just get a miniscule delay. So, on Start of Frame, that event was repeated 50 times adding 1 to a counter each time, then the event loop executed the next action, then the next events, before restarting the whole event loop 0.02 of a second later.

Loop Steps
This is another cool thing about fastloops. The Loop Step is how many times the fastloop has repeated so far. It is called the Loop Index by MMF 1.5's fastloop. The Loop Step starts at 0 and counts up to the number of loops. So if you started the loop 50 times, it starts at loop step 0 and counts up to 49 (loop step 50 isn't executed because 0 counts as a loop step - be caureful!). So, if you spread a value in an object, you can match an object's spread value to the loop step, and start doing cool things with that like making RTS's.

Another important thing to remember
MMF will only update the display at the END of an event loop. Also, built in movements update position between the end of an event loop and screen redraw which is why set position actions on objects with built in movements 'lag'.

It's time for an example, one which shows how fastlooping can get messy and confusing, and why you have to be careful.

+ Always
:Spread value 0 in value C of Tank
:Start loop #0 for NObjects(Tank) loops
:Set counter A to 0


+ Loop Trigger #0
+ Value C of Tank = Loop Step(Fastloop,0)
+ Tank is moving

: Add 1 to counter A

So, we start a loop for the number of tanks there are. If the spread value of the tank equals the loop step whilst we are looping in between that 0.02 of a second, and that one particular tank is moving, we add 1 to counter A. So, can you see what is happening? We are counting the number of moving tanks, and counter A displays how many tanks are moving. This won't slow MMF down noticeably, its a very small loop. But theres a problem with that loop! Look at the actions on Always:

:Start loop #0 for NObjects(Tank) loops
:Set counter A to 0


The loop is started, the counter now displays the number of moving tanks. Staight away after that loop the counter is set back to 0, MMF finishes the loop. redraws the screen. The counter always appears as 0.

:Set counter A to 0
:Start loop #0 for NObjects(Tank) loops


This is the good version. Counter A is reset to 0 before the loop happens. Then it counts how many tanks are moving, then it redraws screen with the counter equalling that amount.
On the next loop its set to 0, counts, redraws with counter showing the right value and so on... the counter is displayed with the correct value. Little changes like that can affect loops a lot so you have to watch out.


Summary

You got to be careful with loops order of actions and order of events can be crucial when a problem occurs. If you encounter a problem involving loops, think about what you have learnt. Think how the order of events and actions are executed, and think what happens first, what happens next, etc. It also helps to know when the frame is redrawn.

I hope you all understand now and can use loops to their powerful extent. They are extremely useful from things like applications loading a set of 10 INI entries into a list instantly, to making an RTS. Have fun

- Tigs