Difference between pages "Miscellaneous ASH Features" and "ASH Errors"

From Kolmafia
(Difference between pages)
Jump to navigation Jump to search
imported>Bale
 
imported>Bale
m
 
Line 1: Line 1:
 
{{TOCright}}
 
{{TOCright}}
==Special Features of ASH Scripting==
+
Errors happen. When they do, it helps to have some idea of what what wrong.
These features are not exactly ASH commands, but they are used to improve script usage.
 
  
 +
===Abort===
  
===Special Syntax Functions===
+
Whenever a script runs the [[abort|abort()]] function, this error is generated.
  
'''[[notify]]'''
+
{{CodeSample|
 +
description=The zero-parameter form:|
 +
code=
 +
<syntaxhighlight>
 +
abort();
 +
</syntaxhighlight>}}
 +
will stop the execution and print: <span style="color:red">KoLmafia declares world peace.</span> Note that this message may also appear as a result of pressing Esc in the Main Interface or hitting "stop now" in the Adventure tab.
  
Used to send a simple kmail to the script's author, letting them know you use their script.
+
{{CodeSample|
 +
description=The one-parameter form:|
 +
code=
 +
<syntaxhighlight>
 +
abort("Aborting script...");
 +
</syntaxhighlight>}}
 +
will stop the execution and print: <span style="color:red">Aborting script...</span>
  
'''[[import]]'''
+
===Cannot return [datatype] from [datatype] function===
  
Used to load an external script into your current one.
+
This indicates that the return value does not match the function's type.
  
'''[[call]]'''
+
{{CodeSample|
 +
description=Example:|
 +
code=
 +
<syntaxhighlight>
 +
int stuff(){
 +
  return "4";
 +
}
 +
</syntaxhighlight>}}
 +
will cause this error: <span style="color:red">Cannot return string value from int function (test.ash, line 2)</span>
  
Enables the script writer to invoke a function whose name is known at runtime.
+
{{CodeSample|
 +
description=Similarly, for void functions:|
 +
code=
 +
<syntaxhighlight>
 +
void stuff(){
 +
  return 4;
 +
}
 +
</syntaxhighlight>}}
 +
will cause this error: <span style="color:red">Cannot return a value from a void function (test.ash, line 2)</span>
  
'''[[new]]'''
+
===Encountered '[break|continue]' outside loop===
  
Constructor function used to populate a record.
+
This indicates that the control structure in question was not in a loop.
  
 +
{{CodeSample|
 +
description=Example:|
 +
code=
 +
<syntaxhighlight>
 +
continue;
 +
</syntaxhighlight>}}
 +
will cause this error: <span style="color:red">Encountered 'break' outside of loop (test.ash, line 1)</span>
  
===Comments===
+
===Expected===
  
Adding '''[[Comments]]''' to your code can make it much easier to maintain, and helps other users understand what's going on.
+
This generally indicates a syntax problem (missing ending semi-colon, unmatched braces, unmatched parenthesis etc...).
  
 +
{{CodeSample|
 +
description=Example:|
 +
code=
 +
<syntaxhighlight>
 +
int a = 1
 +
print(a);
 +
</syntaxhighlight>}}
 +
will cause this error: <span style="color:red">Expected ;, found print (test.ash, line 2)</span>
  
==Errors==
+
==="if" requires a boolean conditional expression ===
  
There are a wide variety of error messages. For help when things go wrong, please see the page on [[ASH Errors]].
+
This indicates that the condition inside an if statement is not a boolean.
  
 +
{{CodeSample|
 +
description=Example:|
 +
code=
 +
<syntaxhighlight>
 +
if(42) {
 +
  print("Don't panic.");
 +
}
 +
</syntaxhighlight>}}
 +
will cause this error: <span style="color:red">"if" requires a boolean conditional expression (test.ash, line 1)</span>
  
