Using listeners and signals with Nasal: Difference between revisions

Jump to navigation Jump to search
no edit summary
(→‎setlistener(): More reliable code.)
No edit summary
 
Line 71: Line 71:


<syntaxhighlight lang="nasal">
<syntaxhighlight lang="nasal">
var listener_id = setlistener(<property>, <function> [, <startup=0> [, <runtime=1>]]);
var listener_id = setlistener(<property>, <function> [, <startup = 0> [, <runtime = 1>]]);
</syntaxhighlight>
</syntaxhighlight>


Line 91: Line 91:
<syntaxhighlight lang="nasal">
<syntaxhighlight lang="nasal">
setlistener("/gear/launchbar/state", func (node) {
setlistener("/gear/launchbar/state", func (node) {
     if (node.getValue() == "Engaged")
     if (node.getValue() == "Engaged") {
         setprop("/sim/messages/copilot", "Engaged!");
         setprop("/sim/messages/copilot", "Engaged!");
    }
}, 1, 0);
}, 1, 0);
</syntaxhighlight>
</syntaxhighlight>
Line 130: Line 131:


<syntaxhighlight lang="nasal">
<syntaxhighlight lang="nasal">
var L = setlistener("/some/property", func {
var id = setlistener("/some/property", func {
     print("I can only be triggered once.");
     print("I can only be triggered once.");
     removelistener(L);
     removelistener(id);
});
});
</syntaxhighlight>
</syntaxhighlight>
Line 159: Line 160:


<syntaxhighlight lang="nasal">
<syntaxhighlight lang="nasal">
var say_bye = func { print("bye") }
var say_bye = func { print("bye") };
setlistener("/sim/signals/exit", say_bye);
setlistener("/sim/signals/exit", say_bye);
</syntaxhighlight>
</syntaxhighlight>
Line 173: Line 174:
     print("Monitored course set to ", course.getValue());
     print("Monitored course set to ", course.getValue());
}
}
var i = setlistener("instrumentation/gps/wp/leg-course-deviation-deg", monitor_course);
var id = setlistener("instrumentation/gps/wp/leg-course-deviation-deg", monitor_course);


# here the listener is active
# here the listener is active


removelistener(i);                    # remove that listener again
removelistener(id);                    # remove that listener again
</syntaxhighlight>
</syntaxhighlight>


Line 187: Line 188:
     print("Another way to get the same setting ", course.getValue());
     print("Another way to get the same setting ", course.getValue());
}
}
var i = setlistener("instrumentation/gps/wp", monitor_course, 0, 2);
var id = setlistener("instrumentation/gps/wp", monitor_course, 0, 2);
</syntaxhighlight>
</syntaxhighlight>


Line 198: Line 199:
Beware, however, that the contents of a function defined within the <tt>setlistener</tt> call are not evaluated until the call is actually made. If, for instance, local variables change before the setlistener call happens, the call will reflect the current value of those variables ''at the time the callback function is called'', not the value ''at the time the listener was set''.  
Beware, however, that the contents of a function defined within the <tt>setlistener</tt> call are not evaluated until the call is actually made. If, for instance, local variables change before the setlistener call happens, the call will reflect the current value of those variables ''at the time the callback function is called'', not the value ''at the time the listener was set''.  


For example, with this loop, the function will always return the value 10--even if mynode[1], mynode[2], mynode[3] or any of the others is the one that changed. It is because the contents of the setlistener are evaluated after the loop has completed running and at that point, i=10:
For example, with this loop, the function will always return the value 10--even if mynode[1], mynode[2], mynode[3] or any of the others is the one that changed. It is because the contents of the setlistener are evaluated after the loop has completed running and at that point, i = 10:


<syntaxhighlight lang="nasal">
<syntaxhighlight lang="nasal">
var output = func(number) {
var output = func(number) {
     print("mynode", number, " has changed!"); #This won't work!
     print("mynode", number, " has changed!"); # This won't work!
}
};
for(i=1; i <= 10; i = i+1) {
 
   var i = setlistener("mynode["~i~"]", func{ output (i); });
for (i = 1; i <= 10; i += 1) {
   var i = setlistener("mynode[" ~ i ~ "]", func { output (i); });
}
}
</syntaxhighlight>
</syntaxhighlight>
Line 212: Line 214:


<syntaxhighlight lang="nasal">
<syntaxhighlight lang="nasal">
for(i=1; i <= 10; i = i+1) {
for (i = 1; i <= 10; i += 1) {
     var i = setlistener("mynode["~i~"]", func (changedNode) { print (changedNode.getPath() ~ " : " ~ changedNode.getValue()); });
     var i = setlistener("mynode[" ~ i ~ "]", func (changedNode) { print (changedNode.getPath() ~ " : " ~ changedNode.getValue()); });
}
}
</syntaxhighlight>
</syntaxhighlight>
Line 280: Line 282:
<syntaxhighlight lang="nasal">
<syntaxhighlight lang="nasal">
setlistener("/sim/signals/fdm-update", func(n) {
setlistener("/sim/signals/fdm-update", func(n) {
      var dt = n.getValue();
    var dt = n.getValue();
      # ... and whatever needs to be done at fdm rate
    # ... and whatever needs to be done at fdm rate
  });
});
</syntaxhighlight>
</syntaxhighlight>


1,330

edits

Navigation menu