Conversations

Материал из Поле цифровой дидактики

In Scratch, two or more sprites can talk to one another, having a dialogue conversation. There are several scripting methods associated with this, each unique to the other. They mainly utilize the say [] block or its timed counterpart for conversing.

Methods

For this entire article, assume the two sprites conversing are "Scratch Cat" and "Gobo".

Broadcast Method

The broadcast method is not always the most efficient way to make sprites communicate many messages, but short conversations can be easily made using broadcasts and no variables or lists. The following script would go into "Scratch Cat".

when gf clicked
say [Hi!] for (2) secs
broadcast [Gobo 1 v]

Then, inside the Gobo sprite:

when I receive [Gobo 1 v]
say [How are you?] for (2) secs
broadcast [Scratch Cat 2 v]

The two sprites continuously broadcast back and forth to each other to say a message. However, when the conversation becomes long, multiple broadcasts are needed and the scripts can become unorganized.

Broadcast and Wait method

This method is slightly more efficient, because only one script is needed in the first sprite to talk.

when gf clicked //in the first sprite
say [Hello!] for (2) secs
broadcast [Gobo 1 v] and wait
say [How are you today?] for (2.5) secs
broadcast [Gobo 2 v] and wait //and so on

Then, inside of Gobo:

when I receive [Gobo 1 v]
say [Hi there!] for (2) secs

when I receive [Gobo 2 v]
say [Very fine indeed! And you?] for (3) secs

Variable Method

The variable method uses a variable to trigger the other sprite to talk. After a sprite finishes speaking, it changes a variable by "1", in which the other sprite recognizes and then begins speaking. The following script would go into "Scratch Cat".

when gf clicked
set [talker v] to [1] //sets it so the conversation starts from the beginning and functions properly
say [Hi Gobo] for (2) secs
change [talker v] by (1) //signals Gobo to talk
wait until <(talker) = [3]> //wait until Gobo is done responding
say [I am good, how about you?] for (4) secs
change [talker v] by (1)

And for the "Gobo" sprite:

when gf clicked
wait until <(talker) = [2]> //wait until Scratch Cat greets
say [Hi Scratch Cat. How are you?] for (3) secs
change [talker v] by (1) //signals Scratch Cat to talk
wait until <(talker) = [4]>
say [I am great also!] for (3) secs

The script can keep cycling continuously. The script can become very long from a long conversation, but it does not use multiple of any broadcasts. The variable method is often best for unordered conversations, in which there are multiple sprites not speaking to one another in a patterned fashion.

List Method

The list method uses the same amount of scripting no matter how long the conversation is. It is a great method to reduce large scripts and a project's file size. The list method uses two scripts, one of "Scratch Cat's" messages and one of "Gobo's". The first sprite says a message, triggers the second sprite to speak, and then the cycle continues, with a variable being changed after both have fully spoken, to signal the next message from the lists. The lists are used to simply store the text which each sprite will speak, and is ordered within. The following script goes into "Scratch Cat"

when gf clicked
set [item# v] to [1] //so the first message is the first item in the list of messages
set [talker v] to [Scratch Cat] //to know that "Scratch Cat" speaks first
forever
say (item (item#) of [Scratch Cat messages v]) for (3) secs //says the current item from the list of messages
set [talker v] to [Gobo] //signal "Gobo" to speak
wait until <(talker) = [Scratch Cat]> //wait until it is its turn to speak

And for "Gobo":

when gf clicked
forever
wait until <(talker) = [Gobo]> //wait until it is its turn to talk
say (item (item#) of [Gobo messages v]) for (3) secs //say the item in the list of messages
change [item# v] by (1) //so the next message in the lists is said by each sprite instead of the same one
set [talker v] to [Scratch Cat] //signals "Scratch Cat" to speak