We'll look at 1. We don't have time for 2 & 3.
(We do have the docs and manuals if you're interested.)
Tree of boxes defines behaviour.
__init__, onLoad, onUnload,
onInput_onStart & onInput_onStop
.
GeneratedClass
whose
__init__
must be the first thing called
in the child class's own __init__
method.Right-click to create a new box.
(Double click the box to see the generated code.)
(Edit code in here. No need to "save".)
(Press the green "Play" button to run.)
(Yay! It works!)
At behaviour start:
__init__
method for
ALL boxes is run.onLoad
method called.(Right click on an input and select Add
Input
.)
(Same again, but right click on an existing output.)
(Double click on the box to edit the code for the
onInput_sum
method.)
def onInput_sum(self, p):
"""
The input p should be the name of a number between 0-9 (defined
in self.numbers defined in __init__) or the word "equals".
Numbers get added to the self.operands list. When the word
"equals" in encountered the operands are summed then cleared
and the result is output.
Anything else is handled by very basic error handling.
"""
self.logger.info(p)
try:
if p.strip() in self.numbers:
self.operands.append(int(p))
elif p == 'equals':
self.logger.info(self.operands)
result = sum(self.operands)
self.operands = []
self.outputResult('The answer is {}'.format(result))
except Exception as ex:
# Yeah, I know... :-(
error = ex.__class__.__name__
msg = 'Cannot compute. I encountered a {}'.format(error)
self.outputResult(msg)
Define a behaviour tree:
(Click on the spanner in the bottom rhs of the Speech Recognition box.)
Add the word "equals" and the digits 0-9 as a delineated semi-colon list. Set the threshold to just over 51% (but don't expect great results - experiment!).
(Live demo)