Difference between revisions of "WML for Complete Beginners"

From The Battle for Wesnoth Wiki
m
Line 11: Line 11:
 
Hey there!
 
Hey there!
  
Now I'm guessing that, if you're reading this, you already know what The Battle for Wesnoth is. If you don't, I suggest finding out before reading further.  If you have played  
+
Now I'm guessing that, if you're reading this, you already know what The Battle for Wesnoth is. If you don't, I suggest finding out before reading further.  If you have played...blahblahblah. (placeholder)
  
 
===Who This Tutorial is For===
 
===Who This Tutorial is For===
Line 18: Line 18:
 
===What Tools You Will Need===
 
===What Tools You Will Need===
  
Programming WML requires only one tool: a basic text editor. You don't need a fancy word processor to program WML, a simple program like Windows Notepad or Mac OS X TextEdit will work just fine. For Linux, there is a very nice text editing application called Kate that actually comes with syntax highlighting for WML.
+
Programming WML requires only one tool: a basic text editor. You don't need a fancy word processor to program WML; a simple program like Windows Notepad or Mac OS X TextEdit will work just fine. For Linux, there is a very nice text editing application called Kate that actually comes with syntax highlighting for WML.
  
 
===So What Exactly is WML?===
 
===So What Exactly is WML?===
Line 44: Line 44:
 
Notice that I said "a tag is a string of ''lowercase'' text". This is a fundamental aspect of the WML syntax: all tags are always written in lowercase letters. If you try and use capital letters (even just one), the WML engine won't be able to understand what you've written and will give you an error when it tries to read the incorrect tag.
 
Notice that I said "a tag is a string of ''lowercase'' text". This is a fundamental aspect of the WML syntax: all tags are always written in lowercase letters. If you try and use capital letters (even just one), the WML engine won't be able to understand what you've written and will give you an error when it tries to read the incorrect tag.
  
As with most markup languages, in WML tags are always used in pairs: one opening tag and one closing tag. Think of the opening and closing tags like the covers of a book: when you open the front cover, you know you're at the beginning. When you reach the back cover, you know you're done reading the book. Likewise, when the WML engine finds a closing tag, it realizes it's at the beginning of a task. When it reaches the closing tag, it realizes it has finished the task.
+
As with most markup languages, in WML tags are always used in pairs: one opening tag and one closing tag. Think of the opening tag and the closing tag like the covers of a book: when you open the front cover, you know you're at the beginning. When you reach the back cover, you know you're done reading the book. Likewise, when the WML engine finds an opening tag, it realizes it's at the beginning of a task. When it reaches the closing tag, it realizes it has finished the task.
  
Closing tags are exactly the same as opening tags except for one key component: closing tags always have a forward slash "/" immediately after the first square bracket. This forward slash tells the WML engine that this is a closing tag and not another opening tag. Here is an example of the closing "campaign" tag:
+
Closing tags are exactly the same as opening tags except for one key component: closing tags always have a forward slash "/" immediately after the first square bracket. This forward slash tells the WML engine that this tag is a closing tag and not another opening tag. Here is an example of the closing "campaign" tag:
 
  [/campaign]
 
  [/campaign]
  
 
The opening and closing tag together are referred to as a "tagset". A tagset always contains exactly two tags: the opening tag and the closing tag. So the "campaign" tagset would look like this:
 
The opening and closing tag together are referred to as a "tagset". A tagset always contains exactly two tags: the opening tag and the closing tag. So the "campaign" tagset would look like this:
 
 
  [campaign]
 
  [campaign]
 
  [/campaign]
 
  [/campaign]
Line 59: Line 58:
 
*The opening tag and the closing tag together are called the tagset. A tagset consists of only two tags: the opening tag and the closing tag.
 
*The opening tag and the closing tag together are called the tagset. A tagset consists of only two tags: the opening tag and the closing tag.
  
So now we know how to tell the WML engine where the beginning and the end of a task are, but what about actually giving it the task? This is where attributes come in.
+
So now we know how to tell the WML engine where the beginning and the end of a task are, but what about actually making it do the task? This is where attributes come in.
  
 
====Attributes====
 
