WML for Complete Beginners: Chapter 11

From The Battle for Wesnoth Wiki
Revision as of 17:39, 2 April 2013 by Artisticdude (talk | contribs) (Created page with '==Chapter 11: More Logic: Cases and Loops== ===Cases=== Suppose you had a scenario in which the player must pick a flower by moving a unit to the coordinates 14,30 in order to …')
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Chapter 11: More Logic: Cases and Loops

Cases

Suppose you had a scenario in which the player must pick a flower by moving a unit to the coordinates 14,30 in order to win. But that's not all: the flower can only be picked after turn 10 and before turn 20 (so the flower can only be picked between turns 11 and 20). If the player tries to pick the flower before turn 11, the game tells him that the flower hasn't bloomed yet and that he or she needs to wait a while longer. If the player tries to pick the flower after turn 20, the game will display a message telling him or her that he or she waited too long, and now the flower is dead. How would you go about implementing this in WML?

Well, you could write something like this:

[event]
    name=moveto
    first_time_only=no
    [filter]
        side=1
    [/filter]
    [if]
        [variable]
            name=turn_number
            less_than_equal_to=10
        [/variable]
        [then]
            [message]
                speaker=narrator
                message= _ "The flower hasn't bloomed yet. Try coming back later."
            [/message]
        [/then]
    [/if] 

    [if]
        [variable]
            name=turn_number
            greater_than=20
        [/variable]
        [then]
            [message]
                speaker=narrator
                message= _ "You waited too long: the flower is dead now."
            [/message]
        [/then]
    [/if]

    [if]
        [variable]
            name=turn_number
            greater_than=10
        [/variable]
    [and]
        [variable]
            name=turn_count
            less_than_equal_to=20
        [/variable]
    [/and]
        [then]
            [message]
                speaker=narrator
                message= _ "You pick the flower."
            [/message]
        [/then]
    [/if]

[/event]

but this is extremely lengthy and tedious to write and maintain. Wouldn't it be awesome if we had some simpler way to test multiple conditions without having to create an entire [if] codeblock for each condition?

Well, actually, there is an easier way to test for multiple conditions without having to wade through a quagmire of if codeblocks. This is done via the switch/case codeblock, which tests the value of a variable for multiple conditions, and determines the action performed as a result of that evaluation. The syntax of the switch/case codeblock goes thusly:

[switch]
    name=variable_name
    [case]
        value=first_value
        #do something
    [/case]
    [case]
        value=second_value
        #do something
    [/case]
    [case]
        value=third_value
        #do something
    [/case]
    [else]
        #do something
    [/else]
[/switch]

Loops

I was a very messy child, much to the distress of my mother, who was the epitome of cleanliness and couldn't for the life of her understand how she could have raised such an untidy child as me. Occasionally she'd work up enough courage to venture into the land of chaos that was my room, and then she'd tell me not leave my room until it was picked up.

Okay, intimate family analogies aside, pay attention to how my mother told me to clean up my room: I was not to leave my room until it was clean. Whether or not the room was clean was the condition on which my permission to leave my room depended. So each time I put something away (which, more often then not, I did by stuffing the item under the bed), I would have to check to see that condition was met. If the room wasn't clean, then my mother's condition wasn't met and I had to keep cleaning. If the room was clean, then my mother's condition was met and I was free to leave my room.

Talk about basic parts of loops, variations of loops (do/while, etc.)