Skip to content

Hello You with mood

cl4cnam edited this page Jan 4, 2024 · 8 revisions

In the previous tutorial, the robot had an emergency. This time, the robot has mood:

  • if you respond within 5 seconds, it's suddenly in a good mood and do bird sounds,
  • if you haven't respond within 5 seconds, it's suddenly in a bad mood and do duck sounds.

The naive implementation is simply adapting the previous tutorial:

displayNewMessage('What is your name?')
parallel exitAfter 1 finished ||
	var theName := awaitHumanText()
	playSoundFile('birds.mp3')
||
	waitSeconds(5)
	playSoundFile('ducks.mp3')

Try it! Do you spot the problem? Otherwise, respond before 5 seconds and listen! You hear bird sounds and also duck sounds.

Here is the timeline:

timeline2D

At a certain point, the bird sounds are discontinued because the second branch is finished.

Remedy

What we need is eliminating the other branch when var theName := awaitHumanText() is finished! So, we need to cut the branch in half!

Fortunately, a syntax for that exists, try:

displayNewMessage('What is your name?')
parallel(select 1) ||
||===
	var theName := awaitHumanText()
...---
	playSoundFile('birds.mp3')
||===
	waitSeconds(5)
...---
	playSoundFile('ducks.mp3')

Explanation

Each parallel branch is introduced by ||===. The point at which the branch is cut in half is materialized by ...---

Tip

You can extend the ||=== syntax and ...--- by typing more = sign / - sign. This enables you to highlight the first part of the branch.

The general syntax is:

parallel(select M) ||
||===
	branch1_part1
...---
	branch1_part2
||===
	[...]
||===
	branchN_part1
...---
	branchN_part2

which means:

  • When a branch reach the end of its first part, this branch is considered selected
  • When M branches are selected, all the other branches are discontinued
  • The parallel construct is finished when all the selected branches are finished.

Tip

  • You'll often use:
parallel(select 1) ||
||===
	waitingInstruction1
...---
	branch1_rest
||===
	[...]
||===
	waitingInstructionN
...---
	branchN_rest