(It's because under 1.4 don't know of it.)
m (New 3.0 image)
(31 intermediate revisions by 20 users not shown)
Line 1: Line 1:
{{Block
+
{{block
 
|name = Ask () and Wait
 
|name = Ask () and Wait
|image = File:Ask_()_and_Wait.png
+
|image = File:Ask () and Wait.png
 
|category = [[Sensing Blocks|Sensing]]
 
|category = [[Sensing Blocks|Sensing]]
 
|type = [[Stack Block|Stack]]
 
|type = [[Stack Block|Stack]]
|default = What's your name?
+
|added = [[1.4]]
|argType = string
 
|added = 1.4
 
 
}}
 
}}
The '''Ask () and Wait''' [[block]] is a [[Sensing Blocks|Sensing block]] and a [[Stack Block|Stack block]]. The block will make an input box (with the specified text above it) show at the bottom of the screen. [[Scratchers]] can input text into it and submit it {{-}} the input is stored in the [[Answer (block)|Answer]] block. The Answer block automatically updates to the latest input.
+
[[File:Ask bar.png|thumb|300px|The [[Scratch Cat]] asking a question]]
[[File:Ask bar.png|thumb|250px|The dialog the question is asked in.]]
+
The '''Ask () and Wait''' [[Blocks|block]] is a [[Sensing Blocks|Sensing block]] and a [[Stack Block|Stack block]]. The block will make an input box (with the specified text above it) show at the bottom of the screen. [[Scratchers]] can input text into it and submit it, and the input is stored then in the [[Answer (block)|Answer]] block. The Answer block automatically updates to most recent input.
  
In native [[Scratch]] and the [[Flash Player]], text can be pasted in, but not in the [[Java Player]].
+
This block and the [[Answer (block)|answer]] block appear [[Obsolete Blocks|obsolete]] in versions of [[Scratch]] under 1.4, because they were only introduced in 1.4.
  
This block and the [[Answer (block)|answer]] block become obsolete in versions of [[Scratch]] under 1.4, because they were only introduced in 1.4.
+
==Example Uses==
 +
As this block allows users to input any text they want, it is widely used when a user must communicate with the [[project]].
 +
 
 +
Some common uses for the Ask () and Wait block:
 +
* Chatbots {{-}} receiving information from the user
 +
 
 +
