2,733
edits
Red Leader (talk | contribs) m (nocat) |
Red Leader (talk | contribs) (Update {{func link}} form) |
||
Line 79: | Line 79: | ||
|syntax = call(func[, args[, me[, locals[, error]]]); | |syntax = call(func[, args[, me[, locals[, error]]]); | ||
|source = {{simgear file|simgear/nasal/lib.c|l=247|t=Source}} | |source = {{simgear file|simgear/nasal/lib.c|l=247|t=Source}} | ||
|text = Calls the given function with the given arguments and returns the result. This function is very useful as it allows much more control over function calls and catches any errors or {{func link|die}} calls that would normally trigger run-time errors cancelling execution of the script otherwise. | |text = Calls the given function with the given arguments and returns the result. This function is very useful as it allows much more control over function calls and catches any errors or {{func link|die()}} calls that would normally trigger run-time errors cancelling execution of the script otherwise. | ||
|param1 = func | |param1 = func | ||
|param1text = Function to execute. | |param1text = Function to execute. | ||
Line 89: | Line 89: | ||
|param4text = A hash with key/value pairs that will be available to the called function, typically used as the namespace for the function to be called. | |param4text = A hash with key/value pairs that will be available to the called function, typically used as the namespace for the function to be called. | ||
|param5 = error | |param5 = error | ||
|param5text = A vector to append errors to. If the called function generates an error, the error, place, and line will be written to this. These errors can be printed using {{func link| | |param5text = A vector to append errors to. If the called function generates an error, the error, place, and line will be written to this. These errors can be printed using {{func link|printerror()|debug}}. | ||
|example1 = | |example1 = | ||
# prints "Called from call()" | # prints "Called from call()" | ||
Line 250: | Line 250: | ||
|syntax = compile(code[, filename]); | |syntax = compile(code[, filename]); | ||
|source = {{simgear file|simgear/nasal/lib.c|l=220|t=Source}} | |source = {{simgear file|simgear/nasal/lib.c|l=220|t=Source}} | ||
|text = Compiles the specified code string and returns a function object bound to the current lexical context. If there is an error, the function dies, with the argument to {{func link|die}} being '''filename'''. | |text = Compiles the specified code string and returns a function object bound to the current lexical context. If there is an error, the function dies, with the argument to {{func link|die()}} being '''filename'''. | ||
|param1 = code | |param1 = code | ||
|param1text = String containing Nasal code to be compiled. | |param1text = String containing Nasal code to be compiled. | ||
Line 345: | Line 345: | ||
|syntax = die(error); | |syntax = die(error); | ||
|source = {{simgear file|simgear/nasal/lib.c|l=288|t=Source}} | |source = {{simgear file|simgear/nasal/lib.c|l=288|t=Source}} | ||
|text = Terminates execution and unwinds the stack. The place and the line will be added to the '''error'''. This invokes the same internal exception handler used for internal runtime errors. Use this to signal fatal errors, or to implement exception handling. The error thrown (including internal runtime errors) can be caught with {{func link|call}}. | |text = Terminates execution and unwinds the stack. The place and the line will be added to the '''error'''. This invokes the same internal exception handler used for internal runtime errors. Use this to signal fatal errors, or to implement exception handling. The error thrown (including internal runtime errors) can be caught with {{func link|call()}}. | ||
|param1 = error | |param1 = error | ||
|param1text = String describing the error. | |param1text = String describing the error. | ||
Line 521: | Line 521: | ||
|syntax = sort(vector, function); | |syntax = sort(vector, function); | ||
|source = {{simgear file|simgear/nasal/lib.c|l=542|t=Source}} | |source = {{simgear file|simgear/nasal/lib.c|l=542|t=Source}} | ||
|text = Returns a vector containing the elements in the input '''vector''' sorted in according to the rule given by '''function'''. Implemented with the ANSI C {{func link|qsort()| | |text = Returns a vector containing the elements in the input '''vector''' sorted in according to the rule given by '''function'''. Implemented with the ANSI C {{func link|qsort()|link=http://www.cplusplus.com/reference/cstdlib/qsort/}}, <code>sort()</code> is stable. This means that if the rules in the first example are used, equal elements in the output vector will appear in the same relative order as they do in the input. It is run in a loop, so '''function''' is run several times. | ||
|param1 = vector | |param1 = vector | ||
|param1text = Input vector to sort. | |param1text = Input vector to sort. | ||
Line 581: | Line 581: | ||
|syntax = sprintf(format[, arg[, arg, [...]]]); | |syntax = sprintf(format[, arg[, arg, [...]]]); | ||
|source = {{simgear file|simgear/nasal/lib.c|l=355|t=Source}} | |source = {{simgear file|simgear/nasal/lib.c|l=355|t=Source}} | ||
|text = Creates and returns a string formatted using ANSI C {{ | |text = Creates and returns a string formatted using ANSI C {{func link|vsnprintf()|link=http://en.cppreference.com/w/c/io/vfprintf}} <ref> | ||
{{Cite web | {{Cite web | ||
|url = http://sourceforge.net/p/flightgear/simgear/ci/next/tree/simgear/nasal/lib.c#l308 | |url = http://sourceforge.net/p/flightgear/simgear/ci/next/tree/simgear/nasal/lib.c#l308 | ||
Line 688: | Line 688: | ||
|syntax = substr(string, start [, length]); | |syntax = substr(string, start [, length]); | ||
|source = {{simgear file|simgear/nasal/lib.c|l=129|t=Source}} | |source = {{simgear file|simgear/nasal/lib.c|l=129|t=Source}} | ||
|text = Similar the {{func link|subvec}}, but operates on strings. Computes and returns a substring. The first argument specifies a string, the second is the index of the start of a substring, the optional third argument specifies a length (the default is to return the rest of the string from the start). | |text = Similar the {{func link|subvec()}}, but operates on strings. Computes and returns a substring. The first argument specifies a string, the second is the index of the start of a substring, the optional third argument specifies a length (the default is to return the rest of the string from the start). | ||
|param1 = string | |param1 = string | ||
|param1text = String to return a substring from. | |param1text = String to return a substring from. | ||
Line 832: | Line 832: | ||
|syntax = abort(); | |syntax = abort(); | ||
|source = {{flightgear file|src/Scripting/NasalSys.cxx|l=565|t=Source}} | |source = {{flightgear file|src/Scripting/NasalSys.cxx|l=565|t=Source}} | ||
|text = This function is a wrapper for the C++ {{func link|abort()| | |text = This function is a wrapper for the C++ {{func link|abort()|link=http://www.cplusplus.com/reference/cstdlib/abort/}} function. It simply aborts FlightGear with an error, which varies depending on the operating system. This function should not really be used; instead, please use the "exit" [[Fgcommands|fgcommand]], which will exit FlightGear more gracefully (see example below). | ||
|example1text = This example will immediately stop FlightGear with an error, such as "FlightGear has stopped working." | |example1text = This example will immediately stop FlightGear with an error, such as "FlightGear has stopped working." | ||
|example1 = abort(); | |example1 = abort(); | ||
Line 863: | Line 863: | ||
|param2 = code | |param2 = code | ||
|param2text = The code that will be executed when the fgcommand is run. Must be a function. | |param2text = The code that will be executed when the fgcommand is run. Must be a function. | ||
|example1text = This example adds a new fgcommand and then runs it. Although it executes a simple {{func link|print}} statement, any valid Nasal code can be used. | |example1text = This example adds a new fgcommand and then runs it. Although it executes a simple {{func link|print()}} statement, any valid Nasal code can be used. | ||
|example1 = addcommand("myFGCmd", func { | |example1 = addcommand("myFGCmd", func { | ||
print("fgcommand 'myFGCmd' has been run."); | print("fgcommand 'myFGCmd' has been run."); | ||
Line 961: | Line 961: | ||
This function returns a vector containing waypoints between two given waypoints. The returned waypoints are ghosts, but can be accessed in the same way as a Nasal hash. See [[Nasal Flightplan]] for more information. | This function returns a vector containing waypoints between two given waypoints. The returned waypoints are ghosts, but can be accessed in the same way as a Nasal hash. See [[Nasal Flightplan]] for more information. | ||
|param1 = start | |param1 = start | ||
|param1text = Start waypoint, in the form of a waypoint ghost, such as that provided by {{func link|flightplan}}. | |param1text = Start waypoint, in the form of a waypoint ghost, such as that provided by {{func link|flightplan()}}. | ||
|param2 = end | |param2 = end | ||
|param2text = Same as above. | |param2text = Same as above. | ||
Line 990: | Line 990: | ||
|version = 3.2 | |version = 3.2 | ||
|commit = {{fgdata commit|8b16a7|t=commit}} | |commit = {{fgdata commit|8b16a7|t=commit}} | ||
|text = Returns either true if the condition evaluates as true, or aborts with a {{func link|die}} call, which can be customised. | |text = Returns either true if the condition evaluates as true, or aborts with a {{func link|die()}} call, which can be customised. | ||
|param1 = condition | |param1 = condition | ||
|param1text = Condition to evaluate. | |param1text = Condition to evaluate. | ||
|param2 = message | |param2 = message | ||
|param2text = Optional message that will be used in any {{func link|die}} call. Defaults to "assertion failed!" | |param2text = Optional message that will be used in any {{func link|die()}} call. Defaults to "assertion failed!" | ||
|example1 = var a = 1; | |example1 = var a = 1; | ||
var b = 2; | var b = 2; | ||
Line 1,034: | Line 1,034: | ||
You should not use <code>cmdarg()</code> in places other than those stated above. Although it won't cause an error, it will return the value of the last legitimate <code>cmdarg()</code> call. | You should not use <code>cmdarg()</code> in places other than those stated above. Although it won't cause an error, it will return the value of the last legitimate <code>cmdarg()</code> call. | ||
Also, you should not delay <code>cmdarg()</code> using {{func link|maketimer}}, {{func link|settimer}} or {{func link|setlistener}}, because it will return an unrelated property. | Also, you should not delay <code>cmdarg()</code> using {{func link|maketimer()}}, {{func link|settimer()}} or {{func link|setlistener()}}, because it will return an unrelated property. | ||
|example1 = fgcommand("dialog-show", {"dialog-name": "cmdarg-demo"}); | |example1 = fgcommand("dialog-show", {"dialog-name": "cmdarg-demo"}); | ||
|example1text = <br>This example demonstrates the usage of <code>cmdarg()</code> in a binding. Save the below XML snippet as <tt>[[$FG_ROOT]]/gui/dialogs/cmdarg-demo.xml</tt>. Then run the Nasal snippet below in your [[Nasal Console]]. Upon clicking {{button|Close}}, a message will be printed sowing the root of the binding in the Property Tree. | |example1text = <br>This example demonstrates the usage of <code>cmdarg()</code> in a binding. Save the below XML snippet as <tt>[[$FG_ROOT]]/gui/dialogs/cmdarg-demo.xml</tt>. Then run the Nasal snippet below in your [[Nasal Console]]. Upon clicking {{button|Close}}, a message will be printed sowing the root of the binding in the Property Tree. | ||
Line 1,209: | Line 1,209: | ||
var fp = flightplan(); | var fp = flightplan(); | ||
fp.appendWP(wp); | fp.appendWP(wp); | ||
|example5text = Creates a new waypoint and adds it to the flight plan. Waypoints of the type "pseudo" are then removed from the flight plan, including the new waypoint. The {{func link|print}} statements show this. | |example5text = Creates a new waypoint and adds it to the flight plan. Waypoints of the type "pseudo" are then removed from the flight plan, including the new waypoint. The {{func link|print()}} statements show this. | ||
|example5 = var pos = geo.aircraft_position().apply_course_distance(getprop("/orientation/heading-deg"), 1000); | |example5 = var pos = geo.aircraft_position().apply_course_distance(getprop("/orientation/heading-deg"), 1000); | ||
var wp = createWP(pos, "NEWWP", "pseudo"); | var wp = createWP(pos, "NEWWP", "pseudo"); | ||
Line 1,233: | Line 1,233: | ||
var fp = flightplan(); | var fp = flightplan(); | ||
fp.appendWP(wp); | fp.appendWP(wp); | ||
|example2text = Creates a new waypoint and appends it to the flight plan. This way point is then removed; the {{func link|print}} statements prove this. | |example2text = Creates a new waypoint and appends it to the flight plan. This way point is then removed; the {{func link|print()}} statements prove this. | ||
|example2 = var apt = airportinfo("KSFO"); | |example2 = var apt = airportinfo("KSFO"); | ||
var wp = createWPFrom(apt, "pseudo"); | var wp = createWPFrom(apt, "pseudo"); | ||
Line 1,277: | Line 1,277: | ||
|syntax = fgcommand(cmd[, args]); | |syntax = fgcommand(cmd[, args]); | ||
|source = {{flightgear file|src/Scripting/NasalSys.cxx|l=456|t=Part 1}} {{!}} {{fgdata file|Nasal/globals.nas|t=Part 2}} | |source = {{flightgear file|src/Scripting/NasalSys.cxx|l=456|t=Part 1}} {{!}} {{fgdata file|Nasal/globals.nas|t=Part 2}} | ||
|text = Runs an fgcommand. See also {{readme file|commands}} and [[Bindings]] for more information. See {{flightgear file|src/Main/fg_commands.cxx|l=1425}} for the full list of fgcommands. Note that fgcommands generated by {{func link|addcommand}} can also be run using this function. Also, the full list of fgcommands depends on the version of FlightGear you have. Returns 1 (true) if the fgcommand succeeded or 0 (false) if it failed. | |text = Runs an fgcommand. See also {{readme file|commands}} and [[Bindings]] for more information. See {{flightgear file|src/Main/fg_commands.cxx|l=1425}} for the full list of fgcommands. Note that fgcommands generated by {{func link|addcommand()}} can also be run using this function. Also, the full list of fgcommands depends on the version of FlightGear you have. Returns 1 (true) if the fgcommand succeeded or 0 (false) if it failed. | ||
|param1 = cmd | |param1 = cmd | ||
|param1text = String that is the name of the command that is to be run. | |param1text = String that is the name of the command that is to be run. | ||
Line 1,692: | Line 1,692: | ||
|syntax = logprint(priority[, msg[, msg[, ...]]]); | |syntax = logprint(priority[, msg[, msg[, ...]]]); | ||
|source = {{flightgear file|src/Scripting/NasalSys.cxx|l=431|t=Source}} | |source = {{flightgear file|src/Scripting/NasalSys.cxx|l=431|t=Source}} | ||
|text = Concatenates a message and logs it with a given priority level. Unlike {{func link|print}} and {{func link|printlog}}, message outputted by this function will be logged in your <code>[[Commonly used debugging tools#fgfs.log|fgfs.log]]</code> file as coming from the Nasal file itself rather than from {{flightgear file|src/Scripting/NasalSys.cxx}}. | |text = Concatenates a message and logs it with a given priority level. Unlike {{func link|print()}} and {{func link|printlog()}}, message outputted by this function will be logged in your <code>[[Commonly used debugging tools#fgfs.log|fgfs.log]]</code> file as coming from the Nasal file itself rather than from {{flightgear file|src/Scripting/NasalSys.cxx}}. | ||
|param1 = priority | |param1 = priority | ||
|param1text = Number specifying the priority level of the outputted message: | |param1text = Number specifying the priority level of the outputted message: | ||
Line 1,750: | Line 1,750: | ||
* '''singleShot''': Bool showing whether the timer is only to be run once, or continuously until told to stop. Can be both set and read from (see examples). | * '''singleShot''': Bool showing whether the timer is only to be run once, or continuously until told to stop. Can be both set and read from (see examples). | ||
* '''isRunning''': Read-only bool telling whether the timer is currently running. | * '''isRunning''': Read-only bool telling whether the timer is currently running. | ||
Unlike {{func link|settimer}}, which it replaces, <code>maketimer()</code> provides more control over the timer. In addition, it can help reduce memory usage. | Unlike {{func link|settimer()}}, which it replaces, <code>maketimer()</code> provides more control over the timer. In addition, it can help reduce memory usage. | ||
|param1 = interval | |param1 = interval | ||
|param1text = Interval in seconds for the timer. | |param1text = Interval in seconds for the timer. | ||
Line 1,989: | Line 1,989: | ||
|syntax = printf(format[, arg[, arg, [...]]]); | |syntax = printf(format[, arg[, arg, [...]]]); | ||
|source = {{fgdata file|Nasal/globals.nas|t=Source}} | |source = {{fgdata file|Nasal/globals.nas|t=Source}} | ||
|text = Creates and prints a formatted string. For a description of its arguments, see {{func link|sprintf}} (it is, in fact, implemented using <code>sprintf()</code>). | |text = Creates and prints a formatted string. For a description of its arguments, see {{func link|sprintf()}} (it is, in fact, implemented using <code>sprintf()</code>). | ||
|example1 = printf("In hexadecimal, 100000 = %X", 186A0); # prints "In hexadecimal, 100000 = 186A0" | |example1 = printf("In hexadecimal, 100000 = %X", 186A0); # prints "In hexadecimal, 100000 = 186A0" | ||
}} | }} | ||
Line 2,044: | Line 2,044: | ||
{{note|It is good practice to remove listeners when they are not required anymore. This prevents the listeners reducing FlightGear's run performance.}} | {{note|It is good practice to remove listeners when they are not required anymore. This prevents the listeners reducing FlightGear's run performance.}} | ||
|param1 = id | |param1 = id | ||
|param1text = ID of listener as returned by {{func link|setlistener}}. | |param1text = ID of listener as returned by {{func link|setlistener()}}. | ||
|example1 = var ls = setlistener("/sim/test", func(){ | |example1 = var ls = setlistener("/sim/test", func(){ | ||
print("Property '/sim/test' has been changed"); | print("Property '/sim/test' has been changed"); | ||
Line 2,075: | Line 2,075: | ||
|private = _setlistener() | |private = _setlistener() | ||
|source = {{flightgear file|src/Scripting/Nasal|l=499|t=Part 1}} {{!}} {{flightgear file|src/Scripting/Nasal|l=1350|t=Part 2}}<br>''Listener implemented using the {{API Link|flightgear|class|FGNasalListener}} class.'' | |source = {{flightgear file|src/Scripting/Nasal|l=499|t=Part 1}} {{!}} {{flightgear file|src/Scripting/Nasal|l=1350|t=Part 2}}<br>''Listener implemented using the {{API Link|flightgear|class|FGNasalListener}} class.'' | ||
|text = Creates a listener which will be triggered when the given property is changed (depending on the '''type'''). A unique integer ID is returned; this can later be used as the argument to {{func link|removelistener}}. | |text = Creates a listener which will be triggered when the given property is changed (depending on the '''type'''). A unique integer ID is returned; this can later be used as the argument to {{func link|removelistener()}}. | ||
{{note|Listeners are known to be a source of resource leaks. To avoid this, please take measures such as: | {{note|Listeners are known to be a source of resource leaks. To avoid this, please take measures such as: | ||
* Using {{func link|removelistener}} when they are not needed any more. | * Using {{func link|removelistener()}} when they are not needed any more. | ||
* Using a single initialization listener. | * Using a single initialization listener. | ||
* Avoiding tying listeners to properties that are rapidly updated (e.g., many times per frame). | * Avoiding tying listeners to properties that are rapidly updated (e.g., many times per frame). | ||
Line 2,133: | Line 2,133: | ||
|source = {{flightgear file|src/Scripting/NasalSys.cxx|l=499|t=Part 1}} {{!}} {{flightgear file|src/Scripting/NasalSys.cxx|l=1286|t=Part 2}} | |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>. | |text = Runs the given function a specified amount of seconds after the current time. Returns <code>'''nil'''</code>. | ||
{{note|Improper use of <code>settimer()</code> may cause resource leaks. It is also highly recommended that the newer {{func link|maketimer}} should be used instead of this function.}} | {{note|Improper use of <code>settimer()</code> may cause resource leaks. It is also highly recommended that the newer {{func link|maketimer()}} should be used instead of this function.}} | ||
|param1 = function | |param1 = function | ||
|param1text = Mandatory function that will be called. It may be necessary to enclose code in an anonymous function (see example). | |param1text = Mandatory function that will be called. It may be necessary to enclose code in an anonymous function (see example). | ||
Line 2,158: | Line 2,158: | ||
|syntax = srand(); | |syntax = srand(); | ||
|source = {{flightgear file|src/Scripting/NasalSys.cxx|l=559|t=Source}} | |source = {{flightgear file|src/Scripting/NasalSys.cxx|l=559|t=Source}} | ||
|text = Makes the pseudorandom number generator (see {{func link|rand}}) generate a new {{wikipedia|random seed|noicon=1}} based on time. Returns 0. | |text = Makes the pseudorandom number generator (see {{func link|rand()}}) generate a new {{wikipedia|random seed|noicon=1}} based on time. Returns 0. | ||
}} | }} | ||
=== systime() === | === systime() === | ||
Line 2,165: | Line 2,165: | ||
|source = {{flightgear file|src/Scripting/NasalSys.cxx|l=770|t=Source}} | |source = {{flightgear file|src/Scripting/NasalSys.cxx|l=770|t=Source}} | ||
|text = Returns the {{wikipedia|Unix time}} (seconds since since 00:00:00 UTC, 1/1/1970) as a floating point number with high resolution. This function is useful for benchmarking purposes (see example 2). | |text = Returns the {{wikipedia|Unix time}} (seconds since since 00:00:00 UTC, 1/1/1970) as a floating point number with high resolution. This function is useful for benchmarking purposes (see example 2). | ||
{{note|1=High resolution timers under Windows can produce inaccurate or fixed sub-millisecond results.<ref>{{cite web|url=http://forum.flightgear.org/viewtopic.php?f=30&t=29259|title=Nasal: systime() ??!?|author=Necolatis|date=Apr 2nd, 2016}}</ref> This is due to the underlying {{func link|GetSystemTimeAsFileTime()| | {{note|1=High resolution timers under Windows can produce inaccurate or fixed sub-millisecond results.<ref>{{cite web|url=http://forum.flightgear.org/viewtopic.php?f=30&t=29259|title=Nasal: systime() ??!?|author=Necolatis|date=Apr 2nd, 2016}}</ref> This is due to the underlying {{func link|GetSystemTimeAsFileTime()|link=https://msdn.microsoft.com/en-us/library/windows/desktop/ms724397(v=vs.85).aspx}} API call, which depends on hardware availability of suitable high resolution timers. See also [https://msdn.microsoft.com/en-us/library/windows/desktop/dn553408(v=vs.85).aspx Acquiring high-resolution time stamps]}} | ||
|example1 = print("Unix time: ", systime()); # prints Unix time | |example1 = print("Unix time: ", systime()); # prints Unix time | ||
|example2 = var myFunc = func(){ | |example2 = var myFunc = func(){ |