1,361
edits
No edit summary |
No edit summary |
||
| Line 61: | Line 61: | ||
== for, while, foreach, and forindex loops == | == for, while, foreach, and forindex loops == | ||
Nasal's looping constructs are mostly C-like: | Nasal's looping constructs are mostly C-like: | ||
<syntaxhighlight lang="nasal"> | <syntaxhighlight lang="nasal"> | ||
for( preloop_initialization; # will be run prior to the first invocation of the loop, usually to initialize a loop counter | for (preloop_initialization; # will be run prior to the first invocation of the loop, usually to initialize a loop counter | ||
condition_during_loop; # will be run prior to each iteration, usually to check the loop counter and cancel the loop if false | condition_during_loop; # will be run prior to each iteration, usually to check the loop counter and cancel the loop if false | ||
post_iteration_expression # will be run after each iteration, usually to increment a loop counter | post_iteration_expression # will be run after each iteration, usually to increment a loop counter | ||
) { | |||
{ | |||
# loop body | # loop body | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
<syntaxhighlight lang="nasal"> | <syntaxhighlight lang="nasal"> | ||
for (var i=0; i < 3; i = | for (var i = 0; i < 3; i += 1) { | ||
# loop body | # loop body | ||
} | } | ||
| Line 87: | Line 84: | ||
<syntaxhighlight lang="nasal"> | <syntaxhighlight lang="nasal"> | ||
foreach(elem; list1) { doSomething(elem); } # NOTE: the delimiter is a SEMICOLON ; | foreach (elem; list1) { doSomething(elem); } # NOTE: the delimiter is a SEMICOLON ; | ||
</syntaxhighlight> | </syntaxhighlight> | ||
| Line 95: | Line 92: | ||
<syntaxhighlight lang="nasal"> | <syntaxhighlight lang="nasal"> | ||
foreach(light; lights) { lightNodes[light] = propertyPath; } | foreach (light; lights) { lightNodes[light] = propertyPath; } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
| Line 101: | Line 98: | ||
<syntaxhighlight lang="nasal"> | <syntaxhighlight lang="nasal"> | ||
myhash = {first: 1000, second: 250, third: 25.2 }; | var myhash = { first: 1000, second: 250, third: 25.2 }; | ||
foreach (var i; keys (myhash)) { | foreach (var i; keys(myhash)) { | ||
# multiply each value by 2: | |||
myhash[i] *= 2; | |||
# print the key and new value: | |||
print(i, ": ", myhash[i]); | |||
} | } | ||
# this will print in some order: | # this will print in some order: | ||
#first: 2000 | # first: 2000 | ||
#second: 250 | # second: 250 | ||
#thid: 25.2 | # thid: 25.2 | ||
</syntaxhighlight> | </syntaxhighlight> | ||
| Line 117: | Line 115: | ||
<syntaxhighlight lang="nasal"> | <syntaxhighlight lang="nasal"> | ||
forindex(i; list1) { doSomething(list1[i]); } | forindex (i; list1) { doSomething(list1[i]); } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
| Line 124: | Line 122: | ||
<syntaxhighlight lang="nasal"> | <syntaxhighlight lang="nasal"> | ||
var c = 0; | var c = 0; | ||
while( c < 5 ) | while (c < 5) | ||
print(c += 1); | |||
print("end of loop\n"); | print("end of loop\n"); | ||
</syntaxhighlight> | </syntaxhighlight> | ||
| Line 133: | Line 131: | ||
<syntaxhighlight lang="nasal"> | <syntaxhighlight lang="nasal"> | ||
for (var i=0; i < 10; i+=1) { | for (var i = 0; i < 10; i += 1) { | ||
# loop body | # loop body | ||
# don't do loop for i == 3 | # don't do loop for i == 3 | ||
if ( i == 3 ) continue; | if (i == 3) { | ||
continue; | |||
} | |||
# exit for other reason | # exit for other reason | ||
if ( want_to_quit ) break; | if (want_to_quit) { | ||
break; | |||
} | |||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
| Line 167: | Line 169: | ||
{{warning|settimer loops are deprecated as of FlightGear 2.11, see the [[#maketimer]] section at the end of this document}} | {{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 197: | Line 200: | ||
... | ... | ||
settimer(func { loop(id) }, 2); # call self with own loop id | settimer(func { loop(id) }, 2); # call self with own loop id | ||
} | }; | ||
loop(loopid); # start loop | loop(loopid); # start loop | ||
| Line 214: | Line 217: | ||
{{warning|settimer loops are deprecated as of FlightGear 2.11, see the [[#maketimer]] section at the end of this document}} | {{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"> | ||
good_loop_init = 0; | var good_loop_init = 0; | ||
good_loop = func{ | var good_loop = func { | ||
# logic | # logic | ||
settimer(good_loop, 1); | settimer(good_loop, 1); | ||
}; | }; | ||
setlistener("sim/signals/fdm-initialized", func { | setlistener("sim/signals/fdm-initialized", func { | ||
if (!good_loop_init){ | if (!good_loop_init) { | ||
good_loop_init = 1; | |||
good_loop(); | |||
} | } | ||
}); | }); | ||
| Line 239: | Line 242: | ||
<syntaxhighlight lang="nasal"> | <syntaxhighlight lang="nasal"> | ||
var mt_loop_example = func { | var mt_loop_example = func { | ||
if (some_condition) { | |||
if (some_condition) | mt_loop_exampleTimer.restart(0.1); # Adjust the timer frequency (ms) | ||
} | |||
if (finished) | 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 = maketimer(0.25, mt_loop_example); | ||
mt_loop_exampleTimer.simulatedTime = 1; # | mt_loop_exampleTimer.simulatedTime = 1; # Use simulated time, as maketimer defaults to using wallclock time and continues during pause. | ||
mt_loop_exampleTimer.start(); | mt_loop_exampleTimer.start(); | ||
</syntaxhighlight> | </syntaxhighlight> | ||
| Line 255: | Line 259: | ||
<syntaxhighlight lang="nasal"> | <syntaxhighlight lang="nasal"> | ||
var timer_loop = func { | |||
var timer_loop = func{ | # logic | ||
# logic | |||
}; | }; | ||
edits