Scripting changes in 7.5

Veracity

Developer
Staff member
The following changes to scripting went into KoLmafia 7.5:

1) I attempted to rationalize how ASH handles errors. Many commands can fail. Those that do usually return a boolean to let you know whether they succeeded or not.

- If you do not "capture" the return value, the script will abort execution after a failure.
- If you DO "capture" it, the script will continue execution.
- However, if the user presses stop, that's an uncapturable failure; the script will abort execution.

You can "capture" the return value of a script command in the following ways:

- Using it as the test conditional for an "if" or "while" command
- Assigning the result to a variable
- Using the result as a parameter in a function call
- Passing back the result with a "return" command
- Doing boolean negation on it with the "!" operator
- Using the result as the left hand side of an operator
- Using the result as the right hand side of an operator, although && and || don't always evaluate the rhs, of course

2) I put in some fairly verbose ASH script "tracing" which kicks in when you have debug logging active. Not only does ASH print the parse tree to the log, as before, but will trace the script execution as well.

I think this might be too much, and I was going to disable it - or find a way to make it optional - but the release came before I was done debugging. So, it's still there - and will be helpful for continued debugging, at least.

3) I changed how ASH handles top-level commands and "main" functions. It used to be that top-level commands were ignored unless they were in the main ASH file you invoked; they were ignored in imported files. Even in the top-level file, they were ignored if you had a "main" function. If you had such a function, ASH executed it and ignored the commands. Otherwise, it executed the commands.

I realized that ANY file - imported or not - could use top-level commands to do complicated initialization and printing and such, so the new behavior is as follows:

- When parsing the ASH script, all top-level commands, main script or imported script, are saved up in the order ASH see them.
- If no parse errors are found, ASH will start execution by running through all the top-level commands.
- When those commands are complete, if the script contained a "main" function, ASH will execute that function and return whatever it returns.

As before, it's an error if you have neither a "main" function or any top-level commands.

A consequence of this change is that you can now declare and initialize global variables; a top-level variable declaration has always been accessable from later imported files or the main script, but now you can initialize such variables.

4) New data type and functions, to give access to further internal data:

monster m = $monster[Knott Yeti];
int monster_base_attack( monster );
int monster_base_defense( monster );
int monster_base_HP( monster );
int weapon_hands( item );
int ranged_weapon( item );

5) The "float" data type now prints as a float, rather than being rounded to an integer. It's now stored as a double; all internal arithmetic was always done on doubles, so no there's no loss of precision when the value is finally stored.
 
Top