Tips, Tricks and Workarounds: Difference between revisions
imported>Bale Anonymous closures |
m →Capturing an Abort: Formatting improvements |
||
(4 intermediate revisions by 3 users not shown) | |||
Line 1: | Line 1: | ||
{{TOCright}} | {{TOCright}} | ||
This page contains various tips and tricks that may help you in using KoLmafia and writing scripts. | |||
==Free Pulls== | ==Free Pulls== | ||
Line 34: | Line 36: | ||
== Capturing an Abort == | == Capturing an Abort == | ||
Several ASH functions return a {{type|boolean}} to indicate success (<code>true</code>) or failure (<code>false</code>). While useful for error handling, it's easy to accidentally forget to check this value. To help script authors, KoLmafia automatically checks the value for you and aborts the script ''if the value is not used''. | |||
< | |||
For example, suppose you call the {{f|hermit}} function. If you call it without using the return value in any way, KoLmafia will abort if the function fails: | |||
<syntaxhighlight lang="d"> | |||
hermit(1, $item[ ten-leaf clover ]); | |||
</syntaxhighlight> | |||
However, if the value is "captured" and used in any way, KoLmafia assumes that you want to manually handle the failure, and will not abort regardless of the return value. | |||
The following is considered as "capturing" a variable. | |||
{{CodeSample| | {{CodeSample| | ||
description= | description=Assigning the return value to a boolean variable.| | ||
code=<syntaxhighlight> | code=<syntaxhighlight lang="d"> | ||
boolean fakeBool = hermit(1, $item[ | boolean fakeBool = hermit(1, $item[ ten-leaf clover ]); | ||
</syntaxhighlight>}} | </syntaxhighlight>}} | ||
{{CodeSample| | {{CodeSample| | ||
description= | description=Checking the return value in a conditional statement (e.g. <code>if</code>-block).| | ||
code=<syntaxhighlight> | code=<syntaxhighlight lang="d"> | ||
if(!hermit(1, $item[ | if (!hermit(1, $item[ ten-leaf clover ])) { | ||
print("Hermit looting failed: continuing on."); | |||
} | |||
</syntaxhighlight>}} | </syntaxhighlight>}} | ||
{{CodeSample| | {{CodeSample| | ||
description=An if | description=An <code>if-</code>block can be empty.| | ||
code=<syntaxhighlight> | code=<syntaxhighlight lang="d"> | ||
if(hermit(1, $item[ | if (hermit(1, $item[ ten-leaf clover ])) {} | ||
</syntaxhighlight>}} | </syntaxhighlight>}} | ||
{{CodeSample| | {{CodeSample| | ||
description=The most | description=The most minimal method is to wrap it with a logical not operator (<code>!</code>) and parentheses.| | ||
code=<syntaxhighlight> | code=<syntaxhighlight lang="d"> | ||
(!hermit(1, $item[ | (!hermit(1, $item[ ten-leaf clover ])); | ||
</syntaxhighlight>}} | </syntaxhighlight>}} | ||
This only applies to the following built-in functions: | |||
* {{f|adv1}} | |||
* {{f|adventure}} | |||
* {{f|autosell}} | |||
* {{f|batch_close}} | |||
* {{f|bjornify_familiar}} | |||
* {{f|buy}} | |||
* {{f|change_mcd}} | |||
* {{f|chew}} | |||
* {{f|cli_execute}} | |||
* {{f|create}} | |||
* {{f|drink}} | |||
* {{f|eatsilent}} | |||
* {{f|eat}} | |||
* {{f|empty_closet}} | |||
* {{f|enthrone_familiar}} | |||
* {{f|equip_all_familiars}} | |||
* {{f|equip}} | |||
* {{f|hedge_maze}} | |||
* {{f|hermit}} | |||
* {{f|outfit}} | |||
* {{f|overdrink}} | |||
* {{f|pick_pocket}} | |||
* {{f|put_closet}} | |||
* {{f|put_display}} | |||
* {{f|put_shop}} | |||
* {{f|put_stash}} | |||
* {{f|refresh_shop}} | |||
* {{f|refresh_stash}} | |||
* {{f|refresh_status}} | |||
* {{f|reprice_shop}} | |||
* {{f|retrieve_item}} | |||
* {{f|sell}} | |||
* {{f|sweet_synthesis}} | |||
* {{f|take_closet}} | |||
* {{f|take_display}} | |||
* {{f|take_shop}} | |||
* {{f|take_stash}} | |||
* {{f|take_storage}} | |||
* {{f|tower_door}} | |||
* {{f|use_familiar}} | |||
* {{f|use_servant}} | |||
* {{f|use_skill}} | |||
* {{f|use}} | |||
* {{f|visit}} | |||
* and possibly more... | |||
Note: KoLmafia does not check the return value of user-defined functions. | |||
== Syntax Highlighting for Notepad++ == | == Syntax Highlighting for Notepad++ == | ||
== Testing Scripts Offline == | |||
Because KoLmafia CLI and ASH are interpreted, it is impossible to know whether there are any errors in your scripts until you run or validate them. If you want to check only the syntax of the script, you can validate it without logging in. From the top menu of the login pane, select "General", then "Graphical CLI". The CLI (and the rest of the mafia interface) will appear. You can then use the validate CLI command to verify their syntax. If the script is purely informational then you can even run it, but note that because you are not logged in, any functions or commands that use or interact with KoL will not work. Thus, testing scripts this way will not reveal any KoL-related logical errors. | |||
[[Category:Scripting]] | [[Category:Scripting]] |
Latest revision as of 20:37, 3 July 2021
This page contains various tips and tricks that may help you in using KoLmafia and writing scripts.
Free Pulls
KoLmafia considers free pulls as completely separate from other items in Hangks. Because there is no resource being consumed by pulling them, they do not use the functions involved in pulls, specifically storage_amount()
and take_storage()
.
To find the quantity of a free pull in Hangks, such as a brick you need to use available_amount()
. To remove a free pull item from Hangks you need to use retrieve_item()
.
Anonymous closures
ASH does not seem to accept anonymous closures by default. This is an example of an anonymous closure.
int var1;
/* Do stuff */
{
int var1;
call_some_function();
/* Do other stuff */
}
However, you can use a workaround with a if-clause like the following example.
int var1;
/* Do stuff */
if (true)
{
int var1;
/* Do other stuff */
}
As for where stuff like this could be used... It can initialize ASH scripts without creating any unnecessary functions or variables in the "namespace". Not everyone's cup of tea, but at least you know.
Capturing an Abort
Several ASH functions return a boolean to indicate success (true
) or failure (false
). While useful for error handling, it's easy to accidentally forget to check this value. To help script authors, KoLmafia automatically checks the value for you and aborts the script if the value is not used.
For example, suppose you call the hermit()
function. If you call it without using the return value in any way, KoLmafia will abort if the function fails:
hermit(1, $item[ ten-leaf clover ]);
However, if the value is "captured" and used in any way, KoLmafia assumes that you want to manually handle the failure, and will not abort regardless of the return value.
The following is considered as "capturing" a variable.
Assigning the return value to a boolean variable.
boolean fakeBool = hermit(1, $item[ ten-leaf clover ]);
Checking the return value in a conditional statement (e.g. if
-block).
if (!hermit(1, $item[ ten-leaf clover ])) {
print("Hermit looting failed: continuing on.");
}
An if-
block can be empty.
if (hermit(1, $item[ ten-leaf clover ])) {}
The most minimal method is to wrap it with a logical not operator (!
) and parentheses.
(!hermit(1, $item[ ten-leaf clover ]));
This only applies to the following built-in functions:
adv1()
adventure()
autosell()
batch_close()
bjornify_familiar()
buy()
change_mcd()
chew()
cli_execute()
create()
drink()
eatsilent()
eat()
empty_closet()
enthrone_familiar()
equip_all_familiars()
equip()
hedge_maze()
hermit()
outfit()
overdrink()
pick_pocket()
put_closet()
put_display()
put_shop()
put_stash()
refresh_shop()
refresh_stash()
refresh_status()
reprice_shop()
retrieve_item()
sell()
sweet_synthesis()
take_closet()
take_display()
take_shop()
take_stash()
take_storage()
tower_door()
use_familiar()
use_servant()
use_skill()
use()
visit()
- and possibly more...
Note: KoLmafia does not check the return value of user-defined functions.
Syntax Highlighting for Notepad++
Testing Scripts Offline
Because KoLmafia CLI and ASH are interpreted, it is impossible to know whether there are any errors in your scripts until you run or validate them. If you want to check only the syntax of the script, you can validate it without logging in. From the top menu of the login pane, select "General", then "Graphical CLI". The CLI (and the rest of the mafia interface) will appear. You can then use the validate CLI command to verify their syntax. If the script is purely informational then you can even run it, but note that because you are not logged in, any functions or commands that use or interact with KoL will not work. Thus, testing scripts this way will not reveal any KoL-related logical errors.