Synchronization

Instructions related to synchronization allow our characters to organize things happen in the order we want.

If you get 0 points...

The easiest way to synchronize the behavior of your characters is using a 'wait' block, which makes the character waits the number of seconds you write as a parameter block. An example of how this block can be used to synchronize two characters:

                    when green flag clicked
                    say [Hello, how are you?] for (2) secs
                    wait (2) secs
                    say [Well, do you want to play hide-and seek?] for (3) secs
                    wait (1) secs
                    say [Let's go] for (2) secs
                    when green flag clicked
                    wait (2) secs
                    say [I'm fine, thanks!] for (2) secs
                    wait (3) secs
                    say [Yes] for (1) secs
                


In this case it uses blocks 'wait' to synchronize these two characters to maintain a conversation, so that while one speaks, the other character waits, and vice versa. Note that the number of seconds that each character expected, are equal to the number of seconds that the other character speaks, so they never talk at the same time.

If you get 1 point...

Synchronization using blocks 'wait' is very simple when programs are small and have few characters, but when are larger, or when we have several characters, or when conditions to generate a reaction can not be measured previously, it is more efficient to use other modes of synchronization and messages. Here's an example:

                    when green flag clicked
                    say [Hello, how are you?] for (2) secs
                    forever
                    if < touching [mouse pointer v]?>then
                    broadcast [catched! v]
                    end
                

                    when I receive [catched! v]
                    say [You have catched my friend] for (2) secs
                    say [Now, you have to catch me!] for (2) secs
                


How do these syncblocks work? When a situation occurs in a character we want to cause a reaction in another character, we can use messaging. In the example, when the mouse touches the cat, the message 'Caught!', which will be sent to all the characters of the project is sent. Thus, when the butterfly receives the message 'Caught!', the instructions on the block 'when receive 'caught!''. Therefore, when the user touches the cat whit the mouse, butterfly says, 'You caught my companion. Now you have to catch me!'.

If you get 2 points...

In addition to sending messages, you can synchronize characters to make things happen in the order we want, using other blocks, such as 'wait until' or 'when the background changes to ...'.

                    wait until <(lives) =[0]>
                    say [Game over] for (2) secs
                    stop [all v]
                
                    when backdrop switches to [Spring v]
                    show
                    say [Spring has come!] for (2) secs
                

In the first example, when program execution reaches that point will stop until the condition is met in this case, until life is equal to 0. When lives equal to 0, then continue execution of the remaining blocks, this case, 'said Game Over' and 'stop all', which ends the execution of the project. In the second example, the moment when the scene changes to the backdrop with the name 'Spring', the character will execute the blocks placed after the instruction 'when the backdrop changes to Spring', ie to be displayed and will say 'spring has come!'.