Difference between revisions of "WML for Complete Beginners: Chapter 11"
m (no such variable turn_count) |
(Use syntax highlighting) |
||
(3 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
+ | {{Translations}}<div style="float:right">{{:WML for Complete Beginners}}</div> | ||
+ | |||
==Chapter 11: More Logic: Cases and Loops== | ==Chapter 11: More Logic: Cases and Loops== | ||
Line 6: | Line 8: | ||
Well, you could write something like this: | Well, you could write something like this: | ||
+ | <syntaxhighlight lang=wml> | ||
+ | [event] | ||
+ | name=moveto | ||
+ | first_time_only=no | ||
+ | [filter] | ||
+ | side=1 | ||
+ | x=14 | ||
+ | y=30 | ||
+ | [/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_number | ||
+ | less_than_equal_to=20 | ||
+ | [/variable] | ||
+ | [/and] | ||
+ | [then] | ||
+ | [message] | ||
+ | speaker=narrator | ||
+ | message= _ "You pick the flower." | ||
+ | [/message] | ||
+ | [/then] | ||
+ | [/if] | ||
+ | |||
+ | [/event] | ||
+ | </syntaxhighlight> | ||
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? | 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: | 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: | ||
− | + | <syntaxhighlight lang=wml> | |
− | + | [switch] | |
− | + | variable=turn_number | |
− | + | [case] | |
− | + | value=1,2,3,4,5,6,7,8,9,10 | |
− | + | # too soon | |
− | + | [/case] | |
− | + | [case] | |
− | + | value=11,12,13,14,15,16,17,18,19,20 | |
− | + | # pick flower | |
− | + | [/case] | |
− | + | [else] | |
− | + | # too late | |
− | + | [/else] | |
− | + | [/switch] | |
+ | </syntaxhighlight> | ||
===Loops=== | ===Loops=== | ||
Line 87: | Line 92: | ||
Talk about basic parts of loops, variations of loops (do/while, etc.) | Talk about basic parts of loops, variations of loops (do/while, etc.) | ||
− | + | {{Navigation|[[WML for Complete Beginners: Chapter 10|'''Chapter 10'''<br>Logic]]|[[WML for Complete Beginners: Conclusion|Conclusion]]}} | |
− | [[WML for Complete Beginners: | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | [[WML for Complete Beginners]] | ||
[[Category:WML_for_Complete_Beginners]] | [[Category:WML_for_Complete_Beginners]] |
Latest revision as of 20:43, 29 January 2023
Chapter 11: More Logic: Cases and Loops
Cases and the Switch Statement
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
x=14
y=30
[/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_number
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]
variable=turn_number
[case]
value=1,2,3,4,5,6,7,8,9,10
# too soon
[/case]
[case]
value=11,12,13,14,15,16,17,18,19,20
# pick flower
[/case]
[else]
# too late
[/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.)
Navigation | ||
---|---|---|
Chapter 10 Logic |
WML for Complete Beginners: Chapter 11 | Conclusion |