Variables and if-else statements

When the party walks on state 11's special encounter, the scenario automatically ends. It would be better if the player was warned that moving to the east would end the scenario, and be given the option to not leave. Change state 11 to the following:

beginstate 11;
  reset_dialog();
  add_dialog_str(0, "Should you leave this merchant and her troubles behind, and look for work elsewhere?", 0);
  add_dialog_str(1, "(Do you want to leave the scenario?)", 0);
  add_dialog_choice(0, "Yes.");
  add_dialog_choice(1, "No.");
  choice = run_dialog(1);

  if (choice == 1) {
    // Using 0 means that the party won't increase its scenarios won count.
    end_scenario(0);
  }
break;

The new code creates a dialog box and gives the player options to choose from. It's a bit more complicated than the message_dialog function, but is a lot more flexible. reset_dialog is a required function call that resets the dialog box options. add_dialog_str lets you add text to the dialog box. You can add up to six lines (0 to 5 for the first argument, or option). The third argument is the indent; just keep it at 0 most of the time. add_dialog_choice adds an option for the player to choose. You can add up to three options (0 to 2 for the first argument). Finally, run_dialog makes the dialog box appear, and returns the option that the player chose. The argument determines whether or not the player can record the message; since we chose 1, the player will be able to.

[IMAGE]

Let's take a closer look at the line choice = run_dialog(1);. We know what run_dialog does. = is the assignment operator. It takes the value of what's on the right side, and gives it to whatever is on the left side. That leaves us with choice. choice is a variable; think of it as a temporary container that can hold a value. In this case, our line of code takes the option that the player chose, and makes choice equal to that number.

When you use a variable in a script, you have to make sure you define it. You have to give a list of all the variables you use at the beginning of a script. You also have to say what each variable's type is. There are two types in Avernumscript: int and string. int is short for 'integer': an int can hold any whole number, positive or negative. You can also use short instead of int when defining variables, it means the same thing. A string variable can hold a string of text.

At the beginning of the town script, add the declaration for choice:

begintownscript;

variables;

int choice;

body;

Note that you didn't have to name your variable choice. You could have called it option1, x, PlayerChoice, or the_choice. It just has to be 19 characters long or less, and not be an AvernumScript term like if or break. Make sure you pick informative variable names.

The rest of state 11 uses the if statement we saw in the last section. If the player chooses option #1, the scenario ends. Otherwise, nothing happens. Due to a confusing AvernumScript bug, choosing the option added with add_dialog_choice(0, "Yes."); makes choice equal to 1, and choosing the option added with add_dialog_choice(1, "No."); makes choice equal to 2. We want to make one extra change to state 11. If the player doesn't want to leave the scenario, they should be stopped from moving any further east. We can do this with an extra if statement:

  if (choice == 1) {
    // Using 0 means that the party won't increase its scenarios won count.
    end_scenario(0);
  }

  if (choice == 2) {
    block_entry(1);
  }

There's a slightly simpler way of doing this: the if-else statement:

  if (choice == 1) {
    // Using 0 means that the party won't increase its scenarios won count.
    end_scenario(0);
  } else {
    block_entry(1);
  }

The if-else statement checks to see if what's inside the parentheses is true. If it is, it runs the code inside the first set of curly brackets. Otherwise, it runs the code inside the second set of curly brackets.

The town script is done for the time being. Now it's time to finally put the merchant and her guard in the scenario.