I've in the last days modified Nasal's listener code. The important
news: the default behavior remains the same, and you don't have to
adapt any Nasal code.
There's now a fourth, optional argument (default=1) that, when set
to zero, only executes the listener function when the node value
has actually changed. Writing the same value repeatedly will not
trigger. Of course, one could also check for that in the function
itself, but this wouldn't avoid the overhead of the Nasal context
generation etc.
setlistener(<prop>, <func> [, <init=0 [, <persistent=1>]]
<prop> ... the property as props.Node or as absolute path string:
var p = props.globals.getNode("/gear/launchbar/state");
setlistener(p, func {});
setlistener("/gear/launchbar/state", func {});
<func> ... a function object/variable. Either the code directly
as anonymous function (func{}) or the function variable.
(Without the parentheses, of course, or it would be a
function *call*).
var f = func { print("test") }
setlistener(p, f);
setlistener(p, func { print("another test") });
<init> ... This exists since a while, but it's not used much. If
non-zero, it causes the callback function to be executed
initially as well. That's useful for cases like this:
setlistener("/sim/current-view/view-number", func {
setprop("/sim/hud/visibility", !cmdarg().getValue());
}, 1);
That is: make sure the HUD is turned off in all views but
cockpit view, and do that also right after startup, not
only when "/sim/hud/visibility" is written to.
** NEW **
<persistent> ... This is by default 1, which means that the listener
function is called whenever the property was just written
to (= traditional behavior). Writing the same value repeatedly
will trigger as well. If the argument is set to 0, then the
function will only be called when a new value is written.
This is important for cases where a property is written to
once per frame, no matter if the value changed or not.
YASim, for example, does that for /gear/gear/wow or
/gear/launchbar/state.
Should be easy to remember: For both optional flags 0 means less
calls, and 1 means more calls. The first is for startup behavior,
and the second for runtime behavior. I would have liked to make
the fourth argument default to 0, but this would probably break
some code, which I wanted to avoid before the release. I'm not
sure if it should be changed later.
m.
-------------------------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc.
Still grepping through log files to find problems? Stop.
Now Search log events and configuration files using AJAX and a browser.
Download your FREE copy of Splunk now >> http://get.splunk.com/
_______________________________________________
Flightgear-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/flightgear-devel