Nasal library: Difference between revisions

Jump to navigation Jump to search
(→‎settimer(): Finish doc)
Line 2,032: Line 2,032:


=== setprop() ===
=== setprop() ===
Sets a property value for a given node path string. Returns 1 on success or 0 if the property could not be set (i.e. was read-only).
{{Nasal doc
 
|syntax = setprop(path[, path[, ...]], value);
|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.
{{note|If you want to remove a property, you will have to use one of the <code>props</code> helpers:
<syntaxhighlight lang="nasal">
<syntaxhighlight lang="nasal">
setprop(<path> [, <path>, [...]], <value>);
props.globals.getNode("foo/bar").remove(); # take out the complete node
</syntaxhighlight>
props.globals.getNode("foo").removeChild("bar"); # take out a certain child node
 
All arguments but the last are concatenated to a path string, like getprop() above. The last value is written to the respective node. If the node isn't writable, then an error message is printed to the console.
 
Examples:
<syntaxhighlight lang="nasal">
setprop("/sim/current-view/view-number", 2);
setprop("/controls", "engines/engine[0]", "reverser", 1);
</syntaxhighlight>
 
'''Erasing a property from the property tree''': a property that has been created, for example through <tt>setprop()</tt> has to be erased using the props namespace helper, like this:
<syntaxhighlight lang="nasal">
props.globals.getNode("foo/bar").remove(); # take out the complete node
props.globals.getNode("foo").removeChild("bar"); # take out a certain child node
</syntaxhighlight>
</syntaxhighlight>
}}
|param1 = path
|param1text = There needs to be at least one '''path''' argument, but there is no limit to the maximum amount that can be given. They will be concatenated together to form a property tree path. The arguments must be strings, but in FlightGear v3.2 onwards, there also is support (added by {{flightgear commit|34ed79}}) for numeric arguments as indices. See example 2 below.
|param2 = value
|param2text = Value to write to the given property. Must be either a string or a number.
|example1 = setprop("/sim/demo", "This is a demo");
|example2text = Note that the example below will only work in FlightGear 3.2 and above.
|example2 = for(var i = 0; i < 3; i += 1){
    setprop("/sim/demo", i, "Demo #" ~ i));
}
|example3text = Same as above, but is supported by all versions of FlightGear.
|example3 = for(var i = 0; i < 3; i += 1){
    setprop("/sim/demo[" ~ i ~ "]", "Demo #" ~ i));
}
}}


=== settimer() ===
=== settimer() ===

Navigation menu