Nasal library: Difference between revisions

Jump to navigation Jump to search
no edit summary
No edit summary
Line 324: Line 324:
}}
}}


=== delete() ===
 
===contains()===
{{Note|Vector support of contains is currently only available on SimGear git next, starting commit ee39abbd3b70c9b6d5e3a1c4ccedddaac1a92b11}}{{Nasal doc
|syntax = contains(vector, item);
|source = {{simgear file|simgear/nasal/lib.c|l=184|t=Source}}
|text = Returns 1 (True) if the vector contains the specified item, or 0 (False) if not.
|param1 = vector
|param1text = The vector to search in.
|param2 = item
|param2text = The object to be searched for in the vector.
|example1 =
# Initialize a hash
var vec = ["element", "foo"];
print(contains(hash, "element") ? "Yes" : "No"); # This will print "Yes"
|example2 =
# Initialize a hash
var vec = ["element", "foo"];
print(contains(hash, "element2") ? "Yes" : "No"); # This will print "No"
}}
 
===delete() ===
{{Nasal doc
{{Nasal doc
|syntax = delete(hash, key);
|syntax = delete(hash, key);
Line 343: Line 363:
}}
}}


=== die() ===
===die()===
{{Nasal doc
{{Nasal doc
|syntax = die(error);
|syntax = die(error);
Line 357: Line 377:
}}
}}


=== find() ===
=== find()===
{{Nasal doc
{{Nasal doc
|syntax = find(needle, haystack);
|syntax = find(needle, haystack);
Line 371: Line 391:
}}
}}


=== ghosttype() ===
===ghosttype()===
{{Nasal doc
{{Nasal doc
|syntax = ghosttype(ghost);
|syntax = ghosttype(ghost);
Line 381: Line 401:
}}
}}


=== id() ===
===id()===
{{Nasal doc
{{Nasal doc
|syntax = id(object);
|syntax = id(object);
Line 391: Line 411:
}}
}}


=== int() ===
===int() ===
{{Nasal doc
{{Nasal doc
|syntax = int(number);
|syntax = int(number);
Line 403: Line 423:
}}
}}


=== keys() ===
===keys()===
{{Nasal doc
{{Nasal doc
|syntax = keys(hash);
|syntax = keys(hash);
Line 419: Line 439:
}}
}}


=== left() ===
===left()===
{{Nasal doc
{{Nasal doc
|syntax = left(string, length);
|syntax = left(string, length);
Line 433: Line 453:
}}
}}


=== num() ===
=== num()===
{{Nasal doc
{{Nasal doc
|syntax = num(number);
|syntax = num(number);
Line 445: Line 465:
}}
}}


=== pop() ===
===pop()===
{{Nasal doc
{{Nasal doc
|syntax = pop(vector);
|syntax = pop(vector);
Line 464: Line 484:
}}
}}


=== right() ===
===right()===
{{Nasal doc
{{Nasal doc
|syntax = right(string, length);
|syntax = right(string, length);
Line 478: Line 498:
}}
}}


=== setsize() ===
=== setsize()===
{{Nasal doc
{{Nasal doc
|syntax = setsize(vector, size);
|syntax = setsize(vector, size);
Line 497: Line 517:
}}
}}


=== size() ===
===size()===
{{Nasal doc
{{Nasal doc
|syntax = size(object);
|syntax = size(object);
Line 519: Line 539:
}}
}}


=== sort() ===
=== sort()===
{{Nasal doc
{{Nasal doc
|syntax = sort(vector, function);
|syntax = sort(vector, function);
Line 570: Line 590:
}}
}}


=== split() ===
=== split()===
{{Nasal doc
{{Nasal doc
|syntax = split(delimiter, string);
|syntax = split(delimiter, string);
Line 584: Line 604:
}}
}}


