Controlling Dialogue from Code

Start and stop conversations, pause your game, and react to what happens in them.

The Quickstart gets a character talking without code. Once your game needs more, like picking a conversation from quest progress, freezing the player while they talk, or playing an animation on a certain line, you do it from your own scripts.

All of it goes through one static class, Poetix, in the PoetixSDK namespace. It talks to the scene's Dialogue Manager for you, so there's nothing to drag into a field first. It works the same with the ready-made Dialogue Box and with your own dialogue UI.

The scene needs a Dialogue Manager, which every Dialogue Box already has. Make your first calls in Start, not Awake: Awake can run before the Dialogue Manager is ready, and Poetix then throws an error saying so.

How a conversation runs#

A conversation is one path through your story:

  1. It starts at a beat, either one you name or the story's root beat.
  2. Each line whose conditions pass is shown, one at a time. Its results are applied as it's shown, and {{variables}} in its text are already filled in.
  3. After the beat's last line, its choices appear. When the player picks one, its results are applied and the conversation moves to the beat it leads to. Auto-choice beats pick for themselves.
  4. The conversation ends when it reaches a choice that leads nowhere, or a beat with nowhere left to go.

Ending a conversation doesn't reset anything. The blackboard keeps its values, so the next conversation, with the same character or another one, sees what happened in this one. That's how a story written as many short conversations still feels like one story. See Branching with Beats & Choices for how this looks on the canvas.

Starting a conversation#

using PoetixSDK;

Poetix.Play("Campfire Greeting");   // start from a beat, by its name
Poetix.Play();                      // start from the story's root beat
Poetix.Play(otherStory, "Intro");   // play another story (a StoryAsset)

Beat names are the names you gave beats in Poetix. A common use is choosing the conversation from the story's variables:

using PoetixSDK;
using UnityEngine;

public class Kael : MonoBehaviour
{
    public void Talk()
    {
        if (Poetix.GetVar<bool>("Has Relic"))
            Poetix.Play("Kael - The Relic");
        else
            Poetix.Play("Kael - Campfire Greeting");
    }
}

Play returns false and does nothing while a conversation is already playing, so an interact key pressed twice can't restart it. To replace the current conversation on purpose, pass restart: true.

To stop a conversation early, for example when the character is attacked or the player walks away, call Poetix.Stop().

Beat names without typos#

A beat name typed as text breaks silently if misspelled or is renamed in Poetix. To catch wrong names while you code, in the story Inspector in Unity click Generate under “Constants for Code” to create a constants file in Assets/Poetix/Generated/. It has one class each for the story's beats, variables, and characters, named after the story file. For ForsakenOutpost.poetix:

using PoetixSDK.Generated;

Poetix.Play(ForsakenOutpostBeats.KaelCampfireGreeting);
int gold = Poetix.GetVar<int>(ForsakenOutpostVariables.Gold);

The file updates by itself whenever the story does. A renamed or deleted beat then becomes a compile error pointing at the exact line, instead of a conversation that never starts.

The class names are taken from the file name when you click Generate, and stay the same after that. A file added with a sync key is named after its version too (Forsaken_Outpost_-_Live_draft gives ForsakenOutpostLiveDraftBeats), so for shorter names, rename the file before generating. Renaming a story file is safe: scenes keep using it.

The story Inspector's Constants for Code section.

Starting from a Dialogue Starter#

If your game has its own interaction system, such as raycasts, a hover cursor, or controller focus, you can still let a Dialogue Starter remember which story and beat a character uses. Set its Start On to Manually (call Play) and call it from your code:

GetComponent<DialogueStarter>().Play();

This way you can pick the beat in the Inspector, and your code only decides when. See Starting Dialogues Without Code for everything else a Dialogue Starter does.

Pausing your game during dialogue#

Poetix doesn't stop your player, camera, or input while a conversation is open, because games differ: some freeze the player, others let them keep walking while characters talk. Your game decides. The simplest way is to check Poetix.IsPlaying:

void Update()
{
    if (Poetix.IsPlaying) return; // no walking while a conversation is open
    // ... movement ...
}

Or react when conversations start and end, for example to hide your HUD:

void OnEnable()
{
    Poetix.Started += HideHud;
    Poetix.Ended += ShowHud;
}

void OnDisable()
{
    Poetix.Started -= HideHud;
    Poetix.Ended -= ShowHud;
}

Always unsubscribe in OnDisable, as here, so a destroyed object isn't called after it's gone. For the same thing without code, use the Dialogue Box's Inspector events.

Keeping dialogue quiet during your menus#

Every Unity script reads the keyboard on its own, so while your pause menu is open, Space would also advance the dialogue behind it. Block dialogue input while the menu is shown:

void OnEnable()  => Poetix.BlockInput(this);   // menu shown
void OnDisable() => Poetix.UnblockInput(this); // menu hidden

Each menu passes itself, so several can be open at once and dialogue input comes back only when the last one closes. A destroyed menu stops blocking by itself. While blocked, the Dialogue Box and Dialogue Starters ignore keys and clicks and auto-advance waits, but the conversation stays where it is. Typing in a UI text field blocks dialogue input automatically.

Reacting to what happens#

These events fire for the scene's conversations, however they were started:

  • Poetix.Started and Poetix.Ended: a conversation begins or ends.
  • Poetix.LineReady: a line is about to be shown. You get the whole line (a SimLine) with its Text, its SpeakerName, and whether it's the beat's last line.
  • Poetix.ChoicesReady: choices are waiting for the player, each with whether its condition passed.
  • Poetix.ChoicePicked: the player picked a choice, before its results are applied. Useful for analytics or a sound. Choices picked by auto-choice beats don't fire it.
  • Poetix.HistoryAdded: a line or a picked choice was added to the history.

For example, to play a talking animation whenever Kael speaks:

using PoetixSDK;
using PoetixSDK.Core;
using UnityEngine;

public class KaelAnimation : MonoBehaviour
{
    [SerializeField] private Animator animator;

    void OnEnable()  => Poetix.LineReady += OnLine;
    void OnDisable() => Poetix.LineReady -= OnLine;

    void OnLine(SimLine line)
    {
        animator.SetBool("Talking", line.SpeakerName == "Kael");
    }
}

To react to a story variable changing, such as gold going up or a quest flag turning on, use Poetix.WatchVar, covered in Variables, Saving & Scene Changes.

Moving on from code#

The Dialogue Box handles the player's keys and clicks. To move the conversation along yourself, for example in a cutscene timed to music:

Poetix.Continue();   // move on from the line being shown
Poetix.Choose(0);    // pick the first choice the player could pick
Poetix.Choose(choice); // or one from Poetix.CurrentChoices

Choose(int) counts only choices whose condition passed, in the order the number keys use. A choice whose condition failed can't be picked. Both calls are ignored in the frame the line or choices appeared, so one key press handled by two scripts never skips two lines.

Poetix.CurrentLine and Poetix.CurrentChoices tell you what's on screen right now.

The conversation history#

Poetix.History lists the lines shown and choices picked so far, oldest first, each with its speaker, text, and beat. Use it for a backlog or journal screen, and Poetix.ClearHistory() to empty it. How many entries it keeps is set by the Dialogue Manager's History Size.

Going further#