<scratchblocks>ask [What's your name?] and wait
 +
say (join [Hello, ] (join (answer) [.]))</scratchblocks>
 +
 
 +
* Setting preferences {{-}} coordinates, color, and so on
 +
 
 +
<scratchblocks>ask [Color?] and wait
 +
set [color v] effect to (answer)</scratchblocks>
 +
 
 +
* Receiving input {{-}} asking the user to give a command
 +
 
 +
<scratchblocks>ask [Move how many steps?] and wait
 +
move (answer) steps</scratchblocks>
  
 
==Workaround==
 
==Workaround==
 
{{main|List of Block Workarounds}}
 
{{main|List of Block Workarounds}}
 +
 +
===Sensing Method===
 
This block can be somewhat replicated with the following code:
 
This block can be somewhat replicated with the following code:
  
Line 23: Line 40:
 
set [count v] to [0]
 
set [count v] to [0]
 
repeat until <<touching [mouse-pointer v]?> and <mouse down?>>
 
repeat until <<touching [mouse-pointer v]?> and <mouse down?>>
if <key [a v] pressed?>
+
if <key [a v] pressed?> then
 
change [count v] by (1)
 
change [count v] by (1)
 
insert [A] at (count) of [answer v]
 
insert [A] at (count) of [answer v]
 
end
 
end
if <key [b v] pressed?>
+
if <key [b v] pressed?> then
 
change [count v] by (1)
 
change [count v] by (1)
 
insert [B] at (count) of [answer v]
 
insert [B] at (count) of [answer v]
 
end
 
end
if <key [c v] pressed?>
+
if <key [c v] pressed?> then
 
change [count v] by (1)
 
change [count v] by (1)
 
insert [C] at (count) of [answer v]
 
insert [C] at (count) of [answer v]
 
end
 
end
if <key [d v] pressed?>
+
if <key [d v] pressed?> then
 
change [count v] by (1)
 
change [count v] by (1)
 
insert [D] at (count) of [answer v]
 
insert [D] at (count) of [answer v]
 
end
 
end
if <key [e v] pressed?>
+
if <key [e v] pressed?> then
 
change [count v] by (1)
 
change [count v] by (1)
 
insert [E] at (count) of [answer v]
 
insert [E] at (count) of [answer v]
 
end
 
end
if <key [f v] pressed?>
+
if <key [f v] pressed?> then
 
change [count v] by (1)
 
change [count v] by (1)
 
insert [F] at (count) of [answer v]
 
insert [F] at (count) of [answer v]
 
end
 
end
if <key [g v] pressed?>
+
if <key [g v] pressed?> then
 
change [count v] by (1)
 
change [count v] by (1)
 
insert [G] at (count) of [answer v]
 
insert [G] at (count) of [answer v]
 
end
 
end
if <key [h v] pressed?>
+
if <key [h v] pressed?> then
 
change [count v] by (1)
 
change [count v] by (1)
 
insert [H] at (count) of [answer v]
 
insert [H] at (count) of [answer v]
 
end
 
end
if <key [i v] pressed?>
+
if <key [i v] pressed?> then
 
change [count v] by (1)
 
change [count v] by (1)
 
insert [I] at (count) of [answer v]
 
insert [I] at (count) of [answer v]
 
end
 
end
if <key [j v] pressed?>
+
if <key [j v] pressed?> then
 
change [count v] by (1)
 
change [count v] by (1)
 
insert [j] at (count) of [answer v]
 
insert [j] at (count) of [answer v]
 
end
 
end
if <key [k v] pressed?>
+
if <key [k v] pressed?> then
 
change [count v] by (1)
 
change [count v] by (1)
 
insert [K] at (count) of [answer v]
 
insert [K] at (count) of [answer v]
 
end
 
end
if <key [l v] pressed?>
+
if <key [l v] pressed?> then
 
change [count v] by (1)
 
change [count v] by (1)
 
insert [L] at (count) of [answer v]
 
insert [L] at (count) of [answer v]
 
end
 
end
if <key [m v] pressed?>
+
if <key [m v] pressed?> then
 
change [count v] by (1)
 
change [count v] by (1)
 
insert [M] at (count) of [answer v]
 
insert [M] at (count) of [answer v]
 
end
 
end
if <key [n v] pressed?>
+
if <key [n v] pressed?> then
 
change [count v] by (1)
 
change [count v] by (1)
 
insert [N] at (count) of [answer v]
 
insert [N] at (count) of [answer v]
 
end
 
end
if <key [o v] pressed?>
+
if <key [o v] pressed?> then
 
change [count v] by (1)
 
change [count v] by (1)
 
insert [O] at (count) of [answer v]
 
insert [O] at (count) of [answer v]
 
end
 
end
if <key [p v] pressed?>
+
if <key [p v] pressed?> then
 
change [count v] by (1)
 
change [count v] by (1)
 
insert [P] at (count) of [answer v]
 
insert [P] at (count) of [answer v]
 
end
 
end
if <key [q v] pressed?>
+
if <key [q v] pressed?> then
 
change [count v] by (1)
 
change [count v] by (1)
 
insert [Q] at (count) of [answer v]
 
insert [Q] at (count) of [answer v]
 
end
 
end
if <key [r v] pressed?>
+
if <key [r v] pressed?> then
 
change [count v] by (1)
 
change [count v] by (1)
 
insert [R] at (count) of [answer v]
 
insert [R] at (count) of [answer v]
 
end
 
end
if <key [s v] pressed?>
+
if <key [s v] pressed?> then
 
change [count v] by (1)
 
change [count v] by (1)
 
insert [S] at (count) of [answer v]
 
insert [S] at (count) of [answer v]
 
end
 
end
if <key [t v] pressed?>
+
if <key [t v] pressed?> then
 
change [count v] by (1)
 
change [count v] by (1)
 
insert [T] at (count) of [answer v]
 
insert [T] at (count) of [answer v]
 
end
 
end
if <key [u v] pressed?>
+
if <key [u v] pressed?> then
 
change [count v] by (1)
 
change [count v] by (1)
 
insert [U] at (count) of [answer v]
 
insert [U] at (count) of [answer v]
 
end
 
end
if <key [v v] pressed?>
+
if <key [v v] pressed?> then
 
change [count v] by (1)
 
change [count v] by (1)
 
insert [V] at (count) of [answer v]
 
insert [V] at (count) of [answer v]
 
end
 
end
if <key [w v] pressed?>
+
if <key [w v] pressed?> then
 
change [count v] by (1)
 
change [count v] by (1)
 
insert [W] at (count) of [answer v]
 
insert [W] at (count) of [answer v]
 
end
 
end
if <key [x v] pressed?>
+
if <key [x v] pressed?> then
 
change [count v] by (1)
 
change [count v] by (1)
 
insert [X] at (count) of [answer v]
 
insert [X] at (count) of [answer v]
 
end
 
end
if <key [y v] pressed?>
+
if <key [y v] pressed?> then
 
change [count v] by (1)
 
change [count v] by (1)
 
insert [Y] at (count) of [answer v]
 
insert [Y] at (count) of [answer v]
 
end
 
end
if <key [z v] pressed?>
+
if <key [z v] pressed?> then
 
change [count v] by (1)
 
change [count v] by (1)
 
insert [Z] at (count) of [answer v]
 
insert [Z] at (count) of [answer v]
 
end
 
end
if <key [0 v] pressed?>
+
if <key [0 v] pressed?> then
 
change [count v] by (1)
 
change [count v] by (1)
 
insert [0] at (count) of [answer v]
 
insert [0] at (count) of [answer v]
 
end
 
end
if <key [1 v] pressed?>
+
if <key [1 v] pressed?> then
 
change [count v] by (1)
 
change [count v] by (1)
 
insert [1] at (count) of [answer v]
 
insert [1] at (count) of [answer v]
 
end
 
end
if <key [2 v] pressed?>
+
if <key [2 v] pressed?> then
 
change [count v] by (1)
 
change [count v] by (1)
 
insert [2] at (count) of [answer v]
 
insert [2] at (count) of [answer v]
 
end
 
end
if <key [3 v] pressed?>
+
if <key [3 v] pressed?> then
 
change [count v] by (1)
 
change [count v] by (1)
 
insert [3] at (count) of [answer v]
 
insert [3] at (count) of [answer v]
 
end
 
end
if <key [4 v] pressed?>
+
if <key [4 v] pressed?> then
 
change [count v] by (1)
 
change [count v] by (1)
 
insert [4] at (count) of [answer v]
 
insert [4] at (count) of [answer v]
 
end
 
end
if <key [5 v] pressed?>
+
if <key [5 v] pressed?> then
 
change [count v] by (1)
 
change [count v] by (1)
 
insert [5] at (count) of [answer v]
 
insert [5] at (count) of [answer v]
 
end
 
end
if <key [6 v] pressed?>
+
if <key [6 v] pressed?> then
 
change [count v] by (1)
 
change [count v] by (1)
 
insert [6] at (count) of [answer v]
 
insert [6] at (count) of [answer v]
 
end
 
end
if <key [7 v] pressed?>
+
if <key [7 v] pressed?> then
 
change [count v] by (1)
 
change [count v] by (1)
 
insert [7] at (count) of [answer v]
 
insert [7] at (count) of [answer v]
 
end
 
end
if <key [8 v] pressed?>
+
if <key [8 v] pressed?> then
 
change [count v] by (1)
 
change [count v] by (1)
 
insert [8] at (count) of [answer v]
 
insert [8] at (count) of [answer v]
 
end
 
end
if <key [9 v] pressed?>
+
if <key [9 v] pressed?> then
 
change [count v] by (1)
 
change [count v] by (1)
 
insert [9] at (count) of [answer v]
 
insert [9] at (count) of [answer v]
 
end
 
end
if <key [right arrow v] pressed?>
+
if <<key [right arrow v] pressed?> and <(count) < ((length of [answer v]) + (1))>> then
 
change [count v] by (1)
 
change [count v] by (1)
 
end
 
end
if <key [left arrow v] pressed?>
+
if <<key [left arrow v] pressed?> and <(count) > [1]>> then
 
change [count v] by (-1)
 
change [count v] by (-1)
 
end
 
end
if <key [space v] pressed?>
+
if <key [space v] pressed?> then
 
change [count v] by (1)
 
change [count v] by (1)
 
insert [] at (count) of [answer v]
 
insert [] at (count) of [answer v]
 
end
 
end
wait (0.001) secs
+
if <<key (backspace) pressed?> and <(count) > [1]>> then // hacked block
 +
change [count v] by (-1)
 +
delete (count) of [answer v]
 +
end
 +
wait until <not <key [any v] pressed?>>
 
end
 
end
 
say []</scratchblocks>
 
say []</scratchblocks>
Line 184: Line 205:
 
The [[list]] "answer" will replace the [[Answer (block)|answer block]].
 
The [[list]] "answer" will replace the [[Answer (block)|answer block]].
  
==Example Uses==
+
===Hat Block Method===
As this block allows users to input any text they want, it is widely used when a user must communicate with the [[project]].
+
Text input can be retrieved in correct order and speed by using this method.
 +
<scratchblocks>
 +
when [a v] key pressed
 +
add [a] to [queue v]
 +
 
 +
when [b v] key pressed
 +
add [b] to [queue v]
 +
 
 +
when [c v] key pressed
 +
add [c] to [queue v]
 +
 
 +
//and so forth for each key
 +
 
 +
when [left arrow v] key pressed
 +
delete (last v) of [queue v] // backspace function
 +
 
 +
when gf clicked
 +
delete (all v) of [queue v]
 +
wait until <key [right arrow v] pressed?> // this will confirm the user is done inputting
 +
set [clump v] to [] // blank
 +
set [n v] to [1]
 +
repeat (length of [queue v]) // compile list into a single string
 +
set [clump v] to (join (clump) (item (n) of [queue v])
 +
change [n v] by (1)
 +
end
 +
 
 +
(clump) // will be the answer
 +
</scratchblocks>
 +
 
 +
==Cancellation==
 +
To cancel the text box that appears once this block is activated; one must stop the script from which the ask came.
 +
 
 +
An example of when one might want to cancel the text box is if an answer is not received in a certain amount of time, or if a cancel button is pressed.
  
Some common uses for the '''Ask () and Wait''' block:
+
A few methods of cancellation:
*Chatbots {{-}} receiving information from the user
 
  
<scratchblocks>ask [What's your name?] and wait
+
<scratchblocks>
say (join [Hello, ] (join (answer) [.]))</scratchblocks>
+
// broadcast method
 +
(message)::grey hat
 +
(cancel?)::grey cap
 +
define ask-calcellable [message] and wait
 +
set [message v] to (message)
 +
set [cancel v] to [0]
 +
broadcast [ask v] and wait//an alternative version of the custom block could not wait here
 +
define cancel ask
 +
set [cancel v] to [1]
 +
broadcast [ask v] and wait//wait for cancel to finish. optional
 +
when I receive [ask v]
 +
if < (cancel?) = [0] > :: control
 +
  ask (message) and wait
 +
else
 +
  say [] // in some scratches, the bubble would otherwise stay up
 +
end
  
*Setting preferences {{-}} coordinates, color, and so on
+
// clone method
 +
((message)::grey)
 +
define ask-calcellable [message] and wait
 +
set [message v] to (message)
 +
create clone of [myself v] // this means that no other clones can be used
 +
broadcast [ask v] and wait // the wait part is the important part
 +
when I receive [ask v]
 +
ask (message) and wait
 +
define cancel ask
 +
broadcast [cancel v] and wait
 +
when I receive [cancel v]
 +
delete this clone
  
<scratchblocks>ask [Color?] and wait
+
// stop method
set [color v] effect to (answer)</scratchblocks>
+
// Stage
 +
((message)::grey)
  
*Receiving input {{-}} asking the user to give a command
+
// Sprite1
 +
define ask-calcellable [message] and wait
 +
set [message v] to (message)
 +
broadcast [ask v] and wait
 +
define cancel ask
 +
broadcast [cancel v] and wait
  
<scratchblocks>ask [Move how many steps?] and wait
+
// Sprite2
move (answer) steps</scratchblocks>
+
when I receive [ask v]
 +
ask (message) and wait
 +
when I receive [cancel v]
 +
stop [other scripts in sprite v]
 +
</scratchblocks>
 +
An example of this can be found [https://scratch.mit.edu/projects/87115370/ here].
  
 
==See Also==
 
==See Also==
*[[Answer (block)]]
+
* [[Answer (block)]]
*[[Answer (value)]]
+
* [[Answer (value)]]
 
{{Sensing Blocks}}
 
{{Sensing Blocks}}
  
 
[[Category:Sensing Blocks]]
 
[[Category:Sensing Blocks]]
 +
[[de:Frage () und warte (Block)]]
 +
[[ja:() と聞いて待つ (ブロック)]]
 +
[[ru: Спросить () и ждать (блок)]]

Revision as of 01:35, 21 June 2019

Ask () and Wait
Ask () and Wait.png
Category Sensing
Type Stack
Introduced in 1.4
The Scratch Cat asking a question

The Ask () and Wait block is a Sensing block and a Stack block. The block will make an input box (with the specified text above it) show at the bottom of the screen. Scratchers can input text into it and submit it, and the input is stored then in the Answer block. The Answer block automatically updates to most recent input.

This block and the answer block appear obsolete in versions of Scratch under 1.4, because they were only introduced in 1.4.

Example Uses

As this block allows users to input any text they want, it is widely used when a user must communicate with the project.

Some common uses for the Ask () and Wait block:

  • Chatbots — receiving information from the user
ask [What's your name?] and wait
say (join [Hello, ] (join (answer) [.]))
  • Setting preferences — coordinates, color, and so on
ask [Color?] and wait
set [color v] effect to (answer)
  • Receiving input — asking the user to give a command
ask [Move how many steps?] and wait
move (answer) steps

Workaround

Main article: List of Block Workarounds


Sensing Method

This block can be somewhat replicated with the following code:

say (question)
delete (all v) of [answer v]
set [count v] to [0]
repeat until <<touching [mouse-pointer v]?> and <mouse down?>>
if <key [a v] pressed?> then
change [count v] by (1)
insert [A] at (count) of [answer v]
end
if <key [b v] pressed?> then
change [count v] by (1)
insert [B] at (count) of [answer v]
end
if <key [c v] pressed?> then
change [count v] by (1)
insert [C] at (count) of [answer v]
end
if <key [d v] pressed?> then
change [count v] by (1)
insert [D] at (count) of [answer v]
end
if <key [e v] pressed?> then
change [count v] by (1)
insert [E] at (count) of [answer v]
end
if <key [f v] pressed?> then
change [count v] by (1)
insert [F] at (count) of [answer v]
end
if <key [g v] pressed?> then
change [count v] by (1)
insert [G] at (count) of [answer v]
end
if <key [h v] pressed?> then
change [count v] by (1)
insert [H] at (count) of [answer v]
end
if <key [i v] pressed?> then
change [count v] by (1)
insert [I] at (count) of [answer v]
end
if <key [j v] pressed?> then
change [count v] by (1)
insert [j] at (count) of [answer v]
end
if <key [k v] pressed?> then
change [count v] by (1)
insert [K] at (count) of [answer v]
end
if <key [l v] pressed?> then
change [count v] by (1)
insert [L] at (count) of [answer v]
end
if <key [m v] pressed?> then
change [count v] by (1)
insert [M] at (count) of [answer v]
end
if <key [n v] pressed?> then
change [count v] by (1)
insert [N] at (count) of [answer v]
end
if <key [o v] pressed?> then
change [count v] by (1)
insert [O] at (count) of [answer v]
end
if <key [p v] pressed?> then
change [count v] by (1)
insert [P] at (count) of [answer v]
end
if <key [q v] pressed?> then
change [count v] by (1)
insert [Q] at (count) of [answer v]
end
if <key [r v] pressed?> then
change [count v] by (1)
insert [R] at (count) of [answer v]
end
if <key [s v] pressed?> then
change [count v] by (1)
insert [S] at (count) of [answer v]
end
if <key [t v] pressed?> then
change [count v] by (1)
insert [T] at (count) of [answer v]
end
if <key [u v] pressed?> then
change [count v] by (1)
insert [U] at (count) of [answer v]
end
if <key [v v] pressed?> then
change [count v] by (1)
insert [V] at (count) of [answer v]
end
if <key [w v] pressed?> then
change [count v] by (1)
insert [W] at (count) of [answer v]
end
if <key [x v] pressed?> then
change [count v] by (1)
insert [X] at (count) of [answer v]
end
if <key [y v] pressed?> then
change [count v] by (1)
insert [Y] at (count) of [answer v]
end
if <key [z v] pressed?> then
change [count v] by (1)
insert [Z] at (count) of [answer v]
end
if <key [0 v] pressed?> then
change [count v] by (1)
insert [0] at (count) of [answer v]
end
if <key [1 v] pressed?> then
change [count v] by (1)
insert [1] at (count) of [answer v]
end
if <key [2 v] pressed?> then
change [count v] by (1)
insert [2] at (count) of [answer v]
end
if <key [3 v] pressed?> then
change [count v] by (1)
insert [3] at (count) of [answer v]
end
if <key [4 v] pressed?> then
change [count v] by (1)
insert [4] at (count) of [answer v]
end
if <key [5 v] pressed?> then
change [count v] by (1)
insert [5] at (count) of [answer v]
end
if <key [6 v] pressed?> then
change [count v] by (1)
insert [6] at (count) of [answer v]
end
if <key [7 v] pressed?> then
change [count v] by (1)
insert [7] at (count) of [answer v]
end
if <key [8 v] pressed?> then
change [count v] by (1)
insert [8] at (count) of [answer v]
end
if <key [9 v] pressed?> then
change [count v] by (1)
insert [9] at (count) of [answer v]
end
if <<key [right arrow v] pressed?> and <(count) < ((length of [answer v]) + (1))>> then
change [count v] by (1)
end
if <<key [left arrow v] pressed?> and <(count) > [1]>> then
change [count v] by (-1)
end
if <key [space v] pressed?> then
change [count v] by (1)
insert [] at (count) of [answer v]
end
if <<key (backspace) pressed?> and <(count) > [1]>> then // hacked block
change [count v] by (-1)
delete (count) of [answer v]
end
wait until <not <key [any v] pressed?>>
end
say []
Note Note: This does not work perfectly. See this project for more information.

The list "answer" will replace the answer block.

Hat Block Method

Text input can be retrieved in correct order and speed by using this method.

when [a v] key pressed
add [a] to [queue v]

when [b v] key pressed
add [b] to [queue v]

when [c v] key pressed
add [c] to [queue v]

//and so forth for each key

when [left arrow v] key pressed
delete (last v) of [queue v] // backspace function

when gf clicked
delete (all v) of [queue v]
wait until <key [right arrow v] pressed?> // this will confirm the user is done inputting
set [clump v] to [] // blank
set [n v] to [1]
repeat (length of [queue v]) // compile list into a single string
set [clump v] to (join (clump) (item (n) of [queue v])
change [n v] by (1)
end

(clump) // will be the answer

Cancellation

To cancel the text box that appears once this block is activated; one must stop the script from which the ask came.

An example of when one might want to cancel the text box is if an answer is not received in a certain amount of time, or if a cancel button is pressed.

A few methods of cancellation:

// broadcast method
(message)::grey hat
(cancel?)::grey cap
define ask-calcellable [message] and wait
set [message v] to (message)
set [cancel v] to [0]
broadcast [ask v] and wait//an alternative version of the custom block could not wait here
define cancel ask
set [cancel v] to [1]
broadcast [ask v] and wait//wait for cancel to finish. optional
when I receive [ask v]
if < (cancel?) = [0] > :: control
  ask (message) and wait
else
  say [] // in some scratches, the bubble would otherwise stay up
end

// clone method
((message)::grey)
define ask-calcellable [message] and wait
set [message v] to (message)
create clone of [myself v] // this means that no other clones can be used
broadcast [ask v] and wait // the wait part is the important part
when I receive [ask v]
ask (message) and wait
define cancel ask
broadcast [cancel v] and wait
when I receive [cancel v]
delete this clone

// stop method
// Stage
((message)::grey)

// Sprite1
define ask-calcellable [message] and wait
set [message v] to (message)
broadcast [ask v] and wait
define cancel ask
broadcast [cancel v] and wait

// Sprite2
when I receive [ask v]
ask (message) and wait
when I receive [cancel v]
stop [other scripts in sprite v]

An example of this can be found here.

See Also