Abort: Difference between revisions

From Kolmafia
Jump to navigation Jump to search
imported>Fewyn
No edit summary
Convert to Template:Function2 format
 
(One intermediate revision by one other user not shown)
Line 1: Line 1:
{{#customtitle:My header|My Title}}
<onlyinclude>{{{{{format|Function2}}}
{{
|name=abort
#vardefine:name|abort}}{{
|function1.return_type=void
#vardefine:return_type|void}}{{
|function1.description=Immediately halts the current script and all queued functions.
 
|function1.param1=message
FunctionPage|
|function1.param1.type=string
name={{#var:name}}|
|function1.param1.optional=yes
 
|function1.param1.default="Script aborted."
function1={{Function|
|function1.param1.description=(optional) abort message to show in the gCLI
name={{#var:name}}|
|description=This function is often used for basic error handling.
aggregate={{#var:aggregate}}|
|code1={{CodeSample
return_type={{#var:return_type}}|
  |title=Code Samples
return_also={{#var:return_also}}|
  |description=This code will abort the script if run by the "wrong" character.
}}|
  |code=
 
<syntaxhighlight lang="d">
function2={{Function|
if ( my_name() != "MrPicky" )
name={{#var:name}}|
{
aggregate={{#var:aggregate}}|
  abort( "I don't want you to run this script, mister!" );
return_type={{#var:return_type}}|
return_also={{#var:return_also}}|
parameter1={{Param|string|message}}|
p1desc={{Pspan|message}} is the (optional) abort message (in place of "KoLmafia declares world peace.")|
}}|
 
function_description=Halts all queued functions and the entire script it is called from. Often used for error-handling.|
 
code1={{CodeSample|
title=Code Samples|
description=This code will abort the script if run by the "wrong" character.|
code=
<syntaxhighlight>
if (my_name() != "MrPicky") {
  abort("I don't want you to run this script, mister!");
}
}
</syntaxhighlight>}}
</syntaxhighlight>
{{CodeSample|
}}
description=An example of '''abort()''' used for error-handling.|
|code2={{CodeSample
code=
  |description=An example of '''abort()''' used for error-handling.
<syntaxhighlight>
  |code=
<syntaxhighlight lang="d">
float mass = 5.0;
float mass = 5.0;
float volume = some_function(); //where some_function() may return 0.0
float volume = some_function(); // where some_function() may return 0.0
float density;
float density;
if (volume_cf == 0) {
if ( volume_cf == 0 )
  abort("Error: Division by zero requested.");
{
} else {
  abort( "Error: Division by zero requested." );
  density = mass / volume;
}
else
{
  density = mass / volume;
}
}
</syntaxhighlight>}} |
</syntaxhighlight>
cli_equiv=Also available as the CLI command "abort," which will not be queued (similarly stops execution of all remaining commands when parsed).
}}
}}
 
|cli_equiv=Also available as the CLI command "abort," which will not be queued (similarly stops execution of all remaining commands when parsed).
}}</onlyinclude>
[[Category:Miscellaneous Functions]]
[[Category:Miscellaneous Functions]]

Latest revision as of 13:34, 23 December 2020

Function Syntax

void abortstring? message = "Script aborted." )

Immediately halts the current script and all queued functions.
  • message: (optional) abort message to show in the gCLI

This function is often used for basic error handling.

Code Samples

This code will abort the script if run by the "wrong" character.

if ( my_name() != "MrPicky" )
{
   abort( "I don't want you to run this script, mister!" );
}

An example of abort() used for error-handling.

float mass = 5.0;
float volume = some_function(); // where some_function() may return 0.0
float density;
if ( volume_cf == 0 )
{
   abort( "Error: Division by zero requested." );
}
else
{
   density = mass / volume;
}

CLI Equivalent

Also available as the CLI command "abort," which will not be queued (similarly stops execution of all remaining commands when parsed).