Nasal library: Difference between revisions

Jump to navigation Jump to search
1,863 bytes removed ,  15 April 2016
→‎settimer(): Start docing
(→‎setlistener(): Finish doc)
(→‎settimer(): Start docing)
Line 2,053: Line 2,053:


=== settimer() ===
=== settimer() ===
{{Leaking Nasal Disclaimer
{{WIP}}
|oldapi=settimer()
{{Nasal doc
|newapi=maketimer()
|syntax = settimer(function, delta[, realtime]);
|source = {{flightgear file|src/Scripting/NasalSys.cxx|l=499|t=Part 1}} {{!}} {{flightgear file|src/Scripting/NasalSys.cxx|l=1286|t=Part 2}}
|text = Runs the given function a specified amount of seconds after the current time. Returns <code>'''nil'''</code>.
|param1 = function
|param1text = Mandatory function that will be called. It may be necessary to enclose code in an anonymous function (see example).
|param2 = delta
|param2text = Mandatory amount of time in seconds after which the function will be called.
|param3 = realtime
|param3text = If 1 (true), "real time" will be used instead of "simulation time." Defaults to 0 (false). Note that if "simulation time" is used, the timer will not run while FlightGear is paused.
}}
}}
Runs a function after a given simulation time (default) or real time in seconds.
<syntaxhighlight lang="nasal">
settimer(<function>, <time> [, <realtime=0>]);
</syntaxhighlight>
The '''first argument''' is a function object (ie, "func { ... }").  Note that this is different from a function call (ie, "func ( ... )"). If you don't understand what this means, just remember to always enclose the first argument in any call to settimer with the word "func" and braces "{ }", and it will always work.
The '''second argument''' is a delay time. After this amount of time the function will be executed.
For instance, if you want to print the words "My result" in five seconds, use this code:
<syntaxhighlight lang="nasal">
settimer ( func { print ( "My result"); }, 5);
</syntaxhighlight>
Inside the braces of the func object you can put any valid Nasal code, including a function call.  In fact, if you want to call a function with certain values as arguments, the way to do it is to turn it into a function object by enclosing it with a func{}, for example:
<syntaxhighlight lang="nasal">
myarg1="My favorite string";
myarg2=432;
settimer ( func { myfunction ( myarg1, myarg2); }, 25);
</syntaxhighlight>
The '''third argument''' is optional and defaults to 0, which lets the time argument be interpreted as "seconds simulation time". In this case the timer doesn't run when FlightGear is paused. For user interaction purposes (measuring key press time, displaying popups, etc.) one usually prefers real time.
<syntaxhighlight lang="nasal">
# simulation time example
var copilot_annoyed = func { setprop("/sim/messages/copilot", "Stop it! Immediately!") }
settimer(copilot_annoyed, 10);
</syntaxhighlight>
<syntaxhighlight lang="nasal">
# real time example
var popdown = func ( tipArg ) {
    fgcommand("dialog-close", tipArg);
}
var selfStatusPopupTip = func (label, delay = 10, override = nil) {
    var tmpl = props.Node.new({
            name : "PopTipSelf", modal : 0, layout : "hbox",
            y: 140,
            text : { label : label, padding : 6 }
    });
    if (override != nil) tmpl.setValues(override);
    popdown(tipArgSelf);
    fgcommand("dialog-new", tmpl);
    fgcommand("dialog-show", tipArgSelf);
    currTimerSelf += 1;
    var thisTimerSelf = currTimerSelf;
    # Final argument 1 is a flag to use "real" time, not simulated time
    settimer(func { if(currTimerSelf == thisTimerSelf) { popdown(tipArgSelf) } }, delay, 1);
}
</syntaxhighlight>
[[Nasal scripting language#settimer loops|More information about using the settimer function to create loops.]]


=== srand() ===
=== srand() ===

Navigation menu