=== sprintf() ===
===sprintf()===
{{Nasal doc
{{Nasal doc
|syntax = sprintf(format[, arg[, arg, [...]]]);
|syntax = <nowiki>sprintf(format[, arg[, arg, [...]]]);</nowiki>
|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 {{func link|vsnprintf()|link=http://en.cppreference.com/w/c/io/vfprintf}} <ref>
|text = Creates and returns a string formatted using ANSI C {{func link|vsnprintf()|link=http://en.cppreference.com/w/c/io/vfprintf}} <ref>
Line 683: Line 703:
}}
}}


=== streq() ===
===streq()===
{{Nasal doc
{{Nasal doc
|syntax = streq(a, b);
|syntax = streq(a, b);
Line 698: Line 718:
}}
}}


=== substr() ===
===substr()===
{{Nasal doc
{{Nasal doc
|syntax = substr(string, start [, length]);
|syntax = substr(string, start [, length]);
Line 716: Line 736:
}}
}}


=== subvec() ===
===subvec()===
{{Nasal doc
{{Nasal doc
|syntax = subvec(vector, start[, length]);
|syntax = subvec(vector, start[, length]);
Line 743: Line 763:
}}
}}


=== typeof() ===
=== typeof()===
{{Nasal doc
{{Nasal doc
|syntax = typeof(object);
|syntax = typeof(object);
Line 836: Line 856:
}} -->
}} -->


== Extension functions ==
==Extension functions==
The '''extension functions''' are global functions that have been added to Nasal since its integration into FlightGear. Unlike the core library functions, they are generally specifically designed to interact directly with FlightGear. Extension functions come from three source files:
The '''extension functions''' are global functions that have been added to Nasal since its integration into FlightGear. Unlike the core library functions, they are generally specifically designed to interact directly with FlightGear. Extension functions come from three source files:
* {{flightgear file|src/Scripting/NasalPositioned.cxx}}
*{{flightgear file|src/Scripting/NasalPositioned.cxx}}
* {{flightgear file|src/Scripting/NasalSys.cxx}}
*{{flightgear file|src/Scripting/NasalSys.cxx}}
* {{fgdata file|Nasal/globals.nas}}
*{{fgdata file|Nasal/globals.nas}}


=== abort() ===
===abort()===
{{Nasal doc
{{Nasal doc
|syntax = abort();
|syntax = abort();
Line 864: Line 884:
}}
}}


=== aircraftToCart() ===
===aircraftToCart() ===
This new function in FG 2017.2.1 takes coordinates in aircraft structural coordinate system, and translate them into geocentric coordinates.
This new function in FG 2017.2.1 takes coordinates in aircraft structural coordinate system, and translate them into geocentric coordinates.
Example for (5,6,7):
Example for (5,6,7):
Line 881: Line 901:
</syntaxhighlight>
</syntaxhighlight>


=== addcommand() ===
===addcommand() ===
{{Nasal doc
{{Nasal doc
|syntax = addcommand(name, code);
|syntax = addcommand(name, code);
Line 907: Line 927:
}}
}}


=== airportinfo() ===
===airportinfo()===
{{Nasal doc
{{Nasal doc
|syntax = airportinfo();
|syntax = airportinfo();
Line 1,007: Line 1,027:
     print(info.runways[rwy].stopway);
     print(info.runways[rwy].stopway);
     print(info.runways[rwy].threshold);
     print(info.runways[rwy].threshold);
}
<nowiki>}</nowiki>
}}
}}


=== airwaysRoute() ===
===airwaysRoute() ===
{{Nasal doc
{{Nasal doc
|syntax = airwaysRoute(start, end[, type]);
|syntax = airwaysRoute(start, end[, type]);
Line 1,037: Line 1,057:
foreach(var wp; rt){
foreach(var wp; rt){
     print(wp.wp_name); # print the waypoints in the computed route
     print(wp.wp_name); # print the waypoints in the computed route
}
<nowiki>}</nowiki>
}}
}}


=== airway() ===
===airway()===


{{Nasal doc
{{Nasal doc
Line 1,048: Line 1,068:
This function returns a ghost containing an airway of a specified id.
This function returns a ghost containing an airway of a specified id.
|param1 = ident
|param1 = ident
|param1text = Identifier of airway
|param1text = a Positioned ghost (leg, navaid, airport) and so on, passed to the search function.
|param2 = pos
|param2 = pos
|param1text = a Positioned ghost (leg, navaid, airport) and so on, passed to the search function.
}}
}}


=== assert() ===
===assert()===
{{Nasal doc
{{Nasal doc
|syntax = assert(condition[, message]);
|syntax = assert(condition[, message]);
Line 1,072: Line 1,091:
}}
}}


=== carttogeod() ===
===carttogeod()===
{{Nasal doc
{{Nasal doc
|syntax = carttogeod(x, y, z);
|syntax = carttogeod(x, y, z);
Line 1,089: Line 1,108:
}}
}}


=== cmdarg() ===
===cmdarg()===
{{Nasal doc
{{Nasal doc
|private = _cmdarg()
|private = _cmdarg()
Line 1,176: Line 1,195:
}}
}}


=== courseAndDistance() ===
===courseAndDistance()===
{{Nasal doc
{{Nasal doc
|syntax = courseAndDistance(to);
|syntax = courseAndDistance(to);
Line 1,224: Line 1,243:
}}
}}


=== createFlightplan() ===
===createFlightplan()===
{{Nasal doc
{{Nasal doc
|syntax = createFlightplan(path);
|syntax = createFlightplan(path);
Line 1,237: Line 1,256:
}}
}}


=== createDiscontinuity() ===
===createDiscontinuity()===
{{Nasal doc
{{Nasal doc
|syntax = createDiscontinuity();
|syntax = createDiscontinuity();
Line 1,245: Line 1,264:
|commit = {{flightgear commit|caead6|t=commit}}
|commit = {{flightgear commit|caead6|t=commit}}
}}
}}
=== createViaTo() ===
===createViaTo()===
{{Nasal doc
{{Nasal doc
|syntax = createViaTo(airway, waypoint);
|syntax = createViaTo(airway, waypoint);
Line 1,259: Line 1,278:
* An <code>airport</code>, <code>navaid</code>, <code>runway</code>, or <code>fix</code> ghost object.
* An <code>airport</code>, <code>navaid</code>, <code>runway</code>, or <code>fix</code> ghost object.
}}
}}
=== createWP() ===
===createWP()===
{{Nasal doc
{{Nasal doc
|syntax = createWP(pos, name[, flag]);
|syntax = createWP(pos, name[, flag]);
Line 1,301: Line 1,320:
}}
}}


=== createWPFrom() ===
===createWPFrom()===
{{Nasal doc
{{Nasal doc
|syntax = createWPFrom(object[, flag]);
|syntax = createWPFrom(object[, flag]);
Line 1,326: Line 1,345:
}}
}}


=== defined() ===
===defined()===
{{Nasal doc
{{Nasal doc
|syntax = defined(symbol);
|syntax = defined(symbol);
Line 1,341: Line 1,360:
}}
}}


=== directory() ===
===directory()===
{{Nasal doc
{{Nasal doc
|syntax = directory(path);
|syntax = directory(path);
Line 1,355: Line 1,374:
}}
}}


=== fgcommand() ===
===fgcommand()===
{{Nasal doc
{{Nasal doc
|syntax = fgcommand(cmd[, args]);
|syntax = fgcommand(cmd[, args]);
Line 1,375: Line 1,394:
}}
}}


=== findAirportsByICAO() ===
===findAirportsByICAO() ===
{{Nasal doc
{{Nasal doc
|syntax = findAirportsByICAO(search[, type]);
|syntax = findAirportsByICAO(search[, type]);
Line 1,398: Line 1,417:
} else {
} else {
     print("Airport does exist");
     print("Airport does exist");
}
<nowiki>}</nowiki>
}}
}}


