Wml for complete beginners: chapter 1/zh-hans

From The Battle for Wesnoth Wiki
< WML for Complete Beginners: Chapter 1
Revision as of 03:59, 12 August 2017 by Shadowm (talk | contribs) (Shadowmaster moved page WML for Complete Beginners: Chapter 1/zh to WML for Complete Beginners: Chapter 1/zh-Hans without leaving a redirect: Use correct naming convention for translated wiki pages)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)


第1章:语法

首先让我们看看 WML 的语法

对于不了解语言“语法”的朋友,可以把它看作是编写 WML 的规则。这个概念有些混乱,但是,无论您是否意识到,您在日常生活中时时刻刻都在使用语法。比如句子“我喜欢果冻和奶酪三明治”。这句话就包含语法,语法让受众能听懂句子的含义。如果您说“喜欢奶酪我和三明治果冻”,这句话也就失去了意义,因为没人听得懂。

就像英语的语法一样,WML 语法也需要字母、拼写、标点,这样才能让其他人理解您想表达什么。例如,如果您胡略语法,然后写出“一天玛丽和我有去看大象在动物园它是长”,那么大家不会明白您在写什么。但是如果您使用正确的语法:“某日,我和玛丽去动物园看到大象。它的鼻子真的很长”,这就很清楚了,因为这里用到了正确的语法。正如英国人无法理解使用错误语法写出的句子一样,韦诺之战也不会读取语法错误的脚本。

所以我们先看一下 WML 的基本语法。稍后再来介绍更复杂的 WML 语法,但是这些足够您开始学习。

基本组件:标签和属性

WML,就像首字母表达的那样,这是一个“标记语言。”这表示整个语言的语法包含两个基本元素:tagsattributes

标签

标签是由两个方括号扩起来的小写字符串,两端各一个。下面是“campaign”的标签示例:
[campaign]
标签只能包含字母和数字和下划线“_”。请注意前面提到的,“标签是小写字符串”。这是 WML 语法的基本:所有标签都用小写字母。如果您使用大写字母,WML 引擎会无法理解,并且发出错误。
和大部分标记语言一样,WML 的标签是成对出现的:一个开始标记和一个结束标记。就像一本书的封面和封底:打开一本书的封面表示开始,到达封底就意味着已经读完了这本书。同样,WML 引擎从开始标签开始。一直执行到结束标签。
结束标签除了和开始标签一样的关键字以外,还需要在方括号后面加入一个正斜杠“/”。正斜杠表示这个标签是结束标。下面是“campaign”标签的示例:
[/campaign]
开始标识和结束标识合起来称作标签集。标签集有两个标签:开始和结束。例如“campaign”标签集就像这个样子:
[campaign]
[/campaign]
所以我们现在知道 WML 引擎如何判断开始和结束和书写它们时的语法。这些是 WML 标签的基本概念。之后我们再讨论更复杂的标签;现在请确保您了解这些概念。
在进入下个章节之前,我们回顾一下这个部分提到的有关标签的内容:
  • 标签是使用两个方括号括起来的小写字符串。
  • 结束标签和起始标签相同,只是多了一个斜杠。
  • 起始标签和结束标签一起称为“标签集”。
  • 标签集只能由两个标签组成:起始和结束。
现在我们了解了起始、结束标签,下面该了解如何使用它们。

属性

属性由两部分组成:keysvalues,书写格式如下:
key=value
属性的功能是存储 WML 引擎需要的信息。属性的key说明存储什么样的信息,属性的value是存储的实际数据。等号左侧的文本作为键,右侧作为值。
这听起来有些乱,举个实际的例子来说明:
您想让您的朋友去杂货店买一块面包。您会直接对他说“去”吗?不会,这样他不会明白您让他做什么,您也不会有面包吃。同样,如果您只写一个标签集,等同于告诉 WML 引擎做什么,但是这还不够。如果您的朋友是个 WML 引擎,您让他去杂货店买面包,您可以给他这样的代码(注意:这不是真正的 WML 代码,这是教程中使用的伪代码)。
[go]
    where=grocery_store
    get=bread
[/go]
标签告诉 WML 引擎做什么(类似让您的朋友“去”),但是没有属性来指定要做什么(比如告诉朋友什么时候去哪里),如果没有具体的信息,WML 引擎不会做任何事情。如果您让您的朋友“去”,他什么都不会做,因为不知道去哪里做什么。
  • Keys
键是小写字母字符串,他告诉游戏处理的是什么信息。键是区分大小写的(虽然不能使用大写),需要正确拼写。
  • Values
WML 的键只会处理一个类型的数据,也就是字符串。字符串是 ASCII 字符序列,包括键盘上的任何字符。字符串分成两类:标准和数字。
1. Standard Strings
A standard string is simply a sequence of ASCII characters that is neither a numerical nor a translatable string. For example, this is a standard string:
x
as is this:
blue
If a standard string includes whitespaces, it must be enclosed within double quotes:
"Everything in these double quotes is a single string. This is the number one: 1. Hooray! :) We are now coming to the end of this string."
Everything within the double quotes (including the colon and parenthesis emoticon) is considered to one long string by the game.
Sometimes you will want to mark a standard string as translatable so that it can be translated into other languages. The only difference between a translatable sting and a non-translatable is that translatable strings are marked so that translators know that they need to translate that string, and non-translatable strings are not marked, so the translators know that they don't translate those strings.
To mark a string as translatable, all you have to do is add an underscore before the first double quote that marks the beginning of the string. Example of a translatable string:
 _ "This is a translatable string."
