Questions on Scripting

SilentNN

New member
I'm fairly new to scripting. Even basic scripting. Heck, even KoLmafia.

I've read the general ASH tut by Xylpher (link) and I could understand most of it. I'm still a bit shaky with a few things, but I know how to make ASH scripts for the most part. There were a few parts where Xylpher put things into the script without taking note of them, so I have a few questions.

First, what is the difference between using = and ==? I think I've even seen a === in a script somewhere before.

What is the int, boolean, string, class, float, and whatnot that precedes so many entries in the wiki? Does that mean the thing (I don't know what it's called.), such as my_level(), returns (returns is a misleading word to me) an integer? And for boolean, it's yes/true or no/false? If so, what are the others? And what about when you put something like int into a script. Does the "word" (again, don't know what to call it) become a variable to be defined? I.E. int iterations.

I've seen scripts that started out with the line (well, there's a void in the line before) "int iterations;". What does this do? Does iterations become a variable, to be defined later? If so, can it also be defined now, like "int iterations = 0;" (again, not sure about = and ==)?

I probably have more questions, but I don't have the time to think of them and type them out, so until later and thanks in advance.
 
Hopefully I can help.

[quote author=SilentNN link=topic=1690.msg7921#msg7921 date=1208837001]
First, what is the difference between using = and ==? I think I've even seen a === in a script somewhere before.[/quote]
The single equal sign is used to set the value of a variable. If I write
Code:
someVariable = 11;
it will set the value of someVariable to whatever is on the right side of the equal sign (in this case, 11).

The double equal sign is used in comparisons, just like the greater than or less than sign. For example:
Code:
if(someVariable == 11) {
    print("That's ridiculous!");
}
This code will print "That's ridiculous!" if the value of someVariable is eleven. It will not change the value of someVariable at all.

[quote author=SilentNN link=topic=1690.msg7921#msg7921 date=1208837001]What is the int, boolean, string, class, float, and whatnot that precedes so many entries in the wiki? Does that mean the thing (I don't know what it's called.), such as my_level(), returns (returns is a misleading word to me) an integer? And for boolean, it's yes/true or no/false? If so, what are the others? [/quote]
Yes, those keywords indicate the type of value returned by that function. my_level() returns an integer, because you can never be level 3.5, or level "potato".

As for what they mean:

  • [li]int - An integer.[/li]
    [li]boolean - Either true or false.[/li]
    [li]string - A piece of text.[/li]
    [li]float - A number. Unlike ints, these can have a decimal point.[/li]
    [li]class, stat, item, skill, familiar, etc. - These all represent class, stat, item, skill, familiar, or whatever in KoL.[/li]
    [li]void - That particular function returns nothing.[/li]


[quote author=SilentNN link=topic=1690.msg7921#msg7921 date=1208837001]And what about when you put something like int into a script. Does the "word" (again, don't know what to call it) become a variable to be defined? I.E. int iterations.

I've seen scripts that started out with the line (well, there's a void in the line before) "int iterations;". What does this do? Does iterations become a variable, to be defined later? If so, can it also be defined now, like "int iterations = 0;" (again, not sure about = and ==)?
[/quote]
In order to use a variable, you first need to tell Mafia what sort of variable it is.

If I were to write this script
Code:
void main() {
    message = "Hello world!";
    print(message);
}
Mafia would get to the message = "Hello world!"; line and throw an error because it's never heard of this "message" variable before. On the other hand, if I try this
Code:
void main() {
    string message = "Hello world!";
    print(message);
}
it creates the "message" variable and gives it the value "Hello world!".

And as you guessed, this code
Code:
void main() {
    string message;
    message = "Hello world!";
    print(message);
}
creates the variable but does not give it a value until the next line.
 
Top