Scripting dialogues

Dialogue trees can be scripted using a few commands. First, you need to fill-in the available responses; it's done using the Game.AddResponse() method. When calling this command, you specify the ID of each response and the text. Optionally, if you are using the iconic responses, you can specify three sprites for each response, for normal, hovered and pressed response button.

Once you've filled all the possible responses, you'll call the Game.GetResponse() method. It will display the response box on screen and let the player select one of the responses. The script is suspended until the player make their choice. Then the ID of the selected response is returned.

A very simple dialog code would look like this:

Game.AddResponse(0, "Hello.");
Game.AddResponse(1, "How are you?");
Game.AddResponse(2, "I'll be going.");

var Res = Game.GetResponse();

This code is nice, but mostly useless. In most cases you will want to check the returned response and do some actions. Also, typically you will want to display the responses until the player selects the "I'll be going" option. The improved code will look like this:

function MyTestDialogue()
{
  var EndBranch = false;

  while(!EndBranch) // loop until the player ends this dialogue
  {
    // fill in the responses
    Game.AddResponse(0, "Hello.");
    Game.AddResponse(1, "How are you?");
    Game.AddResponse(2, "I'll be going.");

    // let the player choose one
    var Res = Game.GetResponse();

    // let the actor say the selected response
    actor.Talk(Game.LastResponse);

    // and now handle the selected one
    switch(Res)
    {
      case 0:
        // some code to handle response #0
        SomeGuy.Talk("Hi mister.");
        break;

      case 1:
        // some code to handle response #1
        SomeGuy.Talk("I'm fine, thank you.");
        break;

      case 2:
        SomeGuy.Talk("Bye bye.");
        EndBranch = true;  // response #2 exits the dialogue
        break;        
    }
  }
}

Ok, now it's slightly more complicated, but it's no rocket science. First, notice that we enclosed the entire dialogue into a function. It's a good idea to create one function for each dialogue branch. You can then simply call one branch from another, and the code is more readable.

Secondly, we added a line that instructs the player to say the selected response text. This text is conveniently stored in the Game.LastResponse property.

Thirdly, the response selection is enclosed in a "while" loop, which loops until the third dialogue option is selected.

And lastly, we added the code for handling the selected response. The "switch" command is probably the most elegant way of doing so. You can use the code above as a template for your own dialogue functions.

 

In addition to the simplest Game.AddResponse() method, WME provides also its special variations Game.AddResponseOnce() and Game.AddResponseOnceGame().

Game.AddResponseOnce() method will add a dialogue response and once this response is selected by the player, it will disappear from the list of responses until the dialogue is finished.
 

Game.AddResponseOnceGame() method will add a dialogue response and once this response is selected by the player, it will disappear from the list of responses and it will never reappear in the current game session.

Using these two methods you don't need to explicitly code whether a certain response should appear only once. The engine will remember which responses have been previously selected by the player and it will hide them automatically.

If you need to revive an automatically hidden response, you can use Game.ResetResponse() method.

 

As an addition to this functionality the Game.GetResponse() method has an optional logical parameter, which specifies what to do if there's only one response left for player to select. If you pass a true value as a parameter to the GetResponse method and there is only one response available, the engine will select it automatically.

But there's a small complication. In order to make the Game.AddResponseOnce* methods work, you must explicitly define where your dialogue branch starts and where it ends so that the engine is able to track to which dialogue which response belongs. You can mark a dialogue start using the Game.StartDlgBranch() method and end it using a Game.EndDlgBranch() method.

OK, let's show the complete dialogue scripting in an example:

function MyTestDialogue()
{
  Game.StartDlgBranch("MyTestDialogue"); // start a new dialogue branch

  var EndBranch = false;

  while(!EndBranch) // loop until the player ends this dialogue
  {  
    Game.AddResponseOnce(0, "You can select this response only once in this dialogue");
    Game.AddResponseOnceGame(1, "You can select this response only once in the entire game");
    Game.AddResponse(2, "Ok, I have to go now.");
    
    var Res = Game.GetResponse(true); // automatically select last response
    
    actor.Talk(Game.LastResponse);
    
    // handle the responses
    switch(Res)
    {
      case 0:
        // some code to handle response #0
        break;

      case 1:
        // some code to handle response #1
        break;

      case 2:
        EndBranch = true;  // response #2 exits the dialogue
        break;        
    }
  }
  
  Game.EndDlgBranch(); // end this dialogue branch
}


There are now two third-party visual editors available for building WME dialogues. Try them, maybe you'll find them helpful: