KoLmafia Guide: An Introduction to Scripting
Scripting is great! It allows you to accomplish many repetitive tasks that you will certainly appreciate if you are farming or doing other repetitive actions for very long.
For this portion of the guide, I am going to walk through a series of scripts that I made. These are relatively simple scripts with limited logic and fairly well commented code. I will explain why and how I did various portions of the script. The idea is to get you to understand each script well enough so that you can use cut&paste to piece together your own scripts. In my opinion, this is the easiest way to write your own scripts; steal pieces from other people's scripts! It really is not all that difficult to steal code snippets from others and make simple scripts that can simplify your life significantly.
Another thing to note is that you create scripts in an incremental manner. In other words, you start off doing something really simple and get it to work. Then, you might increase the complexity and get that to work. As you encounter issues or think of improvements, you add to the script. The bottom line is that you don't want to be overwhelmed and expect to get it all working on the first pass. It is much easier to break down a complicated script idea into several pieces, or to get a very basic version going first.
The print statement is your best friend when debugging. By adding temporary print statements you can tell when your code is hit. You can also print out any variables. Use quotes around normal text, including extra spaces for variables. Use the plus to add together a series of things you want to print out. For example: print("Hit code after buff. Number of items = " +numItems +" at price " +itemPrice); This code would tell you that you hit a certain point and then prints out some variables for you. Notice that printing a variable does NOT add any spacing around it, which is why I add spaces before and after my text in quotes.
Defining your Editor
In the KoLmafia frame, go to General, Preferences, General. At the bottom there is an External Script Editor field. Fill in whatever editor you want to use. (For windows, I'd go with c:\Windows\NOTEPAD.exe). Then you can easily edit a script by holding down shift and clicking it in the 'Scripts' pulldown menu. This is a really cool feature and keeps you from having to keep the KoLmafia/Scripts directory explorer around.