User confirm: Difference between revisions
imported>Icon315 Ok now it makes sense |
imported>Fredg1 No edit summary |
||
(3 intermediate revisions by 3 users not shown) | |||
Line 12: | Line 12: | ||
return_also={{#var:return_also}}| | return_also={{#var:return_also}}| | ||
parameter1={{Param|string|message}}| | parameter1={{Param|string|message}}| | ||
}}| | |||
function2={{Function| | |||
name={{#var:name}}| | |||
aggregate={{#var:aggregate}}| | |||
return_type={{#var:return_type}}| | |||
return_also={{#var:return_also}}| | |||
parameter1={{Param|string|message}}| | |||
parameter2={{Param|int|timeOutMillis}}| | |||
parameter3={{Param|boolean|defaultValue}}| | |||
p1desc={{pspan|message}} is the text to display in the confirmation pop-up| | p1desc={{pspan|message}} is the text to display in the confirmation pop-up| | ||
p2desc={{pspan|timeOutMillis}} the length of time to display the dialog, in milliseconds| | |||
p3desc={{pspan|defaultValue}} the value to return if the user does not choose an input before <b>timeOutMillis</b> ms| | |||
}}| | }}| | ||
function_description=Presents the user with a pop-up dialog box with the text in {{pspan|message}} (can use escaped characters, such as "<nowiki>\n</nowiki>" for a newline); returns true if the user selects "Yes" and false if the user selects "No."</p> | function_description=Presents the user with a pop-up dialog box with the text in {{pspan|message}} (can use escaped characters, such as "<nowiki>\n</nowiki>" for a newline); returns true if the user selects "Yes" and false if the user selects "No."</p> | ||
<p>If you wish for the dialog to "time out" and return a default value if the user does not select an option, use the three-parameter version.</p> | |||
<p>Please use these sparingly, as it can defeat the purpose of scripting if the user must constantly provide information during execution.| | <p>Please use these sparingly, as it can defeat the purpose of scripting if the user must constantly provide information during execution.| | ||
Line 23: | Line 36: | ||
code= | code= | ||
<syntaxhighlight> | <syntaxhighlight> | ||
if (user_confirm("Are you sure you want to proceed with executing this script?") ) | if (!user_confirm("Are you sure you want to proceed with executing this script?") ) | ||
{ | { | ||
abort("Script execution canceled by user."); | abort("Script execution canceled by user."); | ||
} | } | ||
</syntaxhighlight>}}| | </syntaxhighlight>}}| | ||
code2={{CodeSample| | |||
title=Conditional pop-ups| | |||
description=By taking in consideration that ASH has lazy booleans, it is possible to make confirmation messages that will only appear if the user doesn't satisfy a condition without having to isolate the user_confirm() call.<br>In this example, the user is asked to input the amount of adventures to spend. If they have at least that much, the script simply executes, without a pop-up appearing. Otherwise, a pop-up appears, and the script only executes if the user answers yes.| | |||
code= | |||
<syntaxhighlight> | |||
void main( int adventures_to_spend ) | |||
{ | |||
if (my_adventures() < adventures_to_spend && !user_confirm("You don't have that many adventures. Use as many as possible instead?")) | |||
{ | |||
abort("Script execution canceled by user."); | |||
} | |||
// Rest of the script | |||
} | |||
</syntaxhighlight>| | |||
moreinfo=See [[Operators#Boolean_Operators]] for how/why this works.}}| | |||
}} | }} | ||
[[Category:Miscellaneous Functions]] | [[Category:Miscellaneous Functions]] |
Latest revision as of 11:12, 23 October 2020
Function Syntax
boolean user_confirm(string message )
boolean user_confirm(string message ,int timeOutMillis ,boolean defaultValue )
- message is the text to display in the confirmation pop-up
- timeOutMillis the length of time to display the dialog, in milliseconds
- defaultValue the value to return if the user does not choose an input before timeOutMillis ms
Presents the user with a pop-up dialog box with the text in message (can use escaped characters, such as "\n" for a newline); returns true if the user selects "Yes" and false if the user selects "No."
If you wish for the dialog to "time out" and return a default value if the user does not select an option, use the three-parameter version.
Please use these sparingly, as it can defeat the purpose of scripting if the user must constantly provide information during execution.
Code Sample
A check to see if the user truly wants to execute a script.
if (!user_confirm("Are you sure you want to proceed with executing this script?") )
{
abort("Script execution canceled by user.");
}
Conditional pop-ups
By taking in consideration that ASH has lazy booleans, it is possible to make confirmation messages that will only appear if the user doesn't satisfy a condition without having to isolate the user_confirm() call.
In this example, the user is asked to input the amount of adventures to spend. If they have at least that much, the script simply executes, without a pop-up appearing. Otherwise, a pop-up appears, and the script only executes if the user answers yes.
void main( int adventures_to_spend )
{
if (my_adventures() < adventures_to_spend && !user_confirm("You don't have that many adventures. Use as many as possible instead?"))
{
abort("Script execution canceled by user.");
}
// Rest of the script
}
See Operators#Boolean_Operators for how/why this works.