Branching dialogue?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Anastasia

Here’s the code im starting with

 extends RichTextLabel

var dialog 
var page 


func _ready():
	dialog = ["Hello player!", "How are you?" ]
	page = 0
	set_bbcode(dialog[page])
	set_visible_characters(0)
	set_process_input(true)


	

func _input(event):
	if Input.is_action_pressed("ui_accept"):
		if get_visible_characters() > get_total_character_count():
			if page < dialog.size()-1:
				page += 1
				set_bbcode(dialog[page])
				set_visible_characters(0)



func _on_Timer_timeout():
	set_visible_characters(get_visible_characters()+1)

I have two buttons that ‘branch’ the dialogue: i call them A and B

Here’s my aproach:

extends RichTextLabel

var BRANCH_1 = false
var BRANCH_2 = false
var dialog 
var page 
var start = true
var A 
var B




func _ready():
	A = get_node("../1")
	B = get_node("../2")
	A.hide()
	B.hide()
	if start == true:
		dialog = ["Hello player!", "How are you?" ]
		page = 0
	set_bbcode(dialog[page])
	set_visible_characters(0)
	set_process_input(true)
	
	if start == false:
		if BRANCH_1 == true:
			page = 0
			dialog = ["oh thats great!", "Awsome!"]
		if BRANCH_2 == true:
			page = 0
			dialog = ["That's fine too!","hold on buddy"]
	
# warning-ignore:unused_argument
func _input(event):
	if Input.is_action_pressed("ui_accept"):
		if get_visible_characters() > get_total_character_count():
			if page < dialog.size()-1:
				page += 1
				set_bbcode(dialog[page])
				set_visible_characters(0)
	if start == true:
		if page == 1:
			A.show()
			B.show()
		else:
			A.hide()
			B.hide()
	


func _on_Timer_timeout():
	set_visible_characters(get_visible_characters()+1)




func _on_1_pressed():
	#Great!
	start = false
	BRANCH_1 = true


func _on_2_pressed():
	#Ok
	start = false
	BRANCH_2 = true

If you got any diferent solutions or an idea why this doesnt work, please!

Could you describe your problem? You say it doesn’t work, but not what happens.

Basic troubleshooting:

  • Are _on_Timer_timeout, _on_1_pressed and _on_2_pressed properly connected?

  • You handle switching between branches in the _ready function, which only runs when the node first enters the tree. Either you forgot to move it into _process, or you skipped the tutorials: Scripting (continued) — Godot Engine (3.2) documentation in English

  • In the _input function, you only check if ui_accept was pressed to switch pages. Looks like you could just press enter when the buttons show and completely skip the choice.

Bernard Cloutier | 2020-02-26 16:55

:bust_in_silhouette: Reply From: 255h

Hey!
Well i would suggest you to go with something more flexible with this.

You can create custom data structures that hold your game data (texts,values e.t.c).
For dialogue data structure i would go for something like:

extends Resource
class_name Replica

export(String) 	    var Text 
export(Dictionary)  var Answers

Once you have your data structure, you now can populate your custom resource with this structure via New resource->Replica (name given after class_name keyword) . And then maintain it.
enter image description here
This will allow you to construct your dialogs in flexible manner and keep em organised . (and no hardcoding yay)

Then you need the way to visualise and process them.

To access dialogue you created in editor you can just load it as normal resource:

var  dialogue = load("res://dialogue_01.tres");

And then access it’s components just by names.

Well general idea on visualising this stuff:

extends Node

onready var DialogueText = get_node("Text")
onready var Answer_1       = get_node("Answer_1")
onready var Answer_2       = get_node("Answer_2")
onready var Answer_3       = get_node("Answer_3")
onready var Answer_4       = get_node("Answer_4")

func _ready():
    var dialogue = load("res://Dialogue_01.tres")
    set_dialogue(dialogue)

func set_dialogue(dialogue):
    DialogueText.text = dialogue.Text

    var answers = dialogue.Answers.keys()
    Answer_1.text = answers[1]
    Answer_2.text = answers[2]
    Answer_3.text = answers[3]
    Answer_4.text = answers[4]

Can be improved with using some loops and dynamic button instantiation :slight_smile: