Tips, Tricks and Workarounds

From Kolmafia
Revision as of 07:02, 1 October 2010 by imported>PhilmASTErpLus (Added 1-liner page intro and Offline Testing seciton)
Jump to navigation Jump to search

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

By default, whenever an ASH function returns false, everything will abort unless you somehow capture the return value (i.e. assigning it to a variable or enclosing it in a control structure). So, if you want to make use of, say, the hermit() function in a way that won't abort if the function returns false, you could:

Save the result as a boolean variable.

boolean fakeBool = hermit(1, $item[Ten-leaf clover]);

Capture the result in an if() conditional.

if(!hermit(1, $item[Ten-leaf clover])) print("Hermit looting failed: continuing on.");

An if() conditional doesn't even have to do anything.

if(hermit(1, $item[Ten-leaf clover])) {}

The most minimalistic method is to capture it with a not operator and parenthesis.

(!hermit(1, $item[Ten-leaf clover]));

Any of these will capture the return value and prevent mafia from aborting.

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 it. If you want to check only the syntax of the script, you can run 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, and running scripts will properly validate them. 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.