=== findAirportsWithinRange() ===
===findAirportsWithinRange()===
{{Nasal doc
{{Nasal doc
|syntax = findAirportsWithinRange([pos, ]range[, type]);
|syntax = findAirportsWithinRange([pos, ]range[, type]);
Line 1,432: Line 1,451:
foreach(var apt; apts){
foreach(var apt; apts){
     print(apt.name, " (", apt.id, ")");
     print(apt.name, " (", apt.id, ")");
}
<nowiki>}</nowiki>
}}
}}


=== findFixesByID() ===
===findFixesByID()===
{{Nasal doc
{{Nasal doc
|syntax = findFixesByID([pos, ]id);
|syntax = findFixesByID([pos, ]id);
Line 1,460: Line 1,479:
}}
}}


=== findNavaidByFrequency() ===
===findNavaidByFrequency()===
{{Nasal doc
{{Nasal doc
|syntax = findNavaidByFrequency([pos, ]freq[, type]);
|syntax = findNavaidByFrequency([pos, ]freq[, type]);
Line 1,487: Line 1,506:
}}
}}


=== findNavaidsByFrequency() ===
=== findNavaidsByFrequency()===
{{Nasal doc
{{Nasal doc
|syntax = findNavaidsByFrequency([pos, ]freq[, type]);
|syntax = findNavaidsByFrequency([pos, ]freq[, type]);
Line 1,515: Line 1,534:
     if(navaid.course) print("Course: ", navaid.course);
     if(navaid.course) print("Course: ", navaid.course);
     print("--");
     print("--");
}
<nowiki>}</nowiki>
}}
}}


=== findNavaidsByID() ===
===findNavaidsByID()===
{{Nasal doc
{{Nasal doc
|syntax = findNavaidsByID([pos, ]id[, type]);
|syntax = findNavaidsByID([pos, ]id[, type]);
Line 1,546: Line 1,565:
}}
}}


=== findNavaidsWithinRange() ===
===findNavaidsWithinRange()===
{{Nasal doc
{{Nasal doc
|syntax = findNavaidsWithinRange([pos, ]range[, type]);
|syntax = findNavaidsWithinRange([pos, ]range[, type]);
Line 1,571: Line 1,590:
foreach(var nav; navs){
foreach(var nav; navs){
     print(nav.name, " (ID: ", nav.id, ")");
     print(nav.name, " (ID: ", nav.id, ")");
}
<nowiki>}</nowiki>
}}
}}


=== finddata() ===
===finddata()===
{{Nasal doc
{{Nasal doc
|syntax = finddata(path);
|syntax = finddata(path);
Line 1,589: Line 1,608:
}}
}}


=== flightplan() ===
===flightplan()===
{{Nasal doc
{{Nasal doc
|syntax = flightplan([path]);
|syntax = flightplan([path]);
Line 1,605: Line 1,624:
for(var i = 0; i < fp.getPlanSize(); i += 1){
for(var i = 0; i < fp.getPlanSize(); i += 1){
     print(fp.getWP(i).id);
     print(fp.getWP(i).id);
}
<nowiki>}</nowiki>
}}
}}


=== geodinfo() ===
===geodinfo()===
{{Nasal doc
{{Nasal doc
|syntax = geodinfo(lat, lon[, max_alt]);
|syntax = geodinfo(lat, lon[, max_alt]);
Line 1,637: Line 1,656:
if (info != nil and info[1] != nil) {
if (info != nil and info[1] != nil) {
     print("The ground underneath the aircraft is ", info[1].solid == 1 ? "solid." : "water.");
     print("The ground underneath the aircraft is ", info[1].solid == 1 ? "solid." : "water.");
}
<nowiki>}</nowiki>
}}
}}


Line 1,657: Line 1,676:
}}
}}


=== get_cart_ground_intersection() ===
=== get_cart_ground_intersection()===
Introduced in 2017.2.1, see [[Terrain Detection]].
Introduced in 2017.2.1, see [[Terrain Detection]].


=== getprop() ===
===getprop()===
{{Nasal doc
{{Nasal doc
|syntax = getprop(path[, path[, ...]]);
|syntax = <nowiki>getprop(path[, path[, ...]]);</nowiki>
|source = {{flightgear file|src/Scripting/NasalSys.cxx|l=345|t=Source}}
|source = {{flightgear file|src/Scripting/NasalSys.cxx|l=345|t=Source}}
|text = Returns the value of a node in the [[Property Tree]] or <code>'''nil'''</code> if the node does not exist or the value is not a number (NaN).
|text = Returns the value of a node in the [[Property Tree]] or <code>'''nil'''</code> if the node does not exist or the value is not a number (NaN).
Line 1,675: Line 1,694:
|example3 = for(var i = 0; i < 8; i += 1){
|example3 = for(var i = 0; i < 8; i += 1){
     print("View #", i + 1, " is named ", getprop("/sim/view[" ~ i ~ "]/name"));
     print("View #", i + 1, " is named ", getprop("/sim/view[" ~ i ~ "]/name"));
}
<nowiki>}</nowiki>
}}
}}
==== See also ====
==== See also====
{{note| If you have to read/write the same property multiple times (e.g. in an update loop), it is more efficient to use a node object:  
{{note| If you have to read/write the same property multiple times (e.g. in an update loop), it is more efficient to use a node object:  
To get a Node rather than its value, use <code>props.globals.getNode()</code> - see [[Nasal_library/props]]. }}
To get a Node rather than its value, use <code>props.globals.getNode()</code> - see [[Nasal_library/props]]. }}


=== greatCircleMove() ===
===greatCircleMove()===
{{Nasal doc
{{Nasal doc
|syntax = greatCircleMove([pos, ]course, dist);
|syntax = greatCircleMove([pos, ]course, dist);
Line 1,704: Line 1,723:
}}
}}


=== interpolate() ===
===interpolate()===
{{Nasal doc
{{Nasal doc
|private = _interpolate()
|private = _interpolate()
|syntax = interpolate(prop, value1, time1[, value2, time2[, ...]]);
|syntax = <nowiki>interpolate(prop, value1, time1[, value2, time2[, ...]]);</nowiki>
|source = {{flightgear file|src/Scripting/NasalSys.cxx|l=522|t=Part 1}} {{!}} {{fgdata file|Nasal/globals.nas|t=Part 2}}
|source = {{flightgear file|src/Scripting/NasalSys.cxx|l=522|t=Part 1}} {{!}} {{fgdata file|Nasal/globals.nas|t=Part 2}}
|text = Linearly interpolates a node in the property tree to a given value in a specified time. The value/time pairs will be run one after the other in the order that they are passed to the function. Note that the interpolation will continue even when the simulation is paused.
|text = Linearly interpolates a node in the property tree to a given value in a specified time. The value/time pairs will be run one after the other in the order that they are passed to the function. Note that the interpolation will continue even when the simulation is paused.
Line 1,727: Line 1,746:
if (dist == 1) {
if (dist == 1) {
     interpolate(prop, 1, dist / 0.2);
     interpolate(prop, 1, dist / 0.2);
}
<nowiki>}</nowiki>
}}
}}


=== isa() ===
===isa()===
{{Nasal doc
{{Nasal doc
|syntax = isa(object, class);
|syntax = isa(object, class);
Line 1,776: Line 1,795:
}}
}}


