WML for Complete Beginners: Chapter 10

From The Battle for Wesnoth Wiki

Chapter 10: Introduction to Logic

If This, Then That

Suppose you went to the grocery store to get some food. You park you car in the parking lot and begin to walk towards the store, when suddenly you realize that you can't remember if you shut the car door or not after you got out. You turn around to see if the car door is open...

...and then what?

Well, if the car door is open, you know that you did forget to close it, so you go over and close it before you go into the grocery store. If the car door isn't open, there's no need for you to do anything, so you keep walking towards the store.

This kind of logic (called conditional logic) evaluates a condition and determines a reaction based on the state of that condition. In the example above, the condition is whether or not the car door is open. If it is open, then you go over and close it. If it isn't open, then you continue walking towards the store.

In WML you have a number of logical operations available to assess and react to conditions. The first one we will go over is the if/then operation. The syntax of this operations is:

[if]
    # (condition)
    [then]
        # (do something)
    [/then]
[/if]

Else

With the if clause, if the condition is true then we do something. But what if the condition isn't true? Well, in the examples above the WML engine doesn't have anything to do if the condition isn't true, so it just keeps running and doesn't do anything. However, it is possible to make the game perform a task if the condition isn't true, which is done by using the tagset [else]. The syntax is:

[if]
    # (condition]
    [then]
        # (do something)
    [/then]
    [else]
        # (do something else)
    [/else]
[/if]

Returning to the example of the car door, suppose that, before you turn around to check to see whether the door is open or not, you decide to go over and close the door if it is open, and if it isn't then you'll do a backflip to celebrate your genius before continuing your journey across the parking lot to the grocery store. In pseudocode:

[if]
    # the car door is open
    [then]
        # go over and shut it
    [/then]
    [else]
        # do a backflip
    [/else]
[/if]
Navigation
Chapter 9
Macros
WML for Complete Beginners: Chapter 10 Chapter 11
More Logic
This page was last edited on 29 January 2023, at 20:42.