Stuff Done Flags and if statements

As you're testing the scenario, you'll notice that the message_dialog from state 10 appears everytime your party walks over the special encounter. It would be better if that message only appeared once. We need a way for the game to remember that state 10 has been run. To do that, we need to use a Stuff Done Flag (SDF, from now on). Change state 10 to the following:

beginstate 10;
  set_flag(0, 0, 1);
  message_dialog(
    "You see a cart ahead, with a man in leather armour standing guard over a merchant by her wares.",
    "She seems upset about something."
  );
break;

Whenever you play a scenario, the game keeps track of a grid of numbers; each entry in the grid is a SDF. You can access each SDF with its row and column number. Every SDF stars at 0, but the call to the set_flag function in state 10 takes the SDF at row 0 and column 0 and makes it equal 1. What SDF (0,0) represents is whether or not the party has triggered state 10. If SDF (0,0) equals 0, state 10 hasn't been run yet. If it equals 1, it has.

It isn't enough to set SDF (0,0) to 1, because there isn't any code that checks to value of it. We need to add a new type of statement to state 10:

beginstate 10;
  if (get_flag(0, 0) == 0) {
    set_flag(0, 0, 1);
    message_dialog(
      "You see a cart ahead, with a man in leather armour standing guard over a merchant by her wares.",
      "She seems upset about something."
    );
  }
break;

Say hello to the if statement. The if statment has two parts: the part wrapped in parentheses, and the part wrapped in curly brackets. First the if statement checks to see if what's inside the parentheses is true. In this case, it checks if SDF (0,0) is equal to 0. The get_flag function returns the value of the SDF at the given coordinates, and == is the comparison operator; it returns true if what's on the left side equals what's on the right side. You can find other operators on page 71 of the offical documentation.

If what's inside the parentheses is true, the if statement runs the code inside the curly brackets. In this case, it will set SDF (0,0) to 1, and then display a message. But note what happens the second time state 10 is run. This time, SDF (0,0) will not equal 0, and thus the code inside the curly brackets will not run.

There are a lot of SDFs available for use in your scenario: the first number can be from 0 to 299, and the second from 0 to 29. It's important to be organized when using them. A common technique is to use SDFs (0,0), (0,1), (0,2) and so on for town 0; (1,0), (1,1), (1,2) and so on for town 1, etc. Also, keep up to date notes that remind you what each SDF is for, and what each value means. It will come in handy when you have dozens or hundreds of SDFs to keep track of. A possible format could be like this:

0: Merchant's Tunnel
  0: Seeing the merchant (state 10)
    0: Haven't seen
    1: Have seen

(A note to experienced programmers: I realize that I am simplifying things. The purpose of this tutorial is to teach novice designers how to make simple scenarios, not to teach concepts like boolean logic. The techniques given here and later on will work for 90% of situations when designing. If you want to learn more theory and other useful techniques, read the offical documentation, and look at scripts by other designers.)

Time to learn another programming concept: variables.