If the WML engine does not find an underscore in front of the string, it will assume the string is non-translatable.
Although not strictly necessary, it is generally considered a good practice to include a space before and after the underscore that marks a string as translatable. For instance, if we were to assign the translatable string "Hello World!" to this key, it would be considered good syntax to write
key= _ "Hello World!"
rather than
key=_"Hello World!"
However, the game considers both of the above strings to be equivalent, and will therefore recognize both as translatable strings. Adding whitespaces just allows for better human readability in your WML code.


2. Numerical Strings
Unsurprisingly, numerical strings are strings that contain only numbers, decimal points, or minus signs "-". If a string contains anything other than numbers, decimal points and/or a minus sign, the string becomes a standard string instead of a numerical one. Numerical strings can be either a single numeric character like this:
2
a sequence of numeric characters, like this:
230001
or floating-point values (that's just a fancy way of saying that they can contain decimal points), like these two examples:
2.6
395667.49382345
or negative numbers, like these examples:
-49
-594.932
For all intents and purposes, you can treat numerical strings just as you would numbers in real life. You can add, divide, and otherwise mathematically employ them in mathematical computations (we'll go over this in-depth in chapter [FIXME HERE]). Just remember that if you include any characters other than numbers, decimal points, or minus signs, the string will cease to be a numeric string and will become a standard string, which means you won't be able to use it in mathematical calculations.
Directory paths are simply special strings that tell the game where to find a specific file or folder. Here is an example of a directory path:
{data/add-ons/my_first_campaign}

This directory path tells the game where the folder "my_first_campaign" is located.

More About Tags

So now you should understand the basics about tags and attributes. As I promised earlier, we will now discuss some of the more involved aspects of tags.

Nested Tags: Parents and Children

A fundamental aspect of markup languages is that you can use tagsets inside other tagsets. Tagsets located inside other tagsets are called nested tagsets. In the example below, the "side" tagset is nested inside the "scenario" tagset:
[scenario]
    [side]
    [/side]
[/scenario]
When referring to nested tagsets, the tagset located inside the other is called the child tagset, and the tagset that encloses the child tagset is known ast he parent tagset. To illustrate with pseudocode:
[parent]
    [child]
    [/child]
[/parent]
Tagsets that are not child tagsets of any other tagsets are called toplevel tagsets. In this next example, the [scenario] tagset is a toplevel tagset, because it is not the child tagset of any other tagset. The [event] tagset is the child tagset of [scenario], because it is located inside the [scenario] tagset. The tagset [event] is also the parent tagset of the [message] tagset, because the [message] tagset is located inside the [event] tagset. That means that since [event] is the parent of [message], [message] is the child tagset of [event].
[scenario]
    [event]
        [message]
        [/message]
    [/event]
[/scenario]


Indentation and Levels

You may have noticed that in the examples above, the child tagsets are indented four spaces further to the right than are their parent tagsets. Why is this? It's because proper indentation (although not technically required by the WML engine) makes you code a lot easier to read and maintain. It's like writing an outline for a school paper, where you would write something like this:
I.
    A.
    B.
    C.
II.
    A.
        1.
        2.
    B.
    C.
Indentation makes it much easier to see whether tagset is the child or parent of other tagsets. The amount of indentation before a tagset determines in what level that tagset is located. If the tagset is a toplevel tagset (i.e. has no spaces in front of it because it is not the child of any other tagset), that tagset is located at level one. Tagsets with an indentation of four spaces in front of them are located in level 2, because they are the children of the toplevel (level 1) tagset. Tagsets that are children of level 2 tagsets are called level 3 tagsets (and are indented 8 spaces), tagsets inside level 3 tagsets are called level 4 tagsets (and are indented 12 spaces), etc. As a general rule, all the attributes of a tagset, along with any child tagsets of that tagset, are indented one level deeper than that tagset. To illustrate in pseudocode:
[toplevel_tagset]
    level_2_attribute=value
    [level_2_tagset]
        level_3_attribute=value
        [level 3 tagset]
            level_4_attribute=value
        [/level 3 tagset]
    [/level_2_tagset]
[/toplevel_tagset]
Indenting tagsets and attributes into levels like this makes it much easier for you (and others) to read, fix and maintain your code. It is strongly recommended that you indent exactly four spaces for each new level, although you can also use tabs instead of hitting the space key 4 times, if you'd prefer.

注意:本文翻译质量不佳

Next Chapter: WML for Complete Beginners: Chapter 2

Return to introduction: WML for Complete Beginners: Introduction

Return to Main Index: WML for Complete Beginners

This page was last edited on 12 August 2017, at 03:59.