====Attributes====
 +
 +
Attributes consist of two principal elements: ''keys'' and ''values''.
  
 
===More About Tags===
 
===More About Tags===
Line 70: Line 71:
  
 
===The Userdata Directory===
 
===The Userdata Directory===
 +
 +
There are two main directories that Wesnoth creates on your computer. The '''installation directory''' contains all the files for the core game. The '''userdata directory''' stores all your personal Wesnoth data, which includes your installed add-ons, your settings and preferences, and your saved games. This is where we will store your work throughout this tutorial.
 +
 +
The location of your userdata directory differs depending on your operating system, and in the case of Microsoft Windows, the version of your operating system. Refer to the following table to determine where your userdata directory is located.
 +
 +
 +
{| border="1"
 +
!Windows XP
 +
|~/My Documents/My Games/Wesnoth 1.10/
 +
|-
 +
!Windows Vista and above
 +
|~/Documents/My Games/Wesnoth 1.10/
 +
|-
 +
!Mac OS X
 +
|~/Library/Application Support/Wesnoth 1.10/
 +
|-
 +
!Linux
 +
|~/.local/share/wesnoth/1.10/
 +
|}
 +
 +
 +
Now navigate to your userdata folder. You should see a total of five folders ("cache", "data", "editor", "persist", and "saves") in this directory, along with two files ("preferences" and "save_index.gz"). If you see all seven of these, everything is good. If you do not see all seven, try running the Wesnoth application. If that does not work, try restarting your computer. If neither of these steps work, reinstall Wesnoth.
 +
 +
Of the seven objects contained in the userdata folder, you will only ever need to interact with two: the "data" folder and the "editor" folder.
 +
 +
====The data folder====
 +
 +
The data folder is where all installed add-ons are kept. This is where we will be building our campaign, when we get to that. If you have installed any Wesnoth add-ons, they should show up in this folder.
 +
 +
====The editor folder====
 +
 +
The editor folder is where all maps created with the in-game map editor are stored. If you have ever created and saved a map in-game, you should see the map file in this directory.
  
 
===The Campaign Folder===
 
===The Campaign Folder===
  
==Chapter 3: The _main.cfg==
+
Like I told you earlier, all add-ons are installed in the data folder inside the userdata directory. That means that your campaign will also be located inside the data folder. If you're not already there, enter the data folder now.
 +
 
 +