=== logprint() ===
===logprint()===
{{Nasal doc
{{Nasal doc
|syntax = logprint(priority[, msg[, msg[, ...]]]);
|syntax = <nowiki>logprint(priority[, msg[, msg[, ...]]]);</nowiki>
|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}}.
Line 1,807: Line 1,826:
LOG_BULK, LOG_WARN, LOG_DEBUG, LOG_INFO, LOG_ALERT, DEV_WARN, DEV_ALERT }}
LOG_BULK, LOG_WARN, LOG_DEBUG, LOG_INFO, LOG_ALERT, DEV_WARN, DEV_ALERT }}


=== magvar() ===
===magvar() ===
{{Nasal doc
{{Nasal doc
|syntax = magvar([pos]);
|syntax = magvar([pos]);
Line 1,830: Line 1,849:
}}
}}


=== maketimer() ===
===maketimer() ===
{{Nasal doc
{{Nasal doc
|syntax = maketimer(interval[, self], function);
|syntax = maketimer(interval[, self], function);
Line 1,875: Line 1,894:
}}
}}


=== maketimestamp() ===
===maketimestamp()===
{{Nasal doc
{{Nasal doc
|syntax = maketimestamp()
|syntax = maketimestamp()
Line 1,893: Line 1,912:
}}
}}


=== md5() ===
===md5()===
{{Nasal doc
{{Nasal doc
|syntax = md5(string);
|syntax = md5(string);
Line 1,906: Line 1,925:
}}
}}


=== navinfo() ===
===navinfo()===
{{Nasal doc
{{Nasal doc
|syntax = navinfo(lat, lon, type, id);
|syntax = navinfo(lat, lon, type, id);
Line 1,923: Line 1,942:
}}
}}


=== parse_markdown() ===
===parse_markdown()===
{{Nasal doc
{{Nasal doc
|syntax = parse_markdown(markdown);
|syntax = parse_markdown(markdown);
Line 2,037: Line 2,056:
}}
}}


