m (Added link.) |
(oops) |
||
(22 intermediate revisions by 10 users not shown) | |||
Line 1: | Line 1: | ||
− | In Scratch, two or more [[ | + | In [[Scratch]], two or more [[sprite]]s can talk to one another, having a dialogue '''conversation'''. There are several [[script]]ing methods associated with this, each unique to the other. They mainly utilize the [[Say () (block)|<sb>say []</sb>]] block or its [[Say () for () Secs (block)|timed counterpart]] for conversing. |
+ | |||
==Methods== | ==Methods== | ||
− | For this entire article, assume the two sprites conversing are "Scratch Cat" and "Gobo". | + | For this entire article, assume the two sprites conversing are "[[Scratch Cat]]" and "[[Gobo]]". |
===Broadcast Method=== | ===Broadcast Method=== | ||
− | The [[ | + | 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 [[variable]]s or [[list]]s. The following script would go into "Scratch Cat". |
<scratchblocks> | <scratchblocks> | ||
when gf clicked | when gf clicked | ||
− | say [ | + | say [Hi!] for (2) secs |
broadcast [Gobo 1 v] | broadcast [Gobo 1 v] | ||
</scratchblocks> | </scratchblocks> | ||
Line 13: | Line 14: | ||
<scratchblocks> | <scratchblocks> | ||
when I receive [Gobo 1 v] | when I receive [Gobo 1 v] | ||
− | say [ | + | say [How are you?] for (2) secs |
broadcast [Scratch Cat 2 v] | broadcast [Scratch Cat 2 v] | ||
</scratchblocks> | </scratchblocks> | ||
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. | 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==== | ====Broadcast and Wait method==== | ||
− | This method is slightly more efficient, because | + | This method is slightly more efficient, because only one script is needed in the first sprite to talk. |
<scratchblocks> | <scratchblocks> | ||
when gf clicked //in the first sprite | when gf clicked //in the first sprite | ||
Line 29: | Line 31: | ||
<scratchblocks> | <scratchblocks> | ||
when I receive [Gobo 1 v] | when I receive [Gobo 1 v] | ||
− | say [Hi there!] | + | say [Hi there!] for (2) secs |
when I receive [Gobo 2 v] | when I receive [Gobo 2 v] | ||
− | say [Very fine indeed! And you?] | + | say [Very fine indeed! And you?] for (3) secs |
</scratchblocks> | </scratchblocks> | ||
Line 55: | Line 57: | ||
say [I am great also!] for (3) secs | say [I am great also!] for (3) secs | ||
</scratchblocks> | </scratchblocks> | ||
− | The script can keep cycling continuously. The script can become very long from a long conversation, but it | + | 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=== | ===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" | + | 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" |
<scratchblocks> | <scratchblocks> | ||
when gf clicked | when gf clicked | ||
Line 66: | Line 68: | ||
say (item (item#) of [Scratch Cat messages v]) for (3) secs //says the current item from the list of messages | 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 | set [talker v] to [Gobo] //signal "Gobo" to speak | ||
− | wait until <(talker) = [Scratch Cat]> //wait until it | + | wait until <(talker) = [Scratch Cat]> //wait until it is its turn to speak |
</scratchblocks> | </scratchblocks> | ||
And for "Gobo": | And for "Gobo": | ||
Line 72: | Line 74: | ||
when gf clicked | when gf clicked | ||
forever | forever | ||
− | wait until <(talker) = [Gobo]> //wait until it | + | 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 | 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 | change [item# v] by (1) //so the next message in the lists is said by each sprite instead of the same one | ||
Line 78: | Line 80: | ||
</scratchblocks> | </scratchblocks> | ||
− | [[Category: Scripting Tutorials]] | + | [[Category:Scripting Tutorials]] |
− | [[Category: | + | [[Category:Drawing and Animation Tutorials]] |
+ | [[Category:Game Design Tutorials]] |
Latest revision as of 21:15, 8 January 2019
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.
Contents
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