==Relay Override Scripts==
+
===Index type is not a primitive type===
A relay override script is a script that modifies a web page in the relay browser. It is only in the relay browser that the effects of this scripts can be seen. The purpose of this is to alter a KoL page to improve functionality or appearance.
 
  
Detailed information is at [[Relay_Override_Scripting#Relay_Script|Relay Override Script]].
+
This indicates that the map's index is not a standard datatype (float, int, string, etc.).
  
 +
{{CodeSample|
 +
description=Example:|
 +
code=
 +
<syntaxhighlight>
 +
record alpha{
 +
  int a;
 +
  int b;
 +
};
 +
int[alpha] map;
 +
</syntaxhighlight>}}
 +
will cause this error: <span style="color:red">Index type 'alpha' is not a primitive type (test.ash, line 5)</span>
  
==User Interface Script==
+
===Invalid field name===
A User Interface script is a script that creates a web page viewable in the relay browser. It is only in the relay browser that this special page can be seen. The purpose of this is to extend KoLmafia abilities or present information to the user.
 
  
Detailed information is at [[Relay_Override_Scripting#User_Interface_Script|User Interface Script]].
+
This indicates that the record does not contain the field in question.
  
 +
{{CodeSample|
 +
description=Example:|
 +
code=
 +
<syntaxhighlight>
 +
record my_record {
 +
  string a;
 +
  string b;
 +
};
  
==Consult Scripts==
+
my_record [int] my_map;
Consult scripts are used to script combat. They can be used in automated adventuring or in the relay browser.
+
my_map[1].c = "hello";
 +
</syntaxhighlight>}}
 +
will cause this error: <span style="color:red">Invalid field name 'c' (test.ash, line 7)</span>
  
Detailed information is at [[Custom_Combat_Script#Consult_Scripts|Consult Scripts]].
+
Note that this error may also be encountered when neglecting to name a field.
 +
{{CodeSample|
 +
description= For instance:|
 +
code=
 +
<syntaxhighlight>
 +
record my_record {
 +
  string;
 +
};
 +
</syntaxhighlight>}}
 +
will cause this error: <span style="color:red">Invalid field name ';' (test.ash, line 2)</span>
  
 +
===Invalid type name===
  
==Additional Script Uses==
+
This indicates that the specified type is not recognized by Mafia.
  
In addition to regular scripts, override scripts, UI scripts, and consult scripts, mafia has a few other hooks for adding event-driven scripts. These situations are listed below, along with the preference that you can set a script name to (in parentheses).
+
{{CodeSample|
 +
description=Example:|
 +
code=
 +
<syntaxhighlight>
 +
int[fruit] map;
 +
</syntaxhighlight>}}
 +
will cause this error: <span style="color:red">Invalid type name 'fruit' (test.ash, line 1)</span>
  
==== After Adventure (afterAdventureScript) ====
+
===Main method must appear at top level===
  
*Executed just after mafia has finished an adventure.
+
This indicates that the script's main method is inside another method, for some reason. This is usually caused by a missing brace.
*The afterAdventureScript setting is executed like a CLI command. If it is the name of an ASH script, that script does not require any special main() declaration.
 
*By default this only works for automated adventuring. If you wish it to fire when you're using the relay browser then turn it on in General -> Preferences -> - Relay Browser or set the property relayRunsAfterAdventureScript to true.
 
  
 +
{{CodeSample|
 +
description=Example:|
 +
code=
 +
<syntaxhighlight>
 +
void stuff() {
 +
  // stuff happens here
  
==== Between Battle (betweenBattleScript) ====
+
void main() {
 +
  stuff();
 +
}
 +
</syntaxhighlight>}}
 +
will cause this error: <span style="color:red">main method must appear at top level (test.ash, line 6)</span>
  
*Executed whenever mafia is about to enter a combat. That means before every adventure if auto-adventuring, or before using an item that could lead to combat such as black pudding or drum machine.
+
===Missing return value===
*The betweenBattleScript setting is executed like a CLI command. If it is the name of an ASH script, that script does not require any special main() declaration.
 
*By default this only works for automated adventuring. If you wish it to fire when you're using the relay browser then turn it on in General -> Preferences -> - Relay Browser or set the property relayRunsBeforeBattleScript to true.
 
*Example: Zarqon's [http://kolmafia.us/showthread.php?t=1240 Best Between Battle]
 
  
 +
The last line of a user-defined function has to be "return <value>;" (although the [[Control_Structures#return|return]] command can be used before too).
  
=== Breaking the Prism (kingLiberatedScript) ===
+
{{CodeSample|
 +
description=Example:|
 +
code=
 +
<syntaxhighlight>
 +
int my_function( int a, int b ) {
 +
    if ( a > b ) return a ;
 +
    else return b ;
 +
}
 +
</syntaxhighlight>}}
 +
will cause this error: <span style="color:red">Missing return value (test.ash, line 4)</span>
  
: This will be executed after you break the Prism at the top of the Naughty Sorceress' Lair. This trigger is designed so that a character can be automatically prepared for aftercore. The kingLiberatedScript setting is executed like a CLI command. If it is the name of an ASH script, that script does not require any special main() declaration.
 
  
 +
===No closing found===
  
=== Buy (buyScript) ===
+
This can be caused by forgetting to put a '''"''' or a ''']''' at the end of an argument
  
*Will execute whenever mafia needs to determine if it is to purchase or create an item.
+
{{CodeSample|
*Requires a special main declaration boolean main(item itm, int qty, int ingredientLevel, boolean defaultBuy). These values will be supplied by mafia when the script is automatically called. A return value of true will instruct mafia to purchase the item and false will cause it to create the item possibly leading to other buy/create decisions.
+
description=Example:|
**''itm'' and ''qty'' specify the item under consideration.
+
code=
**''ingredientLevel'' indicates what's already available:  
+
<syntaxhighlight>
***0 = none of the ingredients
+
buy(1000 , $item[disco ball, 225);
***1 = some of them
+
</syntaxhighlight>}}
***2 = enough to create at least one of the requested item
+
will cause this error: <span style="color:red">No closing ] found (testing.ash, line 1)</span>
**defaultBuy indicates what KoLmafia would have done otherwise; returning this value is the safest thing your script could do. It will normally be true if ingredientLevel is 0, false if 2. The default at level 1 depends on the item, and is subject to change - normally, anything made with star charts, pixels, the Malus, or multi-use are bought, anything else is created.
 
*Example: Bale's [http://kolmafia.us/showthread.php?p=18313#post18313 potionBuy].
 
  
 +
===Record expected===
  
=== Chatbot (chatBotScript) ===
+
This indicates that an unrecognized record was found. This often appears when you forget to append "()" to a function name.
  
*Will execute whenever a private message is received
+
{{CodeSample|
*Requires a special main declaration which can have an optional third parameter:  
+
description=Example:|
*:void main(string sender, string message)
+
code=
*:void main(string sender, string message, string channel)
+
<syntaxhighlight>
**'sender' is the name of the player who sent the message
+
print(4.to_string);
**'message' is the message that was sent
+
</syntaxhighlight>}}
**'channel' is "/clan" for clan messages and the empty string for private messages
+
will cause this error: <span style="color:red">Record expected (test.ash, line 2)</span>
  
=== Counters (counterScript) ===
+
===Record name is already defined===
  
*Will execute whenever a counter will expire as a result of an intended use of adventures.
+
This indicates that the record name has already been defined, and so cannot be reused. The solution is to change the record name.
*Requires a special main declaration: boolean main(string name, int remain).  These values will be supplied by mafia when the script is automatically called and the return value will determine whether mafia continues operation (true) or aborts (false). 
 
**''name'' will be the name of the counter that is about to expire.
 
**''remain'' is the number of turns remaining before the counter expires.  Remain will usually be 0, but may be higher if you are about to multi-create items, adventure underwater, or take a vacation.  It may be negative if the counter was informational (one that doesn't abort adventuring), and it actually expired in the middle of an action that used multiple turns at once.
 
*Example: Bale's [http://kolmafia.us/showthread.php?t=2519 CounterChecker].
 
  
 +
{{CodeSample|
 +
description=Example:|
 +
code=
 +
<syntaxhighlight>
 +
record str {
 +
  string a;
 +
  string b;
 +
};
 +
record str {
 +
  int a;
 +
  int b;
 +
};
 +
</syntaxhighlight>}}
 +
will cause this error: <span style="color:red">Record name 'str' is already defined (test.ash, line 5)</span>
  
=== Login (loginScript) ===
+
===Return needs [datatype] value===
  
:This script is immediately executed once your character is logged in. The loginScript setting is executed like a CLI command. If it is the name of an ASH script, that script does not require any special main() declaration.
+
This indicates that the return value is void when the function is not.
  
 +
{{CodeSample|
 +
description=Example:|
 +
code=
 +
<syntaxhighlight>
 +
int stuff() {
 +
  return;
 +
}
 +
</syntaxhighlight>}}
 +
will cause this error: <span style="color:red">Return needs int value (test.ash, line 2)</span>
  
=== Logout (logoutScript) ===
+
===Reserved word cannot be a [function|record|variable] name===
  
:Same as the loginScript, but runs on logout. Note that an {{f|abort}} in the script won't stop a logout unless the logout was called in (specific ways, please list).
+
This indicates that the desired name has been [[Reserved Words|reserved]], and so cannot be used. The solution is to use a different name.
  
=== Planting (plantingScript) ===
+
{{CodeSample|
 +
description=An invalid name:|
 +
code=
 +
<syntaxhighlight>
 +
int float(float a) {
 +
  return round(a);
 +
}
 +
</syntaxhighlight>}}
 +
will cause this error: <span style="color:red">Reserved word 'float' cannot be used as a function name (test.ash, line 1)</span>
  
*This script can be used to manage the mushroom fields, which are available when under a muscle zodiac sign. It runs after logging in, if you have a mushroom field. Main function takes no arguments. Generally should take into account current [[moon phase]] so that it can be started in the middle of a cycle.
+
An interesting point to note is that ASH functions take precedent over identically named custom functions rather than throw an exception.
*The plantingScript has to be the name of an ASH file without the extension (if the plantingScript setting is "plant", KoLMafia will call "plant.ash").
 
*Example: [http://kolmafia.us/showthread.php?563-Modified-mushroom-script-to-produce-3rd-4th-gen-crop&p=7507&viewfull=1#post7507 Sandiman's mushroom planting script].
 
  
 +
{{CodeSample|
 +
description=An invalid record name:|
 +
code=
 +
<syntaxhighlight>
 +
record string {
 +
  string a;
 +
  string b;
 +
};
 +
</syntaxhighlight>}}
 +
will cause this error: <span style="color:red">Reserved word 'string' cannot be a record name (test.ash, line 1)</span>
  
=== Post-Ascension (postAscensionScript) ===
+
{{CodeSample|
 +
description=An invalid variable name:|
 +
code=
 +
<syntaxhighlight>
 +
string item = "bottle of gin";
 +
</syntaxhighlight>}}
 +
will cause this error: <span style="color:red">Reserved word 'item' cannot be a variable name (test.ash, line 1)</span>
  
:Executed at once when your character starts a new ascension. (Example: Bales's [http://kolmafia.us/showthread.php?t=2769 newLife]). The postAscensionScript setting is executed like a CLI command. If it is the name of an ASH script, that script does not require any special main() declaration.
+
===Script parsing error===
  
 +
This generally indicates a syntax problem, as in having an excess amount of braces or a variable/function/record name that starts with an invalid character.
  
=== Pre-Ascension (preAscensionScript) ===
+
{{CodeSample|
 +
description=Example:|
 +
code=
 +
<syntaxhighlight>
 +
put_shop(0 ,0 ,$item[wolf mask]);
 +
put_shop(0 ,0 ,$item[rave whistle]);
 +
put_shop(0 ,0 ,$item[giant needle]);
 +
cli_execute ("undercut");
 +
}
 +
</syntaxhighlight>}}
 +
will cause this error: <span style="color:red">Script parsing error (test.ash, line 5)</span>
  
:Same as the Post-Ascension, but executed right before entering Valhalla.
+
===Variable is already defined===
  
 +
This indicates that the same variable has been declared twice.
  
=== Recovery (recoveryScript) ===
+
{{CodeSample|
 +
description=Example:|
 +
code=
 +
<syntaxhighlight>
 +
int Drops ;
 +
int Meat ;
 +
int Drops = numeric_modifier("Item Drop");
 +
</syntaxhighlight>}}
 +
will cause this error: <span style="color:red">Variable Drops is already defined (test.ash, line 3)</span>
  
*Executed whenever mafia would recover your HP / MP. 
 
*Requires a special main() declaration: boolean main(string type, int amount).  These values will be supplied when mafia automatically calls the script, and the return value will instruct mafia if it should attempt to use mafia's default healing after the script concludes.
 
**''type'' is either "HP" or "MP".
 
**''amount'' is the desired amount of restoration needed or 0 to use mafia's default recovery level and target.
 
*Example: Bale's [http://kolmafia.us/showthread.php?t=1780 Universal Recovery]
 
  
==A little CLI help==
+
{{RFI|Obviously, a lot more info is needed here.}}
  
Three CLI commands are of great use to ASH scripters:
+
[[Category:ASH Scripting]]
* [[ashref]] allows one to get a list of ASH functions
 
* [[ash (CLI)|ash]] or [[ash (CLI)|ashq]] allows one to run ASH directly on the CLI.
 
* [[ashwiki]] allows you to search this wiki by launching a web browser.
 
 
 
 
 
==Useful forum threads==
 
[http://kolmafia.us/showthread.php?523-Did-you-know...-It-s-a-secret-feature.&p=2968&viewfull=1#post2968 This thread], started by holatuwol, is a tidy compilation of various undocumented features in KoLmafia.
 
 
 
[[Category:Scripting]]
 

Revision as of 07:13, 23 May 2010

Errors happen. When they do, it helps to have some idea of what what wrong.

Abort

Whenever a script runs the abort() function, this error is generated.

The zero-parameter form:

abort();

will stop the execution and print: KoLmafia declares world peace. Note that this message may also appear as a result of pressing Esc in the Main Interface or hitting "stop now" in the Adventure tab.

The one-parameter form:

abort("Aborting script...");

will stop the execution and print: Aborting script...

Cannot return [datatype] from [datatype] function

This indicates that the return value does not match the function's type.

Example:

int stuff(){
   return "4";
}

will cause this error: Cannot return string value from int function (test.ash, line 2)

Similarly, for void functions:

void stuff(){
   return 4;
}

will cause this error: Cannot return a value from a void function (test.ash, line 2)

Encountered '[break|continue]' outside loop

This indicates that the control structure in question was not in a loop.

Example:

continue;

will cause this error: Encountered 'break' outside of loop (test.ash, line 1)

Expected

This generally indicates a syntax problem (missing ending semi-colon, unmatched braces, unmatched parenthesis etc...).

Example:

int a = 1
print(a);

will cause this error: Expected ;, found print (test.ash, line 2)

"if" requires a boolean conditional expression

This indicates that the condition inside an if statement is not a boolean.

Example:

if(42) {
   print("Don't panic.");
}

will cause this error: "if" requires a boolean conditional expression (test.ash, line 1)

Index type is not a primitive type

This indicates that the map's index is not a standard datatype (float, int, string, etc.).

Example:

record alpha{
   int a;
   int b;
};
int[alpha] map;

will cause this error: Index type 'alpha' is not a primitive type (test.ash, line 5)

Invalid field name

This indicates that the record does not contain the field in question.

Example:

record my_record {
   string a;
   string b;
};

my_record [int] my_map;
my_map[1].c = "hello";

will cause this error: Invalid field name 'c' (test.ash, line 7)

Note that this error may also be encountered when neglecting to name a field.

For instance:

record my_record {
   string;
};

will cause this error: Invalid field name ';' (test.ash, line 2)

Invalid type name

This indicates that the specified type is not recognized by Mafia.

Example:

int[fruit] map;

will cause this error: Invalid type name 'fruit' (test.ash, line 1)

Main method must appear at top level

This indicates that the script's main method is inside another method, for some reason. This is usually caused by a missing brace.

Example:

void stuff() {
   // stuff happens here

void main() {
   stuff();
}

will cause this error: main method must appear at top level (test.ash, line 6)

Missing return value

The last line of a user-defined function has to be "return <value>;" (although the return command can be used before too).

Example:

int my_function( int a, int b ) {	
    if ( a > b ) return a ;
    else return b ;
}

will cause this error: Missing return value (test.ash, line 4)


No closing found

This can be caused by forgetting to put a " or a ] at the end of an argument

Example:

buy(1000 , $item[disco ball, 225);

will cause this error: No closing ] found (testing.ash, line 1)

Record expected

This indicates that an unrecognized record was found. This often appears when you forget to append "()" to a function name.

Example:

print(4.to_string);

will cause this error: Record expected (test.ash, line 2)

Record name is already defined

This indicates that the record name has already been defined, and so cannot be reused. The solution is to change the record name.

Example:

record str {
   string a;
   string b;
};
record str {
   int a;
   int b;
};

will cause this error: Record name 'str' is already defined (test.ash, line 5)

Return needs [datatype] value

This indicates that the return value is void when the function is not.

Example:

int stuff() {
   return;
}

will cause this error: Return needs int value (test.ash, line 2)

Reserved word cannot be a [function|record|variable] name

This indicates that the desired name has been reserved, and so cannot be used. The solution is to use a different name.

An invalid name:

int float(float a) {
   return round(a);
}

will cause this error: Reserved word 'float' cannot be used as a function name (test.ash, line 1)

An interesting point to note is that ASH functions take precedent over identically named custom functions rather than throw an exception.

An invalid record name:

record string {
   string a;
   string b;
};

will cause this error: Reserved word 'string' cannot be a record name (test.ash, line 1)

An invalid variable name:

string item = "bottle of gin";

will cause this error: Reserved word 'item' cannot be a variable name (test.ash, line 1)

Script parsing error

This generally indicates a syntax problem, as in having an excess amount of braces or a variable/function/record name that starts with an invalid character.

Example:

put_shop(0 ,0 ,$item[wolf mask]);
put_shop(0 ,0 ,$item[rave whistle]);
put_shop(0 ,0 ,$item[giant needle]);
cli_execute ("undercut");
}

will cause this error: Script parsing error (test.ash, line 5)

Variable is already defined

This indicates that the same variable has been declared twice.

Example:

int Drops ;
int Meat ;
int Drops = numeric_modifier("Item Drop");

will cause this error: Variable Drops is already defined (test.ash, line 3)


Attention KoLmafia Experts!

We need your help; some details of this function's operation are unknown or unclear.

The following specific question has been raised:

  • Obviously, a lot more info is needed here.