Difference between pages "ASH Errors" and "IntelliJ Setup"

From Kolmafia
(Difference between pages)
Jump to navigation Jump to search
imported>Heeheehee
m (→‎Map modified within foreach: Elaborated on this bit.)
 
 
Line 1: Line 1:
{{TOCright}}
 
Errors happen. When they do, it helps to have some idea of what what wrong.
 
  
===Abort===
+
==== KoLmafia has migrated to the GitHub with gradle builds. ====
  
Whenever a script runs the [[abort|abort()]] function, this error is generated.
 
  
{{CodeSample|
+
[https://www.jetbrains.com/idea/ IntelliJ IDEA] is an IDE for developing Java applications. This guide assumes that you have a recent enough version of Java JDK, Ant, and IntelliJ IDEA. Completing [[Compiling from Source]] is a requirement for this guide, although experienced developers can skip to whatever step is appropriate.
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.
 
  
{{CodeSample|
+
The following instructions are based on IntelliJ IDEA 2021.2 Community Edition. Instructions for other versions/editions of IntelliJ may be slightly different. The latest version is recommended.
description=The one-parameter form:|
 
code=
 
<syntaxhighlight>
 
abort("Aborting script...");
 
</syntaxhighlight>}}
 
will stop the execution and print: <span style="color:red">Aborting script...</span>
 
  
===Cannot initialize parameter [parameter]===
+
== Create a Project ==
 +
=== Create a new project ===
 +
# Start IntelliJ IDEA. In the ''Welcome to IntelliJ IDEA'' dialog, click '''Get from VCS'''.<br>[[File:Intellij-setup-c2021.2-welcome.png|300px]]
 +
# In the ''Get from Version Control'' dialog, select the '''Repository URL''' tab.
 +
## In ''Version control'', select '''GitHub'''.<br>[[File:Intellij-setup-c2021.2-get-from-vcs.png|300px]]
 +
## Add a new repository location. To do so, click the '''+''' button next to ''Repositories''.<br>[[File:Intellij-setup-c2021.2-get-from-vcs-subversion.png|300px]]
 +
## In the ''New Repository Location'' popup, enter the URL to KoLmafia's GitHub repository. If you have a GitHub account and a Personal Access Token, use  git@github.com:kolmafia/kolmafia.git  If you do not have a GitHub account, use  https://github.com/kolmafia/kolmafia.git<br>[[File:Intellij-setup-c2021.2-new-repository-location.png|200px]]
 +
## Click '''OK''' to close the popup.
 +
## Once the repository has been added, '''click the repository URL to highlight it''', then click '''Check Out'''.<br>[[File:Intellij-setup-c2021.2-get-from-vcs-check-out.png|300px]]
 +
# If the ''Destination Directory'' popup appears, choose a directory to check out the working copy.<br>''Caution'': Don't choose a common directory like <samp>C:\Users\&lt;username&gt;\Documents</samp>. Instead, create a new directory under it (by clicking the folder icon) and give it an appropriate name (e.g. <samp>kolmafia</samp>).<br>[[File:Intellij-setup-c2021.2-choose-working-copy-dir.png|200px]]<br>Click '''OK''' to continue.
 +
# IntelliJ will generate a new project. If it asks you whether to add <code>kolmafia.iml</code> to Subversion, you may click '''Cancel''' to skip it
  
This error is created when an assignment is attempted inside a function declaration.
+
=== Add the Gradle Build File ===
The following code results in an error.
+
# Open the Gradle Toolbar Item (typically on the right side, and not very large)
 +
# Click the '''+''' button to add a build file
 +
# Choose build.gradle from the file selector and press Open
 +
#Run Configurations should contain <code>kolmafia [runShadow]</code>. If it does not, continue to the next steps.
 +
#In the Gradle Toolbar click on the elephant to Execute Gradle Task
 +
#A window titled ''Run Anything'' will popup. Search for <code>runShadow</code> and select it
  
{{CodeSample|
+
===How to build and run/debug===
code=
+
#Click the drop down in the top right and select the desired option
<syntaxhighlight>
+
##<code>kolmafia [runShadow]</code> is the application itself
/* test.ash */
+
##<code>Tests in 'KoLMafia'</code> are the unit tests
 
+
#Click the play button to build and run
void my_function( string myvar = "value" )
+
#Or click the bug button to build and debug
{
 
  /* Do something */
 
}
 
</syntaxhighlight>}}
 
will halt the script and print: <span style="color: red;">Cannot initialize parameter myvar (test.ash, line X)</span>
 
 
 
===Cannot return [datatype] from [datatype] function===
 
 
 
This indicates that the return value does not match the function's type.
 
 
 
{{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>
 
 
 
{{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>
 
 
 
===Encountered '[break|continue]' outside loop===
 
 
 
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 'continue' outside of loop (test.ash, line 1)</span>
 
 
 
===Expected===
 
 
 
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>
 
 
 
===Function defined multiple times===
 
 
 
This indicates that the function in question has already been defined.
 
 
 
{{CodeSample|
 
description=Example:|
 
code=
 
<syntaxhighlight>
 
int abs(int a){
 
    return a*((a>0).to_int()*2 - 1);
 
}
 
int abs(int a){
 
    if(a<0) a = -a;
 
    return a;
 
}
 
</syntaxhighlight>}}
 
will cause this error: <span style="color:red">Function <nowiki>'abs( int )'</nowiki> defined multiple times (test.ash, line 4)</span>
 
 
 
==="if" requires a boolean conditional expression ===
 
 
 
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>
 
 
 
===Index type is not a primitive type===
 
 
 
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>
 
 
 
===Invalid field name===
 
 
 
This indicates that the record does not contain the field in question.
 
 
 
{{CodeSample|
 
description=Example:|
 
code=
 
<syntaxhighlight>
 
record my_record {
 
  string a;
 
  string b;
 
};
 
 
 
my_record [int] my_map;
 
my_map[1].c = "hello";
 
</syntaxhighlight>}}
 
will cause this error: <span style="color:red">Invalid field name 'c' (test.ash, line 7)</span>
 
 
 
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===
 
 
 
This indicates that the specified type is not recognized by Mafia.
 
 
 
{{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>
 
 
 
===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.
 
 
 
{{CodeSample|
 
description=Example:|
 
code=
 
<syntaxhighlight>
 
void stuff() {
 
  // stuff happens here
 
 
 
void main() {
 
  stuff();
 
}
 
</syntaxhighlight>}}
 
will cause this error: <span style="color:red">main method must appear at top level (test.ash, line 6)</span>
 
 
 
===Map modified within foreach===
 
 
 
You cannot add or remove new entries into a map within a <code>[[foreach]]</code> loop, with the exception of removing the current key. This can happen if you accidentally wrap the name of a string variable with double-quotes for referencing a map entry.
 
 
 
{{CodeSample|
 
description=For example, the following code:|
 
code=
 
<syntaxhighlight>
 
/* test.ash */
 
item [string] my_map;
 
/* Populate my_map with several entries */
 
foreach str in my_map
 
  print( str + " => " + my_map[ "str" ] );
 
</syntaxhighlight>}}
 
Yields: <span style="color: red;">Map modified within foreach (test.ash, line X)</span>
 
 
 
===Missing return value===
 
 
 
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).
 
 
 
{{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>
 
 
 
 
 
===No closing found===
 
 
 
This can be caused by forgetting to put a '''"''' or a ''']''' at the end of an argument
 
 
 
{{CodeSample|
 
description=Example:|
 
code=
 
<syntaxhighlight>
 
buy(1000 , $item[disco ball, 225);
 
</syntaxhighlight>}}
 
will cause this error: <span style="color:red">No closing ] found (testing.ash, line 1)</span>
 
 
 
===Record expected===
 
 
 
This indicates that an unrecognized record was found. This often appears when you forget to append "()" to a function name.
 
 
 
{{CodeSample|
 
description=Example:|
 
code=
 
<syntaxhighlight>
 
print(4.to_string);
 
</syntaxhighlight>}}
 
will cause this error: <span style="color:red">Record expected (test.ash, line 2)</span>
 
 
 
===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.
 
 
 
{{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>
 
 
 
===Return needs [datatype] value===
 
 
 
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>
 
 
 
===Reserved word cannot be a [function|record|variable] name===
 
 
 
This indicates that the desired name has been [[Reserved Words|reserved]], and so cannot be used. The solution is to use a different name.
 
 
 
{{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>
 
 
 
An interesting point to note is that ASH functions take precedent over identically named custom functions rather than throw an exception.
 
 
 
{{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>
 
 
 
{{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>
 
 
 
===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.
 
 
 
{{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>
 
 
 
===Variable is already defined===
 
 
 
This indicates that the same variable has been declared twice.
 
 
 
{{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>
 
 
 
 
 
{{RFI|Obviously, a lot more info is needed here.}}
 
 
 
[[Category:Scripting]]
 

Latest revision as of 01:09, 5 December 2023

KoLmafia has migrated to the GitHub with gradle builds.

IntelliJ IDEA is an IDE for developing Java applications. This guide assumes that you have a recent enough version of Java JDK, Ant, and IntelliJ IDEA. Completing Compiling from Source is a requirement for this guide, although experienced developers can skip to whatever step is appropriate.

The following instructions are based on IntelliJ IDEA 2021.2 Community Edition. Instructions for other versions/editions of IntelliJ may be slightly different. The latest version is recommended.

Create a Project

Create a new project

  1. Start IntelliJ IDEA. In the Welcome to IntelliJ IDEA dialog, click Get from VCS.
    Intellij-setup-c2021.2-welcome.png
  2. In the Get from Version Control dialog, select the Repository URL tab.
    1. In Version control, select GitHub.
      Intellij-setup-c2021.2-get-from-vcs.png
    2. Add a new repository location. To do so, click the + button next to Repositories.
      Intellij-setup-c2021.2-get-from-vcs-subversion.png
    3. In the New Repository Location popup, enter the URL to KoLmafia's GitHub repository. If you have a GitHub account and a Personal Access Token, use git@github.com:kolmafia/kolmafia.git If you do not have a GitHub account, use https://github.com/kolmafia/kolmafia.git
      Intellij-setup-c2021.2-new-repository-location.png
    4. Click OK to close the popup.
    5. Once the repository has been added, click the repository URL to highlight it, then click Check Out.
      Intellij-setup-c2021.2-get-from-vcs-check-out.png
  3. If the Destination Directory popup appears, choose a directory to check out the working copy.
    Caution: Don't choose a common directory like C:\Users\<username>\Documents. Instead, create a new directory under it (by clicking the folder icon) and give it an appropriate name (e.g. kolmafia).
    Intellij-setup-c2021.2-choose-working-copy-dir.png
    Click OK to continue.
  4. IntelliJ will generate a new project. If it asks you whether to add kolmafia.iml to Subversion, you may click Cancel to skip it

Add the Gradle Build File

  1. Open the Gradle Toolbar Item (typically on the right side, and not very large)
  2. Click the + button to add a build file
  3. Choose build.gradle from the file selector and press Open
  4. Run Configurations should contain kolmafia [runShadow]. If it does not, continue to the next steps.
  5. In the Gradle Toolbar click on the elephant to Execute Gradle Task
  6. A window titled Run Anything will popup. Search for runShadow and select it

How to build and run/debug

  1. Click the drop down in the top right and select the desired option
    1. kolmafia [runShadow] is the application itself
    2. Tests in 'KoLMafia' are the unit tests
  2. Click the play button to build and run
  3. Or click the bug button to build and debug