Choice branches
- Game Development course
- Advanced
- Lesson 34/71
- 4 min
Learn branching dialogue flow by choice.
Branching Dialogue Choices
In role-playing and adventure games, dialogues do not simply run along a single path; players are presented with choices. Each Choice contains the display text and the destination node identifier (targetNodeId).
[System.Serializable]
public class DialogueChoice {
public string choiceText;
public int targetNodeId;
}When a choice button is clicked, execution diverges from linear progression and branches to the designated node.
Dialogue Graph and Node Jumping
When nodes interconnect via choices, a dialogue graph emerges. Upon player selection, the runner resolves the targetNodeId and begins playback of that node's lines.
public void SelectChoice(int targetNodeId) {
currentNode = FindNodeById(targetNodeId);
currentIndex = 0;
ShowCurrentLine();
}Through this branching mechanism, quests and narrative consequences unfold dynamically based on player decisions.
Practice Choice branch routing
Define a Start method. Inside, declare int hedefDugum = 5; and print "Secim yapildi, hedef dugum: " + hedefDugum to the console.
Starter code
using UnityEngine;
public class ChoiceRouter : MonoBehaviour
{
// Start icinde hedefDugum tanimla ve yazdir
}Sample solution
using UnityEngine;
public class ChoiceRouter : MonoBehaviour
{
void Start()
{
int hedefDugum = 5;
Debug.Log("Secim yapildi, hedef dugum: " + hedefDugum);
}
}Expected output
Secim yapildi, hedef dugum: 5
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What are the most essential attributes a choice must possess in a branching dialogue system?
- Graphics card fan speed
- The text shown to the player and the target node ID it leads to Correct answer
- Save file path and size
- Character jump height and speed
Explanation: Choices encapsulate the text displayed to the player and the target identifier (targetNodeId) indicating which node to jump to.
2 Choice routing
public void SelectChoice(int ____) { currentNodeId = targetNodeId; }
public void SelectChoice(int targetNodeId) { currentNodeId = targetNodeId; }
Explanation: The 'targetNodeId' parameter is used to specify the destination node of the chosen branch.
3 Branching concepts
| Term | Meaning |
|---|---|
Choice |
Dialogue option the player can click |
Branch |
Conversation route branching upon choice |
targetNodeId |
Target node ID to jump to on selection |
Dialogue Graph |
Network of interconnected dialogue nodes |