=== parsexml() ===
===parsexml()===
{{Nasal doc
{{Nasal doc
|syntax = parsexml(path[, start[, end[, data[, pro_ins]]]]);
|syntax = <nowiki>parsexml(path[, start[, end[, data[, pro_ins]]]]);</nowiki>
|source = {{flightgear file|src/Scripting/NasalSys.cxx|l=710|t=Source}}
|source = {{flightgear file|src/Scripting/NasalSys.cxx|l=710|t=Source}}
|text = This function is an interface into the built-in [http://expat.sourceforge.net/ Expat XML parser]. The absolute path to the file is returned as string, or <code>'''nil'''</code> is returned on error.
|text = This function is an interface into the built-in [http://expat.sourceforge.net/ Expat XML parser]. The absolute path to the file is returned as string, or <code>'''nil'''</code> is returned on error.
Line 2,085: Line 2,104:
}}
}}


=== print() ===
===print()===
{{Note|As of 07/2020, we are in the process of slowly deprecating and removing Nasal print() (in fgdata) where possible in favor of log [[#logprint()]] which takes a priority level.
{{Note|As of 07/2020, we are in the process of slowly deprecating and removing Nasal print() (in fgdata) where possible in favor of log [[#logprint()]] which takes a priority level.


Line 2,091: Line 2,110:


{{Nasal doc
{{Nasal doc
|syntax = print(data[, data[, ...]]);
|syntax = <nowiki>print(data[, data[, ...]]);</nowiki>
|source = {{flightgear file|src/Scripting/NasalSys.cxx|l=415|t=Source}}
|source = {{flightgear file|src/Scripting/NasalSys.cxx|l=415|t=Source}}
|text = Concatenates its arguments and then prints it to the terminal and the [[Commonly used debugging tools#fgfs.log|log]]. Note that a newline is automatically added.
|text = Concatenates its arguments and then prints it to the terminal and the [[Commonly used debugging tools#fgfs.log|log]]. Note that a newline is automatically added.
Line 2,102: Line 2,121:
=== printf() ===
=== printf() ===
{{Nasal doc
{{Nasal doc
|syntax = printf(format[, arg[, arg, [...]]]);
|syntax = <nowiki>printf(format[, arg[, arg, [...]]]);</nowiki>
|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>).
Line 2,108: Line 2,127:
}}
}}


=== printlog() ===
===printlog()===
{{Nasal doc
{{Nasal doc
|syntax = printlog(level, data[, data[, ...]]);
|syntax = <nowiki>printlog(level, data[, data[, ...]]);</nowiki>
|source = {{fgdata file|Nasal/globals.nas|t=Source}}
|source = {{fgdata file|Nasal/globals.nas|t=Source}}
|text = Prints the given message with the given log level. If the log level is higher or equal to <code>/sim/logging/priority</code>, it is printed.
|text = Prints the given message with the given log level. If the log level is higher or equal to <code>/sim/logging/priority</code>, it is printed.
Line 2,121: Line 2,140:
}}
}}


=== rand() ===
===rand()===
{{Nasal doc
{{Nasal doc
|syntax = rand();
|syntax = rand();
Line 2,129: Line 2,148:
}}
}}


=== registerFlightPlanDelegate() ===
===registerFlightPlanDelegate()===
{{Nasal doc
{{Nasal doc
|syntax = registerFlightPlanDelegate(init_func);
|syntax = registerFlightPlanDelegate(init_func);
Line 2,137: Line 2,156:
|param1text = Initialization function which will be called during FlightGear's startup.
|param1text = Initialization function which will be called during FlightGear's startup.
}}
}}
=== removecommand() ===
===removecommand()===
{{Nasal doc
{{Nasal doc
|syntax = removecommand(cmd);
|syntax = removecommand(cmd);
Line 2,152: Line 2,171:
}}
}}


=== removelistener() ===
===removelistener()===
{{Nasal doc
{{Nasal doc
|syntax = removelistener(id);
|syntax = removelistener(id);
Line 2,168: Line 2,187:
}}
}}


=== resolvepath() ===
===resolvepath()===
{{Nasal doc
{{Nasal doc
|syntax = resolvepath(path);
|syntax = resolvepath(path);
Line 2,182: Line 2,201:
} else {
} else {
     gui.popupTip("some-file not found", 2);
     gui.popupTip("some-file not found", 2);
}
<nowiki>}</nowiki>
}}
}}


=== setlistener() ===
===setlistener()===
{{Nasal doc
{{Nasal doc
|syntax = setlistener(node, code[, init[, type]]);
|syntax = <nowiki>setlistener(node, code[, init[, type]]);</nowiki>
|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.''
Line 2,217: Line 2,236:
}}
}}


=== setprop() ===
===setprop()===
{{Nasal doc
{{Nasal doc
|syntax = setprop(path[, path[, ...]], value);
|syntax = <nowiki>setprop(path[, path[, ...]], value);</nowiki>
|source = {{flightgear file|src/Scripting/NasalSys.cxx|l=385|t=Source}}
|source = {{flightgear file|src/Scripting/NasalSys.cxx|l=385|t=Source}}
|text = Sets the value of a property in the [[Property Tree]]. If the property does not exist, it will be created. Returns 1 (true) on success or 0 (false) if there was an error.
|text = Sets the value of a property in the [[Property Tree]]. If the property does not exist, it will be created. Returns 1 (true) on success or 0 (false) if there was an error.
Line 2,240: Line 2,259:
|example3 = for(var i = 0; i < 3; i += 1){
|example3 = for(var i = 0; i < 3; i += 1){
     setprop("/sim/demo[" ~ i ~ "]", "Demo #" ~ i));
     setprop("/sim/demo[" ~ i ~ "]", "Demo #" ~ i));
}
<nowiki>}</nowiki>
}}
}}
==== See also ====
====See also====
{{note| If you have to read/write the same property multiple times (e.g. in an update loop), it is more efficient to use a node object:  
{{note| If you have to read/write the same property multiple times (e.g. in an update loop), it is more efficient to use a node object:  
To get a Node rather than its value, use <code>props.globals.getNode()</code> - see [[Nasal_library/props]]. }}
To get a Node rather than its value, use <code>props.globals.getNode()</code> - see [[Nasal_library/props]]. }}


=== settimer() ===
===settimer() ===
{{Nasal doc
{{Nasal doc
|syntax = settimer(function, delta[, realtime]);
|syntax = settimer(function, delta[, realtime]);
Line 2,272: Line 2,291:
}}
}}


=== srand() ===
===srand() ===
{{Nasal doc
{{Nasal doc
|syntax = srand();
|syntax = srand();
Line 2,278: Line 2,297:
|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()===
{{Nasal doc
{{Nasal doc
|syntax = systime();
|syntax = systime();
Line 2,296: Line 2,315:
}}
}}


=== thisfunc() ===
===thisfunc()===
{{Nasal doc
{{Nasal doc
|syntax = thisfunc();
|syntax = thisfunc();
Line 2,330: Line 2,349:
}}
}}


=== tileIndex() ===
===tileIndex() ===
{{Nasal doc
{{Nasal doc
|syntax = tileIndex();
|syntax = tileIndex();
Line 2,338: Line 2,357:
}}
}}


=== tilePath() ===
=== tilePath()===
{{Nasal doc
{{Nasal doc
|syntax = tilePath();
|syntax = tilePath();
Line 2,346: Line 2,365:
}}
}}


=== values() ===
=== values()===
{{Nasal doc
{{Nasal doc
|syntax = values(hash);
|syntax = values(hash);
Line 2,371: Line 2,390:
foreach(var key; keys(hash)){
foreach(var key; keys(hash)){
     print(hash[key]);
     print(hash[key]);
}
<nowiki>}</nowiki>
}}
}}


== Extension functions new in FG 2020.1 ==
==Extension functions new in FG 2020.1 ==
The following functions have been added to the nasal core library and will be released with FlightGear version 2020.1.  
The following functions have been added to the nasal core library and will be released with FlightGear version 2020.1.  
Before the release they are available in the development branch "next".
Before the release they are available in the development branch "next".


=== isfunc() ===
===isfunc()===
Returns 1 if type or argument is a function, otherwise 0.
Returns 1 if type or argument is a function, otherwise 0.


=== isghost() ===
===isghost()===
Returns 1 if type or argument is a ghost, otherwise 0.
Returns 1 if type or argument is a ghost, otherwise 0.


=== ishash() ===
=== ishash()===
Returns 1 if type or argument is a hash, otherwise 0.
Returns 1 if type or argument is a hash, otherwise 0.


=== isint() ===
===isint()===
{{Nasal doc
{{Nasal doc
|syntax = isint(x);
|syntax = isint(x);
Line 2,394: Line 2,413:
}}
}}


=== isnum() ===
===isnum()===
{{Nasal doc
{{Nasal doc
|syntax = isnum(x);
|syntax = isnum(x);
Line 2,401: Line 2,420:
}}
}}


=== isscalar() ===
===isscalar()===
{{Nasal doc
{{Nasal doc
|syntax = isscalar(x);
|syntax = isscalar(x);
Line 2,414: Line 2,433:
}}
}}


=== isstr() ===
===isstr()===
Returns 1 if type or argument is a string, otherwise 0.  
Returns 1 if type or argument is a string, otherwise 0.  


=== isvec() ===
===isvec()===
Returns 1 if type or argument is a vector, otherwise 0.
Returns 1 if type or argument is a vector, otherwise 0.


=== vecindex() ===
===vecindex()===
{{Nasal doc
{{Nasal doc
|syntax = vecindex(vector, value);
|syntax = vecindex(vector, value);
Line 2,434: Line 2,453:
if (vecindex(myvector, "apple")) {
if (vecindex(myvector, "apple")) {
     print("found apple");
     print("found apple");
}
<nowiki>}</nowiki>


}}
}}


{{note| There is a similar function function contains(hash, key) to check if a hash contains a key (not value!).
==Variables==
It is to be discussed, if contains can/should be extended to support vectors in the above mentioned way. }}
 
== Variables ==
Various global constants (technically variables) are provided for use in converting between different units. They are all found in {{fgdata file|Nasal/globals.nas|text=$FG_ROOT/Nasal/globals.nas}}.
Various global constants (technically variables) are provided for use in converting between different units. They are all found in {{fgdata file|Nasal/globals.nas|text=$FG_ROOT/Nasal/globals.nas}}.


=== D2R ===
===D2R===
{{Nasal doc
{{Nasal doc
|syntax = var radians = degrees * D2R;
|syntax = var radians = degrees * D2R;
|text = Converts an angle from degrees to radians when multiplied by the angle in degrees. Equal to <code>π / 180</code>.
|text = Converts an angle from degrees to radians when multiplied by the angle in degrees. Equal to <code>π / 180</code>.
}}
}}
=== FPS2KT ===
===FPS2KT===
{{Nasal doc
{{Nasal doc
|syntax = var knots = feet_per_second * FPS2KT;
|syntax = var knots = feet_per_second * FPS2KT;
|text = Converts a velocity from feet per second to knots when multiplied by the velocity in feet per second. Approximately equal to 0.5925.
|text = Converts a velocity from feet per second to knots when multiplied by the velocity in feet per second. Approximately equal to 0.5925.
}}
}}
=== FT2M ===
===FT2M===
{{Nasal doc
{{Nasal doc
|syntax = var metres = feet * FT2M;
|syntax = var metres = feet * FT2M;
|text = Converts a length from feet to metres when multiplied by the length in feet. Equal to 0.3048.
|text = Converts a length from feet to metres when multiplied by the length in feet. Equal to 0.3048.
}}
}}
=== GAL2L ===
===GAL2L ===
{{Nasal doc
{{Nasal doc
|syntax = var litres = gallons * GAL2L;
|syntax = var litres = gallons * GAL2L;
|text = Converts a volume from US liquid gallons to litres when multiplied by the volume in gallons. Approximately equal to 3.7854.
|text = Converts a volume from US liquid gallons to litres when multiplied by the volume in gallons. Approximately equal to 3.7854.
}}
}}
=== IN2M ===
===IN2M===
{{Nasal doc
{{Nasal doc
|syntax = var metres = inches * IN2M;
|syntax = var metres = inches * IN2M;
|text = Converts a length from inches to metres when multiplied by the length in inches. Equal to 0.0254.
|text = Converts a length from inches to metres when multiplied by the length in inches. Equal to 0.0254.
}}
}}
=== KG2LB ===
===KG2LB===
{{Nasal doc
{{Nasal doc
|syntax = var pounds = kilograms * KG2LB;
|syntax = var pounds = kilograms * KG2LB;
|text = Converts a mass from kilograms to pounds when multiplied by the mass in kilograms. Approximately equal to 2.2046.
|text = Converts a mass from kilograms to pounds when multiplied by the mass in kilograms. Approximately equal to 2.2046.
}}
}}
=== KT2FPS ===
===KT2FPS===
{{Nasal doc
{{Nasal doc
|syntax = var feet_per_second = knots * KT2FPS;
|syntax = var feet_per_second = knots * KT2FPS;
|text = Converts a velocity from knots to feet per second when multiplied by the velocity in knots. Approximately equal to 1.6878.
|text = Converts a velocity from knots to feet per second when multiplied by the velocity in knots. Approximately equal to 1.6878.
}}
}}
=== KT2MPS ===
===KT2MPS===
{{Nasal doc
{{Nasal doc
|syntax = var metres_per_second = knots * KT2MPS;
|syntax = var metres_per_second = knots * KT2MPS;
|text = Converts a velocity from knots to metres per second when multiplied by the velocity in knots. Approximately equal to 0.5144.
|text = Converts a velocity from knots to metres per second when multiplied by the velocity in knots. Approximately equal to 0.5144.
}}
}}
=== L2GAL ===
===L2GAL===
{{Nasal doc
{{Nasal doc
|syntax = var gallons = litres * L2GAL;
|syntax = var gallons = litres * L2GAL;
|text = Converts a volume from litres to US liquid gallons when multiplied by the volume in litres. Approximately equal to 0.2642.
|text = Converts a volume from litres to US liquid gallons when multiplied by the volume in litres. Approximately equal to 0.2642.
}}
}}
=== LB2KG ===
===LB2KG===
{{Nasal doc
{{Nasal doc
|syntax = var kilograms = pounds * LB2KG;
|syntax = var kilograms = pounds * LB2KG;
|text = Converts a mass from pounds to kilograms when multiplied by the mass in pounds. Approximately equal to 0.4536.
|text = Converts a mass from pounds to kilograms when multiplied by the mass in pounds. Approximately equal to 0.4536.
}}
}}
=== M2FT ===
===M2FT ===
{{Nasal doc
{{Nasal doc
|syntax = var feet = metres * M2FT;
|syntax = var feet = metres * M2FT;
|text = Converts a length from metres to feet when multiplied by the length in metres. Approximately equal to 3.2808.
|text = Converts a length from metres to feet when multiplied by the length in metres. Approximately equal to 3.2808.
}}
}}
=== M2IN ===
===M2IN===
{{Nasal doc
{{Nasal doc
|syntax = var inches = metres * M2IN;
|syntax = var inches = metres * M2IN;
|text = Converts a length from metres to inches when multiplied by the length in metres. Approximately equal to 39.3701.
|text = Converts a length from metres to inches when multiplied by the length in metres. Approximately equal to 39.3701.
}}
}}
=== M2NM ===
===M2NM ===
{{Nasal doc
{{Nasal doc
|syntax = var nautical_miles = metres * M2NM;
|syntax = var nautical_miles = metres * M2NM;
|text = Converts a length from metres to nautical miles when multiplied by the length in metres. Approximately equal to 0.00054.
|text = Converts a length from metres to nautical miles when multiplied by the length in metres. Approximately equal to 0.00054.
}}
}}
=== MPS2KT ===
===MPS2KT ===
{{Nasal doc
{{Nasal doc
|syntax = var knots = metres_per_second * MPS2KT;
|syntax = var knots = metres_per_second * MPS2KT;
|text = Converts a velocity from metres per second to knots when multiplied by the velocity in metres per second. Approximately equal to 1.9438.
|text = Converts a velocity from metres per second to knots when multiplied by the velocity in metres per second. Approximately equal to 1.9438.
}}
}}
=== NM2M ===
===NM2M ===
{{Nasal doc
{{Nasal doc
|syntax = var metres = nautical_miles * NM2M;
|syntax = var metres = nautical_miles * NM2M;
|text = Converts a length from nautical miles to metres when multiplied by the length in nautical miles. Equal to 1,852.
|text = Converts a length from nautical miles to metres when multiplied by the length in nautical miles. Equal to 1,852.
}}
}}
=== R2D ===
===R2D===
{{Nasal doc
{{Nasal doc
|syntax = var degrees = radians * R2D;
|syntax = var degrees = radians * R2D;
97

edits

Navigation menu