Difference between revisions of "WML for Complete Beginners: Chapter 10"

From The Battle for Wesnoth Wiki
(Nicer index and navigation)
(Use syntax highlighting)
 
Line 14: Line 14:
  
 
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:
 
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:
 
+
<syntaxhighlight lang=wml>
[if]
+
[if]
    # (condition)
+
    # (condition)
    [then]
+
    [then]
        # (do something)
+
        # (do something)
    [/then]
+
    [/then]
[/if]
+
[/if]
 +
</syntaxhighlight>
  
 
===Else===
 
===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:
 
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:
 
+
<syntaxhighlight lang=wml>
[if]
+
[if]
    # (condition]
+
    # (condition]
    [then]
+
    [then]
        # (do something)
+
        # (do something)
    [/then]
+
    [/then]
    [else]
+
    [else]
        # (do something else)
+
        # (do something else)
    [/else]
+
    [/else]
[/if]
+
[/if]
 +
</syntaxhighlight>
  
 
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:
 
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]
+
<syntaxhighlight lang=wml>
    # the car door is open
+
[if]
    [then]
+
    # the car door is open
        # go over and shut it
+
    [then]
    [/then]
+
        # go over and shut it
    [else]
+
    [/then]
        # do a backflip
+
    [else]
    [/else]
+
        # do a backflip
[/if]
+
    [/else]
 +
[/if]
 +
</syntaxhighlight>
  
 
{{Navigation|[[WML for Complete Beginners: Chapter 9|'''Chapter 9'''<br>Macros]]|[[WML for Complete Beginners: Chapter 11|'''Chapter 11'''<br>More Logic]]}}
 
{{Navigation|[[WML for Complete Beginners: Chapter 9|'''Chapter 9'''<br>Macros]]|[[WML for Complete Beginners: Chapter 11|'''Chapter 11'''<br>More Logic]]}}
  
 
[[Category:WML_for_Complete_Beginners]]
 
[[Category:WML_for_Complete_Beginners]]

Latest revision as of 20:42, 29 January 2023

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.