In the last video, you made both characters talk, but they talk at the same time.
One solution would be to put wait blocks between the “speak” blocks, but placing all those “wait” blocks would get tedious.
Instead, control who is talking by creating a variable that keeps track of whose turn it is to talk.
Make a variable and name it something that makes sense, like “person talking.”
Isabel should wait to talk until “person talking” equals “Isabel.”
Place a “wait until” block under the "define speak" block.
Add an “equals” block inside the “wait until” block, and place the “person talking” variable on one side of the “equals” block.
Type “Isabel” on the other side.
If you renamed this sprite, type in the name you selected.
Next, set the variable to Isabel when Malik is done talking.
Click on the “Malik talk” sprite.
Place the “set variable” block at the end of the “speak” function definition.
Type “Isabel” in the box.
Click the flag again.
Isabel waits to speak until Malik is done talking.
But Malik doesn’t wait for Isabel.
Testing the code uncovered a “bug,” or mistake that needs to be fixed.
Make sure to test your code often so you catch bugs early!
To make the characters wait their turn to talk, add the same code to Malik that you added to Isabel.
Place a “wait until” block under the “speak” define block definition.
Add an “equals” block inside the “wait until” block.
Place the “person talking” block on one side of the “equals” block, and type “Malik” on the other side.
Finally, change the “person talking” variable when Isabel is finished talking.
Click on the “Isabel talk” sprite, and set “person talking” to Malik.
Click the flag.
Both characters wait their turn to talk!
But, Isabel talks first.
That’s because “person talking” is still set to Isabel.
You found another bug in the code by testing often!
Set “person talking” at the beginning of the program to the character that should start the conversation.
Place the “set variable” block under the “when flag clicked” block.
In the box, type the character who will start the conversation.
In this example, it’s Malik.
Click on the “Malik talk” sprite, and do the same thing.
Now, both sprites know to start with Malik talking.
Run the code again.
The conversation happens in the right order!
Because both sprites now wait their turn to talk using variables, the “wait” blocks are no longer needed.
Remove the “wait” block from both sprites.
Test the code once more.
Functions make fixing bugs in code easier.
Once you fix one part of a function, it is fixed for each time you use it.
If you weren’t using functions, you would need to change who’s speaking between each “say” block.
Instead, you need to fix this error only once for each sprite!
Now that you’ve built the functions to easily create a dialogue, it’s time to write it!
In this example story, Malik and Isabel work out their differences and decide to have pizza.
Functions make it easier to write longer, more interesting conversations because you can add more lines of dialogue, switch between speakers, and add emotions without writing new stacks of code.
Add more “speak” blocks to the project to create your own dialogue.
Create any kind of conversation -- funny, heartfelt, silly, tragic, friendly -- with the help of computer science!
Now, it’s your turn.
Change whose turn it is to speak using “set person talking” and “wait until” blocks.
Set "person talking" to the first character in both "Malik talk" and "Isabel talk."
Delete the “wait” blocks.
Create a conversation between the two characters.
- Change whose turn it is to speak using "set person talking" and "wait until" blocks.
- Set "person talking" to the first character in both "Malik talk" and "Isabel talk."
- Delete the "wait" blocks.
- Create a conversation between the two characters.