QML - Create Your Own Adventure

Practical examples

Here you can find QML code examples, each offering one approach to a certain task (more often than not, many approaches are "correct"). If the station element itself is missing in the sample, it's simply the station of id "start".

All samples need QML2.02+ to work.

Light switch

The following lets the player turn a light switch in a room on and off:

<text>You're in dark a room. The light here is </text>
<text check="[light]">on, shining brightly.</text>
<text check="not [light]">off, and darkness surrounds you.</text>
<choice station="start" check="not [light]">Switch on the light<state name="light" value="true"/></choice>
<choice station="start" check="[light]">Switch off the light<state name="light" value="false"/></choice>

Elements used: text | choice

Password

Here the player can enter a password in the first station. It's validated in the second:

<station id="start">
<text>The guard looks at you suspiciously. "What is the password?"</text>
<input station="answer">Answer</input>
</station>

<station id="answer">
<if check="[qmlInput] = 'silver'">
    <text>"That's right."<break/>
    The guard opens the door.</text>
</if>
<else>
    <text>"That's wrong." says the guard.</text>
</else>
</station>

Elements used: text | choice | if | else | input

Picking up a key

The following creates a room with two exits and a key that can be picked up:

<text>You're in a cozy room. </text>
<text check="not [has key]">There's a key lying on the floor. </text>
<text>Exits lead east and west.</text>
<choice station="start" check="not [has key]">You pick up the key<state name="has key"/></choice>
<choice station="east room">You go east</choice>
<choice station="west room">You go west</choice>

Elements used: text | choice

Picking up a key (verbose)

The following creates a room with two exits and a key that can be picked up. As opposed to the previous example, this time there's an additional station describing the picking up action and the key:

<station id="start">
<text>You're in a cozy room. </text>
<text check="not [has key]">There's a key lying on the floor. </text>
<text>Exits lead east and west.</text>
<choice station="get key" check="not [has key]">You pick up the key</choice>
<choice station="east room">You go east</choice>
<choice station="west room">You go west</choice>
</station>

<station id="get key">
<text>You pick up the shiny golden key and put it in your pocket.</text>
<state name="has key"/>
<choice station="back">Continue</choice>
</station>

Elements used: text | choice | state

Knocking on a door

Here we create a scene where a door will only open after the player knocked 3 times:

<station id="start">
<text>There's a door here.
</text>
<choice station="knock on door">You knock<number name="knocks" value="[knocks] + 1"/></choice>
<choice station="path">You leave</choice>
</station>

<station id="knock on door">
<if check="[knocks] lower 3">
    <text>You knock on the door, but nobody opens.</text>
    <choice station="back">Continue</choice>
</if>
<else>
    <text check="">You knock on the door again, and finally someone opens.</text>
    <choice station="inside house">You enter</choice>
</else>
</station>

Elements used: text | choice | if | else | state

Rolling a dice


The following rolls a dice and uses an image to show the result. (The images are already contained within the standard QML package.) The text-attribute used in the image is for alternative displays without graphic capabilities.

<number name="dice" value="{random 1, 6}"/>
<text>You roll the dice:
</text>
<image source="media/dice/[dice].gif" text="[dice]"/>

Elements used: text | choice | image

Several items needed

The following sample station checks if the player has the 3 items book, pencil, and key (the state name "has book" could as well be "picked up book", "book" and so on):

<if check="[has book] and [has pencil] and [has key]">
    <text>You got all the necessary items.
    </text>
    <choice station="room number 2">Continue</choice>
</if>
<else>
    <text>You lack certain needed items.
    </text>
    <choice station="room number 3">Continue</choice>
</else>

Elements used: text | choice | if | else

Battle system

Here, there's the two opponents player and enemy. Both have stamina and skill. A random number is added to both opponents skill value and stored as opponent power; whoever loses in a comparison of those numbers has points subtracted from the stamina. If stamina drops below zero, the character is dead.
The sample here works in a reusable fashion, as only opponent skill and continuing station after a win have to be defined every time.

<station id="start">
  <text>There's a big monster in front of you blocking the path.
  </text>

  <number name="player skill" value="10"/>
  <number name="player stamina" value="10"/>
  
  <number name="enemy skill" value="10"/>
  <number name="enemy stamina" value="10"/>

  <string name="after win" value="new path"/>

  <choice station="battle">Let's start the battle</choice>
</station>

<station id="battle">
  <number name="player power" value="[player skill] + {random 2, 12}"/>
  <number name="enemy power" value="[enemy skill] + {random 2, 12}"/>

  <if check="[player power] = [enemy power]">
    <text>Both of your swords miss the other. Stamina stays the same.</text>
    <choice station="battle">Battle again</choice>
  </if>
  <if check="[player power] greater [enemy power]">
    <text>You hit the enemy and wound him.</text>
    <number name="enemy stamina" value="[enemy stamina] - 2"/>
    <choice station="battle result">Continue</choice>
  </if>
  <else>
    <text>The enemy hits you and it hurts.</text>
    <number name="player stamina" value="[player stamina] - 2"/>
    <choice station="battle result">Continue</choice>
  </else>
</station>

<station id="battle result">
  <if check="[player stamina] lower 1">
    <text>The enemy wounds you deadly. Game over.</text>
  </if>
  <if check="[enemy stamina] lower 1">
    <text>You kill the enemy. You can continue your game.</text>
    <choice station="[after win]">You continue</choice>
  </if>
  <else>
    <text>Both of you are still standing.
    Your Stamina is [player stamina], the enemy stamina
    is [enemy stamina].
    </text>
    <choice station="battle">Battle again</choice>
  </else>
</station>

<station id="new path">
  <text>You're on a new path, leaving the monster behind you...
    <break/><emphasis>This is just the beginning of the quest,
    but this sample is over.</emphasis>
  </text>
</station>
Battle sample in PHP-Interpreter (any browser)
Batlle sample in VBScript-Interpreter (IExplorer 5+).