(Redirected from Ask)
Ask () and Wait | |
ask [] and wait | |
Category | Sensing |
Type | Stack |
Introduced in | 13Feb09 (1.4 alpha) |
The Ask () and Wait block is a Sensing block and a Stack block. The block will make the sprite using the block say the question and show an input box at the bottom of the screen. If the question is being asked by the stage or a hidden sprite, the question will appear above the input instead. 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 were added in 13Feb09 of Scratch 1.4 alpha.
Appearance
If this block is used in a sprite that is showing, the question asked will pop up in a speech bubble, similar to the Say () (block). When the sprite is hidden, or if the block is used in the stage, the question pops up above the input box.
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 [What is your favourite colour?] and wait set [color v] effect to (answer) say [I turned into your favourite colour!]
- Receiving input — asking the user to give a command
ask [Move how many steps?] and wait move (answer) steps
Workarounds
- Main article: List of Block Workarounds
Sensing Method
This block can be somewhat replicated with the following code:
define ask (question) and wait say (question) delete all of [answer v] set [position v] to [1] // where to insert the next letter repeat until <key (join [enter] []) pressed?> detect keys if <<key (right arrow v) pressed?> and <(position) < ((length of [answer v]) + (1))>> then change [position v] by (1) end if <<key (left arrow v) pressed?> and <(position) > [1]>> then change [position v] by (-1) end if <<key (join [][~]) pressed?> and <(position) > [1]>> then // the tilde serves as the backspace key, since it is impossible to let Scratch detect if the backspace key has been pressed change [position v] by (-1) delete (position) of [answer v] end wait until <not <key (any v) pressed?>> end say [] define detect keys // run without screen refresh set [all letters v] to [ !"#$%&'()*+,-./0123456789:;<=>?@[\\\]^_`abcdefghijklmnopqrstuvwxyz{|}] set [letter # v] to [0] repeat (length of (all letters)) change [letter # v] by (1) if <key (letter (letter #) of (all letters)) pressed?> then insert (letter (letter #) of (all letters)) at (position) of [answer v] change [position v] by (1) end end
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 (length of [queue v]) of [queue v] // backspace function when gf clicked delete all 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 which ran this block. 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 are:
Clone Method
define ask (question) // question is asked with this custom block set [question v] to (question) create clone of (myself v) when I start as a clone ask (question) and wait broadcast (stop message v) // question is stopped with this broadcast when I receive [stop message v] // This will delete all clones. If this is not desired, a way to identify clones should be added. delete this clone
Stop Method
// Sprite1 define ask [question] // question is asked with this custom block set [question v] to (question) broadcast (ask v) broadcast (cancel v) // question is cancelled with this broadcast // Sprite2 when I receive [ask v] ask (message) and wait when I receive [cancel v] stop [other scripts in sprite v] // note that this will stop all scripts in Sprite2, so it is best that Sprite2 is empty apart from these two scripts