Skip to content

Instantly share code, notes, and snippets.

@BrianMacIntosh
Last active October 16, 2021 05:34
Show Gist options
  • Save BrianMacIntosh/dae7e9870e29632271cfcc66f45f1403 to your computer and use it in GitHub Desktop.
Save BrianMacIntosh/dae7e9870e29632271cfcc66f45f1403 to your computer and use it in GitHub Desktop.
Bare-bones example of using the Dialogue Designer Unity package (https://github.com/BrianMacIntosh/DialogueDesignerUnity)
using UnityEngine;
// Include the DialogueDesigner namespace
using DD;
/// <summary>
/// Complete example that plays a Dialogue Designer dialogue in the unity debug console.
/// Use the number keys to make choices.
/// </summary>
public class DialogueDesignerExample : MonoBehaviour
{
// The dialogue to play
// Drop the conversation file asset in here using the Inspector
public TextAsset DialogueFile;
// The conversation, parsed and loaded at runtime
private Dialogue m_loadedDialogue;
// A player that can track progress through the dialogue
private DialoguePlayer m_dialoguePlayer;
void Start()
{
// load the dialogue
m_loadedDialogue = Dialogue.FromAsset(DialogueFile);
// create a player to play through the dialogue
m_dialoguePlayer = new DialoguePlayer(m_loadedDialogue);
// set up your unique code to display dialogues
DialoguePlayer.GlobalOnShowMessage += OnShowMessage;
DialoguePlayer.GlobalOnEvaluateCondition += OnEvaluateCondition;
DialoguePlayer.GlobalOnExecuteScript += OnExecuteScript;
// if you want to handle a particular dialogue differently, you can use these instead
//m_dialoguePlayer.OverrideOnShowMessage += OnShowMessageSpecial;
//m_dialoguePlayer.OverrideOnEvaluateCondition += OnEvaluateConditionSpecial;
//m_dialoguePlayer.OverrideOnExecuteScript += OnExecuteScriptSpecial;
m_dialoguePlayer.OnDialogueEnded += OnDialogueEnded;
// start playing the dialogue
m_dialoguePlayer.Play();
}
private void OnDialogueEnded(DialoguePlayer sender)
{
Debug.Log("[Dialogue Ended]");
}
void Update()
{
// don't forget to update all players every frame if you want to use Wait nodes
m_dialoguePlayer.Update();
// wait for player input on any Show Message Node, then advance
ShowMessageNode showMessageNode = m_dialoguePlayer.CurrentNode as ShowMessageNode;
if (showMessageNode != null)
{
ShowMessageNodeChoice choiceNode = showMessageNode as ShowMessageNodeChoice;
if (choiceNode != null)
{
// the player must make a choice with the number keys
int numberInput = GetAlphaNumberDown();
Debug.LogWarning(numberInput);
if (numberInput >= 1)
{
m_dialoguePlayer.AdvanceMessage(numberInput - 1);
}
}
else
{
// the player must press 1 to proceed to the next node
if (Input.GetKeyDown(KeyCode.Alpha1))
{
m_dialoguePlayer.AdvanceMessage(0);
}
}
}
}
private void OnShowMessage(DialoguePlayer sender, ShowMessageNode node)
{
// display the text of the node
Debug.Log(node.GetText("ENG"));
ShowMessageNodeChoice choiceNode = node as ShowMessageNodeChoice;
if (choiceNode != null)
{
// choice nodes have multiple choices for the player to choose from
for (int i = 0; i < choiceNode.Choices.Length; i++)
{
Debug.LogFormat("{0}.) {1}", i + 1, choiceNode.GetChoiceText(i, "ENG"));
}
}
else
{
// non-choice nodes only have one option
Debug.Log("1.) Continue");
}
}
private bool OnEvaluateCondition(DialoguePlayer sender, string script)
{
// You may parse and evaluate a condition string however you like
// This is left as an exercise for the reader
return false;
}
private void OnExecuteScript(DialoguePlayer sender, string script)
{
// You may parse and execute a script string however you like
// This is left as an exercise for the reader
}
/// <summary>
/// Helper function - returns the number key the player just pressed down, or -1 if none.
/// </summary>
private int GetAlphaNumberDown()
{
for (int i = 0; i < 10; i++)
{
if (Input.GetKeyDown(KeyCode.Alpha0 + i))
{
return i;
}
}
return -1;
}
}
@BrianMacIntosh
Copy link
Author

It's super minor but Line 37: ONEvaluateConditionSpecial should be OnEvaluateConditionSpecial

Thanks! Fixed it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment