Difference between pages "ASH For Beginners" and "Talk:Adventure"

From Kolmafia
(Difference between pages)
Jump to navigation Jump to search
imported>Heeheehee
m (→‎Functions: Scope also applies to functions. Built-in functions have priority over user-defined functions. (fun fact: you can cause an edit conflict for yourself!))
 
imported>Heeheehee
m
 
Line 1: Line 1:
{{TOCright}}
+
jasonharper said we can supply a macro as the third parameter; any ideas on usage? KoL macros require line breaks in a picky manner, but setting my string to have "\n" doesn't work... or am I sup'd to use a macro's name? --[[User:StDoodle|StDoodle (#1059825)]] 21:21, 29 April 2010 (UTC)
==File Editing==
 
To create and edit ASH scripts, you need a text editing program that doesn't automatically add line-breaks for word wrapping.
 
  
===Mac===
+
:I think he means to use the macro's name. Try it. --[[User:Bale|Bale]] 21:23, 29 April 2010 (UTC)
While the built in editor TextEdit will work fine (as long as you remember to write only in plaintext), [http://www.barebones.com/products/textwrangler/ TextWrangler] is a much more powerful tool. It may not be designed for ASH scripting specifically, but it was written for a similar purpose.
 
  
===Linux===
+
Wow!! The wiki has come a long ways since I was last on here in December. It's beautiful! I don't know if this is the right place for this, but I think it would be helpful to have a list of special events that cause adventure to stop and return false besides conditions being satisfied. Demon name and hobo glyph are already listed, but I think there are a lot more. I don't know that many, but looking over Zarqon's MacGuffin script I saw several. I just tested out the Sietch in Time adventure. adventure(my_adventures(), $location[desert (ultrahydrated)]) stopped adventuring and returned false when I received the Sietch in Time adventure. What do you guys think? -- That FN Ninja 21:39, 26 June 2010 [attribution added]
If you use Linux, you probably already know about the options available for text editing. Emacs, Vi, etc. are all fine for creating ASH scripts.
 
  
===Windows===
+
It already lists auto-stops as a reason to return false. I'll add a link to the page of auto-stops for people to find the list of them. --[[User:Bale|Bale]] 22:09, 26 June 2010 (UTC)
Windows users beware; using Microsoft Word to create ASH files will lead to no end of headaches! The built-in program notepad will work, as can wordpad when configured with the option "No Wrap" for text. However, having a program that can handle syntax-highlighting can be very beneficial. Many scripters recommend [http://notepad-plus.sourceforge.net/uk/site.htm Notepad++] for ASH (and other programmatic) scripting. In this case, it's also beneficial to have a style configuration that works well with ASH, and one example can be found [http://kolmafia.us/showthread.php?3164-How-do-you-write-an-ASH-file&p=21567&viewfull=1#post21567 here].
 
  
==Saving==
 
ASH scripts need to be saved with the file extension ".ash" to be properly recognized by KoLmafia. If you're using a built-in text editor on a Windows machine, it may automatically save your files with the ".txt" extension by default. (So you may end up with a file called "script.ash.txt" instead of the desired "script.ash".) One way around this on Windows that usually works is to put quotes around the file name when you save it, including the extension.
 
  
Most advanced text editors (such as Notepad++) will save files as specified, without the need for the quotes-trick.
+
===Combat filter function must be top-level code?===
 +
I tried just tossing a filter function into some other script's main function (also within a for loop, but that's probably irrelevant), and when I ran the script, it started printing debug logs like crazy. Moving the function to top-level seems to have fixed it.  
  
Most files should be saved in the "scripts" directory, which is located by default at the same directory level as the KoLmafia .jar or .exe file on Windows. You can also create a sub-directory under "scripts," and it will be listed in KoLmafia's Scripts Menu.
+
If this is, indeed, the case, we should probably add a note to that effect on this page. --[[User:Heeheehee|Heeheehee]] 23:05, 10 July 2010 (UTC)
  
Relay browser scripts are the exception to the rule: they must be saved in the "relay" directory. Relay browser overrides need to be saved in the top level of "relay," and must be named the same as the page they will override, except with an ".ash" extension. For example, to override "charpane.php" your script would need to be named "charpane.ash". For user-interface scripts, the script name needs to start with "relay_" and end with ".ash". These scripts must also be saved in the top-level of the "relay" directory.
+
More likely, something needs to be fixed. You might want to post that on the forums. --[[User:StDoodle|StDoodle (#1059825)]] 05:04, 11 July 2010 (UTC)
 +
: Okay, JH clarified that this is, indeed, the case and isn't a bug. Will add to this page. --[[User:Heeheehee|Heeheehee]] 23:36, 11 July 2010 (UTC)
  
==Programming==
+
===Combat Filter info===
You should first read through the various pages listed under "LANGUAGE CONVENTIONS" on the [[Main Page]]. One should also be aware that, with the exception of [[Control Structures]], all ASH commands need to end with a semi-colon.
 
  
===Variables===
+
I propose that we have a separate section to describe combat filter functions; there's still a little bit more that needs to be said, but it's starting to "take over" this function page. Perhaps a section under [[Custom Combat Script]] would be in order (with a link from this page, of course)?
Variables are used to hold information that you need to access multiple times. They must be declared before they can be used, by specifying the [[Data Types|Data Type]] that they will hold, followed by the variable name.
 
{{CodeSample|description=For example:|code=<syntaxhighlight>int count;</syntaxhighlight>}}
 
would declare a variable named "count" of an integer type. You can also set the initial value of a variable on the same line it is declared.
 
{{CodeSample|description=On the other hand:|code=<syntaxhighlight>int count = 6;</syntaxhighlight>}}
 
would declare the variable "count" with an initial value of 6.
 
  
Notice that you would not want to have both of the above lines in your script! Variables operate under what's referred to as their scope. In a nutshell, scope defines where a variable is accessible. Scope "cascades" down, similar to stylesheets for those familiar with web programming. A variable declared at the top-level of a script (a "global" variable), outside of any functions, will be accessible anywhere in your script, or in any other scripts that import it. A variable declared inside of a function will be accessible only inside that function (including inside of control structures), but is considered to not exist by other functions. Since ASH uses curly brace scope, variables within a set of curly braces ('''{ }''') will not be available outside that same set of braces.
+
Note: I'd do it myself, but I'm unsure exactly how their return values need to be formatted. Also, they can return "regular" CCS or macro language, right? Or no? --[[User:StDoodle|StDoodle (#1059825)]] 05:38, 12 July 2010 (UTC)
 
+
: Nope, it returns just a CCS language -- that's why "void" got converted into "item ovoid leather thing, ovoid leather thing". Also why ''skill [number]'' didn't work. But yeah, adding it to [[Custom Combat Script|CCS]] would probably keep this page less cluttered. Obviously I don't know enough about them, or else I wouldn't be asking for troubleshooting. --[[User:Heeheehee|Heeheehee]] 10:58, 12 July 2010 (UTC)
Declaring a variable multiple times will generate an [[ASH Errors#Variable is already defined|error]]. Note that this only applies to variable with overlapping scope; if one function uses a variable named "bob" that is declared inside of the function, another function can declare another variable with the same name, even as a different datatype. However, the error will still occur if you have a globally-defined variable, and then declare a variable with the same name inside of a function (as their scopes overlap).
 
 
 
===Functions===
 
Functions are groupings of operations that can be applied multiple times by referencing the function name. Basically, they avoid having to re-type the same code over and over. Many functions are built-in to KoLmafia, and examples can be found all over this wiki. It's also possible to write your own functions. To do so, you declare them similarly to a variable as above. However, a function can also have the datatype of "void," which is not allowed for variables. A "void" function is one that does not return any value; it simply does what it's programmed to do and the rest of your program moves on, regardless of the results (unless of course an [[ASH Errors|error]] occurs, including a call to [[abort|abort()]].)
 
{{CodeSample|description=For example, this simple function will double an integer:|code=<syntaxhighlight>
 
int double_it(int value) {
 
  return 2 * value;
 
}
 
</syntaxhighlight>}}
 
{{CodeSample|description=Furthermore, there are two distinct ways to call functions, user-defined or built-in:<br />Conventional,|code=<syntaxhighlight>
 
function(param1[,param2,param3,...]);
 
</syntaxhighlight>}}
 
{{CodeSample|description=and "Java style":|code=<syntaxhighlight>
 
param1.function([param2,param3,...]);
 
</syntaxhighlight>}}
 
The only difference between the two forms is aesthetics, as they are functionally equivalent.
 
{{CodeSample|description=Also somewhat important to note is that these commands can be chained together, and they will be executed from left-to-right. So, for instance,|code=<syntaxhighlight>
 
"polka pop".to_item().to_int()
 
</syntaxhighlight>}}
 
{{CodeSample|description=will return the item number for $item[Polka Pop], 4342. This is one instance where "Java style" is more aesthetically pleasing compared to the conventional|code=<syntaxhighlight>
 
to_int(to_item("polka pop"))
 
</syntaxhighlight>}}
 
 
 
Note that scope (discussed above, under [[ASH_For_Beginners#Variables|Variables]]) also applies to user-defined functions. Also, functions built into KoLmafia have priority over any user-defined functions with the same name and parameters.
 
 
 
===String Concatenation===
 
The basics to string concatenation: "Hello" + "world" yields "Helloworld". Likewise, variables can be strung together in this fashion. Unless the variables are both numbers (floats, booleans, or ints), they will be converted to strings, then concatenated.
 
Argh, me tired, finish later.
 
 
 
[[Category:Scripting]]
 

Latest revision as of 10:58, 12 July 2010

jasonharper said we can supply a macro as the third parameter; any ideas on usage? KoL macros require line breaks in a picky manner, but setting my string to have "\n" doesn't work... or am I sup'd to use a macro's name? --StDoodle (#1059825) 21:21, 29 April 2010 (UTC)

I think he means to use the macro's name. Try it. --Bale 21:23, 29 April 2010 (UTC)

Wow!! The wiki has come a long ways since I was last on here in December. It's beautiful! I don't know if this is the right place for this, but I think it would be helpful to have a list of special events that cause adventure to stop and return false besides conditions being satisfied. Demon name and hobo glyph are already listed, but I think there are a lot more. I don't know that many, but looking over Zarqon's MacGuffin script I saw several. I just tested out the Sietch in Time adventure. adventure(my_adventures(), $location[desert (ultrahydrated)]) stopped adventuring and returned false when I received the Sietch in Time adventure. What do you guys think? -- That FN Ninja 21:39, 26 June 2010 [attribution added]

It already lists auto-stops as a reason to return false. I'll add a link to the page of auto-stops for people to find the list of them. --Bale 22:09, 26 June 2010 (UTC)


Combat filter function must be top-level code?

I tried just tossing a filter function into some other script's main function (also within a for loop, but that's probably irrelevant), and when I ran the script, it started printing debug logs like crazy. Moving the function to top-level seems to have fixed it.

If this is, indeed, the case, we should probably add a note to that effect on this page. --Heeheehee 23:05, 10 July 2010 (UTC)

More likely, something needs to be fixed. You might want to post that on the forums. --StDoodle (#1059825) 05:04, 11 July 2010 (UTC)

Okay, JH clarified that this is, indeed, the case and isn't a bug. Will add to this page. --Heeheehee 23:36, 11 July 2010 (UTC)

Combat Filter info

I propose that we have a separate section to describe combat filter functions; there's still a little bit more that needs to be said, but it's starting to "take over" this function page. Perhaps a section under Custom Combat Script would be in order (with a link from this page, of course)?

Note: I'd do it myself, but I'm unsure exactly how their return values need to be formatted. Also, they can return "regular" CCS or macro language, right? Or no? --StDoodle (#1059825) 05:38, 12 July 2010 (UTC)

Nope, it returns just a CCS language -- that's why "void" got converted into "item ovoid leather thing, ovoid leather thing". Also why skill [number] didn't work. But yeah, adding it to CCS would probably keep this page less cluttered. Obviously I don't know enough about them, or else I wouldn't be asking for troubleshooting. --Heeheehee 10:58, 12 July 2010 (UTC)