Nasal Loops: Difference between revisions

Jump to navigation Jump to search
2,101 bytes added ,  14 September 2018
updated to describe current best practice regarding settimer loops
m (Update {{func link}} form)
(updated to describe current best practice regarding settimer loops)
Line 142: Line 142:
== settimer loops ==
== settimer loops ==
{{Callback Disclaimer}}
{{Callback Disclaimer}}
{{warning|settimer loops are deprecated as of FlightGear 2.11, see the [[#maketimer]] section at the end of this document}}


Loops using <tt>while</tt>, <tt>for</tt>, <tt>foreach</tt>, and <tt>forindex</tt> block all of FlightGear's subsystems that run in the main thread for the duration of the loop body, and can, thus, only be used for instantaneous operations that don't take too long.  
Loops using <tt>while</tt>, <tt>for</tt>, <tt>foreach</tt>, and <tt>forindex</tt> block all of FlightGear's subsystems that run in the main thread for the duration of the loop body, and can, thus, only be used for instantaneous operations that don't take too long.  
Line 160: Line 162:
The fewer code FlightGear has to execute, the better, so it is desirable to run loops only when they are needed. But how does one stop a loop? A once triggered timer function can't be revoked. But one can let the loop function check an outside variable and refuse calling itself, which makes the loop chain die off:
The fewer code FlightGear has to execute, the better, so it is desirable to run loops only when they are needed. But how does one stop a loop? A once triggered timer function can't be revoked. But one can let the loop function check an outside variable and refuse calling itself, which makes the loop chain die off:


{{warning|settimer loops are deprecated as of FlightGear 2.11, see the [[#maketimer]] section at the end of this document}}
<syntaxhighlight lang="nasal">
<syntaxhighlight lang="nasal">
var running = 1;
var running = 1;
Line 176: Line 179:
=== Loop Identifiers ===
=== Loop Identifiers ===


Unfortunately, this method is rather unreliable. What if the loop is "stopped" and a new instance immediately started again? Then the ''running'' variable would be ''1'' again, and a pending old loop call, which should really finish this chain, would happily continue. And the new loop chain would start, too, so that we would end up with two loop chains.
The example above has a fundamental problem; which is that it isn't possible to manage the timer as it will always fire (which is why it is better to use [[#maketimer]]). However the problem can also be solved by using a loop identifier. This is simply a variable that lives at the outer scope of the module and is used to identify the currently active timer loop. Using this technique when the loop identifier is incremented the currently waiting timers will exit when it is their turn to run and the timer chain will be broken.
 
To do this we provide each loop chain with a ''loop identifier'' and let the loop chain function will terminate itself at the start if the id doesn't match the loop-id from the outer loop. To facilitate this also requires a delegate in the settimer call that will pass in the current loop id (that was passed into the loop chain function).  
 
==== Example of using the loop indentifier technique ====


This can be solved by providing each loop chain with a ''loop identifier'' and letting the function end itself if the id doesn't match the global loop-id. Self-called loop functions need to inherit the chain id. So, every time the global loop id is increased, all loop chains die, and a new one can immediately be started.
{{warning|settimer loops are deprecated as of FlightGear 2.11, see the [[#maketimer]] section at the end of this document}}


<syntaxhighlight lang="nasal">
<syntaxhighlight lang="nasal">
Line 194: Line 201:
  loop(loopid);      # start new chain; this can also be abbreviated to:  loop(loopid += 1);
  loop(loopid);      # start new chain; this can also be abbreviated to:  loop(loopid += 1);
</syntaxhighlight>
</syntaxhighlight>
== Starting timers from a fdm initialized listener ==
{{caution|Be carfeul when using settimer from a listener on fdm-initialized as this can easily create a new timer each time the aircraft is repositioned}}
The correct way to use settimer from a listener on fdm-initialized is as follows:
{{warning|settimer loops are deprecated as of FlightGear 2.11, see the [[#maketimer]] section at the end of this document}}
<syntaxhighlight lang="nasal">
good_loop_init = 0;
good_loop = func{
# logic
    settimer(good_loop, 1);
};
setlistener("sim/signals/fdm-initialized", func {
    if (!good_loop_init){
      good_loop_init = 1;
      good_loop();
    }
});
</syntaxhighlight>
== maketimer ==


Beginning with FlightGear 2.11+ you should consider using the {{func link|maketimer()}} API instead.
Beginning with FlightGear 2.11+ you should consider using the {{func link|maketimer()}} API instead.


See also {{func link|settimer()}}
This is because maketimer creates an object that can be managed, rather than as settimer which once called will irrevocably call the method once the duration has elapsed. For this reason settimer is considered to be unmanageable.
 
=== maketimer example ===
 
<syntaxhighlight lang="nasal">
var mt_loop_example = func {
 
    if (some_condition)
      mt_loop_exampleTimer.restart(0.1); # adjust the timer frequency (ms)
 
    if (finished)
      mt_loop_exampleTimer.stop(); #cancel the timer. Timer can be started again later.
}
 
mt_loop_exampleTimer = maketimer(0.25, mt_loop_example);
mt_loop_exampleTimer.simulatedTime = 1; # defaults to using wallclock time and will therefore continue during pause.
mt_loop_exampleTimer.start();
</syntaxhighlight>
 
 
See also {{func link|maketimer()}} {{func link|settimer()}}
306

edits

Navigation menu