Next, create a new folder inside the data folder. Name this new folder "my_campaign" exactly as I wrote it here (but don't include the quotation marks). Make sure you spelled it right, that you didn't accidentally capitalize any letters and that you included the underscore between "my" and "campaign" in the folder name, or your campaign won't work when you try and play it.
 +
 
 +
Now enter the "my_campaign" folder and create six new folders. Name these folders "images", "macros", "maps", "scenarios", "units" and "utils" (again, make sure you spelled everything right, that you didn't accentually capitalize anything, and that you don't include the quotation marks).
 +
 
 +
All campaigns share this common folder structure.
 +
 
 +
==The _main.cfg==
 +
 
 +
 
 +
And now we're going to add the "campaign" tagset. Yes, it's our old friend "campaign" from Chapter 1. This
 +
 
 +
 
 +
==Creating Your First Scenario==
 +
 
 +
Now that you have your _main.cfg file, it's time to create your first scenario.
 +
 
 +
Create a new text file in the "scenarios" folder inside your campaign folder. Name this new text file "my_first_scenario.cfg". Open the "my_first_scenario.cfg" file in your text editor, if you haven't already. Since it is a new file, it should be completely empty right now. It won't be when we're done with it, however!
 +
 
 +
First, let's add the "scenario" tagset:
 +
 
 +
[scenario]
 +
[/scenario]
 +
 
 +
The "scenario" tagset tells the game where the scenario begins and where it ends. All the information for your scenario goes within this tagset.
  
==Chapter 4: Creating Your First Scenario==
+
Next, inside the "scenario" tagset, include the following keys (don't forget to indent 4 spaces before each key):
 +
id=
 +
next_scenario=
 +
name=
 +
map_data=
 +
turns=
  
==Chapter 5: Events==
 
  
==Chapter 6: ==
+
==Events==

Revision as of 23:41, 8 February 2012

Template for future "WML for Complete Beginners" tutorial

Note: this is a work in progress.




Introduction

Hey there!

Now I'm guessing that, if you're reading this, you already know what The Battle for Wesnoth is. If you don't, I suggest finding out before reading further. If you have played...blahblahblah. (placeholder)

Who This Tutorial is For

This tutorial is aimed at users with no previous programming knowledge. This tutorial does assume that you have a certain level of competence in basic computer skills and concepts, such as opening and editing text files, and being able to follow folder directories. In this tutorial you will learn WML by building a short single-player campaign from the ground up.

What Tools You Will Need

Programming WML requires only one tool: a basic text editor. You don't need a fancy word processor to program WML; a simple program like Windows Notepad or Mac OS X TextEdit will work just fine. For Linux, there is a very nice text editing application called Kate that actually comes with syntax highlighting for WML.

So What Exactly is WML?

WML is an acronym for the "Wesnoth Markup Language", a custom scripting language that The Battle For Wesnoth uses to allow players to create and modify content without having to learn a much more complex language like Lua or C++. Now I'm going to say this here and now: don't think you can learn WML overnight. Although WML is relatively easy to learn, it will take a certain amount of effort, time and dedication to fully understand the ins and outs of the language.

Chapter 1: Syntax

First things first; let's go over the syntax of WML.

For those of you who might not know what the "syntax" of a language is, think of it as a set of rules for how WML needs to be written in order for the game to understand it. This concept may sound a bit confusing, but whether you realize it or not you have been using the concept of syntax your entire life! Think of the structure of a sentence in English: "I like to eat jelly and cheese sandwiches." That sentence uses a certain set of rules, or syntax in order to make sense to people who hear or read it. If you said instead, "Like cheese I and sandwiches jelly", that would make no sense, and no one would understand what you were saying. Likewise, if you said "I like cheese sandwiches and jelly", that would change the entire meaning of the sentence!

Just like the syntax of the English language, WML syntax also requires proper capitalization, spelling, grammar and punctuation in order for others to understand what you're saying or writing. For example, if you decided to ignore the syntax of the English language and wrote something like this: "won day mary and i had went to seen the elefant at the zoo it's trunc was reely long", chances are people would not understand much of what you wrote. On the other hand, if you used the correct syntax and wrote: "One day Mary and I went to see the elephant at the zoo. Its trunk was really long!", people can easily understand what you wrote because it is written in the syntax they recognize and understand. Just as English-reading people would be unable to understand something were it not written in the correct English syntax, the Battle for Wesnoth game is unable to read any WML that is not written in the correct WML syntax.

So now let's go over the basics of WML syntax. Later on you will be gradually introduced to more complex WML syntax, but for now we'll just go over the basic stuff, enough to get you started.

Basic Components: Tags and Attributes

WML, as one can infer from the meaning of the acronym, is a markup language. This means that the syntax of the entire language consists of two fundamental elements: tags and attributes.

Tags

A tag is a string of lowercase text encapsulated by two square brackets, one at either end. This is an example of a "campaign" tag:

[campaign]

Notice that I said "a tag is a string of lowercase text". This is a fundamental aspect of the WML syntax: all tags are always written in lowercase letters. If you try and use capital letters (even just one), the WML engine won't be able to understand what you've written and will give you an error when it tries to read the incorrect tag.

As with most markup languages, in WML tags are always used in pairs: one opening tag and one closing tag. Think of the opening tag and the closing tag like the covers of a book: when you open the front cover, you know you're at the beginning. When you reach the back cover, you know you're done reading the book. Likewise, when the WML engine finds an opening tag, it realizes it's at the beginning of a task. When it reaches the closing tag, it realizes it has finished the task.

Closing tags are exactly the same as opening tags except for one key component: closing tags always have a forward slash "/" immediately after the first square bracket. This forward slash tells the WML engine that this tag is a closing tag and not another opening tag. Here is an example of the closing "campaign" tag:

[/campaign]

The opening and closing tag together are referred to as a "tagset". A tagset always contains exactly two tags: the opening tag and the closing tag. So the "campaign" tagset would look like this:

[campaign]
[/campaign]

So now we know how the WML engine knows where the beginning and end of a task are, and what syntax to use when writing them. Before we move on to the next section, let's review the points we've learned in this section about tags:

  • A tag is a string of lowercase text encapsulated by two square brackets, one at either end.
  • A closing tag is exactly the same as an opening tag except for the forward slash immediately following the first square bracket.
  • The opening tag and the closing tag together are called the tagset. A tagset consists of only two tags: the opening tag and the closing tag.

So now we know how to tell the WML engine where the beginning and the end of a task are, but what about actually making it do the task? This is where attributes come in.

Attributes

Attributes consist of two principal elements: keys and values.

More About Tags

-Discuss nested tags, whitespaces and underscores in tags


Chapter 2: The Userdata Directory and the Campaign Folder

The Userdata Directory

There are two main directories that Wesnoth creates on your computer. The installation directory contains all the files for the core game. The userdata directory stores all your personal Wesnoth data, which includes your installed add-ons, your settings and preferences, and your saved games. This is where we will store your work throughout this tutorial.

The location of your userdata directory differs depending on your operating system, and in the case of Microsoft Windows, the version of your operating system. Refer to the following table to determine where your userdata directory is located.


Windows XP ~/My Documents/My Games/Wesnoth 1.10/
Windows Vista and above ~/Documents/My Games/Wesnoth 1.10/
Mac OS X ~/Library/Application Support/Wesnoth 1.10/
Linux ~/.local/share/wesnoth/1.10/


Now navigate to your userdata folder. You should see a total of five folders ("cache", "data", "editor", "persist", and "saves") in this directory, along with two files ("preferences" and "save_index.gz"). If you see all seven of these, everything is good. If you do not see all seven, try running the Wesnoth application. If that does not work, try restarting your computer. If neither of these steps work, reinstall Wesnoth.

Of the seven objects contained in the userdata folder, you will only ever need to interact with two: the "data" folder and the "editor" folder.

The data folder

The data folder is where all installed add-ons are kept. This is where we will be building our campaign, when we get to that. If you have installed any Wesnoth add-ons, they should show up in this folder.

The editor folder

The editor folder is where all maps created with the in-game map editor are stored. If you have ever created and saved a map in-game, you should see the map file in this directory.

The Campaign Folder

Like I told you earlier, all add-ons are installed in the data folder inside the userdata directory. That means that your campaign will also be located inside the data folder. If you're not already there, enter the data folder now.

Next, create a new folder inside the data folder. Name this new folder "my_campaign" exactly as I wrote it here (but don't include the quotation marks). Make sure you spelled it right, that you didn't accidentally capitalize any letters and that you included the underscore between "my" and "campaign" in the folder name, or your campaign won't work when you try and play it.

Now enter the "my_campaign" folder and create six new folders. Name these folders "images", "macros", "maps", "scenarios", "units" and "utils" (again, make sure you spelled everything right, that you didn't accentually capitalize anything, and that you don't include the quotation marks).

All campaigns share this common folder structure.

The _main.cfg

And now we're going to add the "campaign" tagset. Yes, it's our old friend "campaign" from Chapter 1. This


Creating Your First Scenario

Now that you have your _main.cfg file, it's time to create your first scenario.

Create a new text file in the "scenarios" folder inside your campaign folder. Name this new text file "my_first_scenario.cfg". Open the "my_first_scenario.cfg" file in your text editor, if you haven't already. Since it is a new file, it should be completely empty right now. It won't be when we're done with it, however!

First, let's add the "scenario" tagset:

[scenario]
[/scenario]

The "scenario" tagset tells the game where the scenario begins and where it ends. All the information for your scenario goes within this tagset.

Next, inside the "scenario" tagset, include the following keys (don't forget to indent 4 spaces before each key):

id=
next_scenario=
name=
map_data=
turns=


Events