Abort: Difference between revisions

From Kolmafia
Jump to navigation Jump to search
imported>StDoodle
m moved Abort() to Abort
Convert to Template:Function2 format
 
(12 intermediate revisions by 4 users not shown)
Line 1: Line 1:
'''abort([string message])'''
<onlyinclude>{{{{{format|Function2}}}
<br />Aborts the current script, printing the message passed in red. If no message is passed, it will display the all-too-familiar "KoLMafia declares world peace."
|name=abort
<br />Note: "abort" is also usable as a CLI command, and will not be queued.
|function1.return_type=void
 
|function1.description=Immediately halts the current script and all queued functions.
<p><pre># useless function, but serves as a code snippet
|function1.param1=message
void do_ns_maze()
|function1.param1.type=string
if (item_amount($item[hedge maze puzzle]) < 1) abort("You have no puzzle pieces, man.");
|function1.param1.optional=yes
else cli_execute("maze");
|function1.param1.default="Script aborted."
}</pre></p>
|function1.param1.description=(optional) abort message to show in the gCLI
|description=This function is often used for basic error handling.
|code1={{CodeSample
  |title=Code Samples
  |description=This code will abort the script if run by the "wrong" character.
  |code=
<syntaxhighlight lang="d">
if ( my_name() != "MrPicky" )
{
  abort( "I don't want you to run this script, mister!" );
}
</syntaxhighlight>
}}
|code2={{CodeSample
  |description=An example of '''abort()''' used for error-handling.
  |code=
<syntaxhighlight lang="d">
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;
}
</syntaxhighlight>
}}
|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]]

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).