This commit is contained in:
2024-09-07 15:01:45 -07:00
parent fbaf698852
commit b470a9d2fe
116 changed files with 15968 additions and 24 deletions

View File

@@ -0,0 +1,49 @@
[gd_scene format=3 uid="uid://debwoww4x115m"]
[node name="IntroductionDemoMarginContainer" type="CenterContainer"]
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
[node name="IntroductionDemoVBoxContainer" type="VBoxContainer" parent="."]
layout_mode = 2
[node name="IntroductionDemoExaplanationPanelContainer" type="PanelContainer" parent="IntroductionDemoVBoxContainer"]
layout_mode = 2
[node name="IntroductionDemoExaplanationLabel" type="Label" parent="IntroductionDemoVBoxContainer/IntroductionDemoExaplanationPanelContainer"]
layout_mode = 2
text = "Basic dialogue processing is composed of two parts:"
horizontal_alignment = 1
[node name="IntroductionDemoPreparationLabel" type="Label" parent="IntroductionDemoVBoxContainer"]
layout_mode = 2
text = "1. Dialogue tree preparation:"
[node name="IntroductionDemoPreparationExampleCodeEdit" type="CodeEdit" parent="IntroductionDemoVBoxContainer"]
layout_mode = 2
theme_override_colors/font_readonly_color = Color(1, 1, 1, 1)
text = "extends DialogueEngine
func _setup() -> void:
add_text_entry(\"First dialogue message\")
add_text_entry(\"Second dialogue message\")
add_text_entry(\"Third dialogue message\")
# and so on"
editable = false
scroll_fit_content_height = true
symbol_lookup_on_click = true
auto_brace_completion_highlight_matching = true
[node name="IntroductionDemoTraversalLabel" type="Label" parent="IntroductionDemoVBoxContainer"]
layout_mode = 2
text = "2. And tree traversal:"
[node name="IntroductionDemoTraversalExamplePanelContainer" type="PanelContainer" parent="IntroductionDemoVBoxContainer"]
layout_mode = 2
[node name="IntroductionDemoTraversalExampleLabel" type="Label" parent="IntroductionDemoVBoxContainer/IntroductionDemoTraversalExamplePanelContainer"]
layout_mode = 2
text = "dialogue_engine.advance() # called when convenient"

View File

@@ -0,0 +1,8 @@
extends DialogueEngine
func _setup() -> void:
add_text_entry("Hey...")
add_text_entry("[i]Have [i][b]you[/b][/i] seen the code for this sample?[/i]")
add_text_entry("[rainbow freq=1.0 sat=0.4 val=0.8]It's beautiful![/rainbow]")
add_text_entry("[i][shake rate=20.0 level=5 connected=1]You won't believe it![/shake][/i]")
add_text_entry("[code][i]Press <Enter> or <Space> to exit.[/i][/code]")

View File

@@ -0,0 +1,33 @@
extends VBoxContainer
@export var dialogue_gdscript : GDScript = null
var dialogue_engine : DialogueEngine = null
func _ready() -> void:
dialogue_engine = dialogue_gdscript.new()
dialogue_engine.dialogue_started.connect(__on_dialogue_started)
dialogue_engine.dialogue_continued.connect(__on_dialogue_continued)
dialogue_engine.dialogue_finished.connect(__on_dialogue_finished)
func _input(p_input_event : InputEvent) -> void:
if p_input_event.is_action_pressed(&"ui_accept"):
dialogue_engine.advance()
func __on_dialogue_started() -> void:
print("Dialogue Started!")
func __on_dialogue_continued(p_dialogue_entry : DialogueEntry) -> void:
var label : RichTextLabel = RichTextLabel.new()
label.set_use_bbcode(true)
label.set_fit_content(true)
label.set_text(" > " + p_dialogue_entry.get_text())
add_child(label)
func __on_dialogue_finished() -> void:
print("Dialogue Finished! Exiting...")
get_tree().quit()

View File

@@ -0,0 +1,26 @@
[gd_scene load_steps=3 format=3 uid="uid://bl1ul621fkvxw"]
[ext_resource type="Script" path="res://demos/1. simple dialogue/simple_log.gd" id="1_km05v"]
[ext_resource type="Script" path="res://demos/1. simple dialogue/simple_dialogue.gd" id="2_clmvy"]
[node name="DialogueEngineDemoVBoxContainer" type="VBoxContainer"]
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
script = ExtResource("1_km05v")
dialogue_gdscript = ExtResource("2_clmvy")
[node name="DialogueEngineDemoQuickStartPanelContainer" type="PanelContainer" parent="."]
layout_mode = 2
[node name="DialogueEngineDemoQuickStartLabel" type="Label" parent="DialogueEngineDemoQuickStartPanelContainer"]
layout_mode = 2
text = "Press <Enter> or <Space> to progress the dialogue."
horizontal_alignment = 1
[node name="LogRichTextLabel" type="RichTextLabel" parent="."]
layout_mode = 2
text = "Log:"
fit_content = true

View File

@@ -0,0 +1,56 @@
extends DialogueEngine
const SAVE_PATH : String = "user://save.dat"
var counter : int = 0
var start_counting_id : int = 0
var log_history : Array = []
func _setup() -> void:
add_text_entry("This is an example of an infinite dynamically generated/saved/loaded dialogue.")
add_text_entry("You can save the dialogue progress at any time by clicking the save button above.")
add_text_entry("And when you restart this scene, the dialogue will continue from where it left off.")
add_text_entry("As the dialogue progresses, the graph in the debugger will update automatically as well.")
add_text_entry("Let's count to infinity!!")
dialogue_continued.connect(__log_history)
dialogue_about_to_finish.connect(__continue_counting)
# Load previous state if any
load_state()
func __log_history(p_dialogue_entry : DialogueEntry) -> void:
# Always track the log history:
log_history.push_back(p_dialogue_entry.get_formatted_text())
func __continue_counting() -> void:
counter += 1
add_text_entry(str(counter))
func save_state() -> void:
var file_handle: FileAccess = FileAccess.open(SAVE_PATH, FileAccess.WRITE)
file_handle.store_var(counter)
file_handle.store_var(get_current_entry().get_id())
file_handle.store_var(log_history)
print("State Saved")
func load_state() -> void:
if FileAccess.file_exists(SAVE_PATH):
var file_handle: FileAccess = FileAccess.open(SAVE_PATH, FileAccess.READ)
counter = file_handle.get_var()
var entry_id : int = file_handle.get_var()
if has_entry_id(entry_id):
set_current_entry(entry_id)
else:
set_current_entry(add_text_entry("Let's continue counting!!").get_id())
log_history = file_handle.get_var()
print("State Loaded")
func clear_state() -> void:
if FileAccess.file_exists(SAVE_PATH):
DirAccess.remove_absolute(SAVE_PATH)
print("State Cleared")

View File

@@ -0,0 +1,140 @@
extends Control
@export var dialogue_gdscript : GDScript = null
var dialogue_engine : DialogueEngine = null
@onready var dialogue : VBoxContainer = $VBox/Dialogue
@onready var history : CenterContainer = $History
@onready var history_log : VBoxContainer = $History/Panel/Margin/HistoryLog
@onready var animator : AnimationPlayer = $Animator
func _ready() -> void:
dialogue_engine = dialogue_gdscript.new()
dialogue_engine.dialogue_started.connect(__on_dialogue_started)
dialogue_engine.dialogue_continued.connect(__on_dialogue_continued)
dialogue_engine.dialogue_finished.connect(__on_dialogue_finished)
dialogue_engine.dialogue_cancelled.connect(__on_dialogue_cancelled)
func _input(p_input_event : InputEvent) -> void:
if p_input_event.is_action_pressed(&"ui_accept"):
if animator.is_playing():
# Player is inpatient -- auto-advance the text
var animation_name : StringName = animator.get_current_animation()
var animation : Animation = animator.get_animation(animation_name)
animator.advance(animation.get_length()) # this will fire the animation_finished signal automatically else:
else:
# Advance current entry
dialogue_engine.advance()
accept_event() # accepting input event here to stop it from traversing into into buttons possibly added through the interaction
func __on_dialogue_started() -> void:
print("Dialogue Started!")
func __on_dialogue_continued(p_dialogue_entry : DialogueEntry) -> void:
# Add the text to the log:
var label : RichTextLabel = RichTextLabel.new()
label.set_use_bbcode(true)
label.set_fit_content(true)
if p_dialogue_entry.has_metadata("author"):
var author : String = p_dialogue_entry.get_metadata("author")
label.set_text(" > " + author + ": " + p_dialogue_entry.get_formatted_text())
else:
label.set_text(" > " + p_dialogue_entry.get_formatted_text())
dialogue.add_child(label, true)
# Setup the animation:
animator.stop(true) # internally some timers do not reset properly unless we do this
if not animator.has_animation_library(&"demo"):
var new_animation_library : AnimationLibrary = AnimationLibrary.new()
animator.add_animation_library(&"demo", new_animation_library)
var animation_library : AnimationLibrary = animator.get_animation_library(&"demo")
var animation : Animation = create_visible_characters_animation_per_character(label.get_text(), 0.045, true)
animator.set_root_node(label.get_path())
animation_library.add_animation(&"dialogue", animation)
animator.play(&"demo/dialogue")
func __on_dialogue_finished() -> void:
print("Dialogue Finished! Exiting...")
get_tree().quit()
func __on_dialogue_cancelled() -> void:
print("Dialogue Cancelled! Exiting...")
get_tree().quit()
func __on_animation_started(p_animation_name : StringName) -> void:
print("Animation started:", p_animation_name)
func create_visible_characters_animation_per_character(p_text : String, p_time_per_character : float, p_instant_first_character : bool = false, p_time_whitespace : bool = false) -> Animation:
# Do initial calculations
var whitespace_regex : RegEx
if not p_time_whitespace or p_instant_first_character:
whitespace_regex = RegEx.new()
whitespace_regex.compile("\\s")
# Create animation and track
var animation : Animation = Animation.new()
var track_index : int = animation.add_track(Animation.TYPE_VALUE)
animation.track_set_path(track_index, ".:visible_characters")
animation.track_set_interpolation_type(track_index, Animation.INTERPOLATION_LINEAR)
# Configure keys
var total_time : float = 0.0
var total_visible_characters : int = 0
var whitespace_time_offset : float = 0.0
animation.track_insert_key(track_index, total_time, 0)
var total_animation_length : float = 0.0
for character : String in p_text:
total_time += p_time_per_character
total_visible_characters += 1
if not p_time_whitespace and whitespace_regex.sub(character, "", true).is_empty():
whitespace_time_offset += p_time_per_character
continue
total_animation_length = total_time - whitespace_time_offset
animation.track_insert_key(track_index, total_animation_length, total_visible_characters)
animation.set_length(total_animation_length)
if p_instant_first_character:
if animation.track_get_key_count(track_index) > 0:
# Shift all the keys back in time according to the time it took per character
for key_index : int in animation.track_get_key_count(track_index):
var key_time : float = animation.track_get_key_time(track_index, key_index)
animation.track_set_key_time(track_index, key_index, key_time - p_time_per_character)
animation.set_length(total_animation_length - p_time_per_character)
return animation
func _on_history_log_toggled(p_should_be_visible : bool) -> void:
# Free all previous history labels
for child: Node in history_log.get_children():
child.queue_free()
history.visible = p_should_be_visible
if p_should_be_visible:
@warning_ignore("unsafe_method_access")
var log_history : Array = dialogue_engine.get_log_history()
for text : String in log_history:
var label: Label = Label.new()
label.text = text
history_log.add_child(label)
func _on_save_pressed() -> void:
@warning_ignore("unsafe_method_access")
dialogue_engine.save_state()
get_tree().quit()
func _on_clear_pressed() -> void:
@warning_ignore("unsafe_method_access")
dialogue_engine.clear_state()
get_tree().quit()

View File

@@ -0,0 +1,84 @@
[gd_scene load_steps=3 format=3 uid="uid://b0gsgxefiut4g"]
[ext_resource type="Script" path="res://demos/10. dynamic save and load/dynamic_save_load.gd" id="1_75aob"]
[ext_resource type="Script" path="res://demos/10. dynamic save and load/dynamic_save_dialogue.gd" id="2_lqhmn"]
[node name="DynamicSaveLoad" type="Control"]
layout_mode = 3
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
script = ExtResource("1_75aob")
dialogue_gdscript = ExtResource("2_lqhmn")
[node name="VBox" type="VBoxContainer" parent="."]
layout_mode = 1
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
[node name="DialogueEngineDemoQuickStartPanelContainer" type="PanelContainer" parent="VBox"]
layout_mode = 2
[node name="DialogueEngineDemoQuickStartLabel" type="Label" parent="VBox/DialogueEngineDemoQuickStartPanelContainer"]
layout_mode = 2
text = "Press <Enter> or <Space> to progress the dialogue."
horizontal_alignment = 1
[node name="Buttons" type="HBoxContainer" parent="VBox"]
layout_mode = 2
[node name="Save" type="Button" parent="VBox/Buttons"]
layout_mode = 2
text = "Save and Quit"
[node name="Clear" type="Button" parent="VBox/Buttons"]
layout_mode = 2
text = "Clear State and Quit"
[node name="HistoryLog" type="Button" parent="VBox/Buttons"]
layout_mode = 2
toggle_mode = true
text = "History Log"
[node name="Dialogue" type="VBoxContainer" parent="VBox"]
layout_mode = 2
[node name="LogRichTextLabel" type="RichTextLabel" parent="VBox/Dialogue"]
layout_mode = 2
text = "Log:"
fit_content = true
[node name="History" type="CenterContainer" parent="."]
visible = false
layout_mode = 1
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
mouse_filter = 2
[node name="Panel" type="PanelContainer" parent="History"]
layout_mode = 2
[node name="Margin" type="MarginContainer" parent="History/Panel"]
layout_mode = 2
theme_override_constants/margin_left = 16
theme_override_constants/margin_top = 16
theme_override_constants/margin_right = 16
theme_override_constants/margin_bottom = 16
[node name="HistoryLog" type="VBoxContainer" parent="History/Panel/Margin"]
layout_mode = 2
[node name="Animator" type="AnimationPlayer" parent="."]
root_node = NodePath("../VBox")
[connection signal="pressed" from="VBox/Buttons/Save" to="." method="_on_save_pressed"]
[connection signal="pressed" from="VBox/Buttons/Clear" to="." method="_on_clear_pressed"]
[connection signal="toggled" from="VBox/Buttons/HistoryLog" to="." method="_on_history_log_toggled"]

View File

@@ -0,0 +1,107 @@
extends VBoxContainer
@export var dialogue_gdscript : GDScript = null
var dialogue_engine : DialogueEngine = null
@onready var progress_bar : ProgressBar = $ProgressBar
@onready var vbox : VBoxContainer = $VBox
func _ready() -> void:
dialogue_engine = dialogue_gdscript.new()
dialogue_engine.dialogue_started.connect(__on_dialogue_started)
dialogue_engine.dialogue_continued.connect(__on_dialogue_continued)
dialogue_engine.dialogue_finished.connect(__on_dialogue_finished)
dialogue_engine.dialogue_cancelled.connect(__on_dialogue_cancelled)
func _input(p_input_event : InputEvent) -> void:
if p_input_event.is_action_pressed(&"ui_accept"):
dialogue_engine.advance()
accept_event() # to avoid hidding an button due to the input event travelling through the children
func __on_dialogue_started() -> void:
print("Dialogue Started!")
var enabled_buttons : Array[Button] = []
func __on_dialogue_continued(p_dialogue_entry : DialogueEntry) -> void:
var label : RichTextLabel = RichTextLabel.new()
label.set_use_bbcode(true)
label.set_fit_content(true)
label.set_text(" > " + p_dialogue_entry.get_text())
vbox.add_child(label)
if p_dialogue_entry.has_options():
var dont_show_options : Array = p_dialogue_entry.get_metadata("dont_show_options", [])
for option_id : int in range(0, p_dialogue_entry.get_option_count()):
if option_id in dont_show_options:
continue
var option_text : String = p_dialogue_entry.get_option_text(option_id)
var button : Button = Button.new()
button.set_text(option_text)
vbox.add_child(button)
var tween: Tween = create_tween()
if option_id == 0:
button.grab_focus()
tween.tween_property(button, "modulate", Color.TRANSPARENT, 3.0)
tween.tween_callback(button.hide)
else:
# Only show other buttons after the tween finishes
button.hide()
tween.tween_callback(button.show).set_delay(5.0)
if option_id == 1:
tween.tween_callback(button.grab_focus)
tween.tween_callback(progress_bar.show)
tween.tween_method(progress_bar.set_value, 1.0, 0.0, 2.0)
# The timer has just finished
tween.tween_callback(progress_bar.hide)
tween.tween_callback(advance_dialogue_no_answer)
button.pressed.connect(__advance_dialogue_with_chosen_option.bind(option_id))
enabled_buttons.push_back(button)
set_process_input(false)
func advance_dialogue_no_answer() -> void:
for button : Button in enabled_buttons:
button.set_disabled(true)
var entry : DialogueEntry = dialogue_engine.get_current_entry()
var option_id : int = entry.get_metadata("auto_choose")
entry.choose_option(option_id)
dialogue_engine.advance()
set_process_input(true)
func __advance_dialogue_with_chosen_option(p_option_id : int) -> void:
# Kill all tweens from processing further
for tween: Tween in get_tree().get_processed_tweens():
tween.kill()
for button : Button in enabled_buttons:
button.set_disabled(true)
# Reset modulate of vanishing button
button.modulate = Color.WHITE
enabled_buttons.clear()
progress_bar.hide()
var current_entry : DialogueEntry = dialogue_engine.get_current_entry()
current_entry.choose_option(p_option_id)
dialogue_engine.advance()
set_process_input(true)
func __on_dialogue_finished() -> void:
print("Dialogue Finished! Exiting...")
get_tree().quit()
func __on_dialogue_cancelled() -> void:
print("Dialogue Cancelled! Exiting...")
get_tree().quit()

View File

@@ -0,0 +1,36 @@
[gd_scene load_steps=3 format=3 uid="uid://ctlvurl3kobjf"]
[ext_resource type="Script" path="res://demos/11. timed options/timed_options.gd" id="1_g0ote"]
[ext_resource type="Script" path="res://demos/11. timed options/timed_options_dialogue.gd" id="2_gqlp8"]
[node name="TimedOptions" type="VBoxContainer"]
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
script = ExtResource("1_g0ote")
dialogue_gdscript = ExtResource("2_gqlp8")
[node name="DialogueEngineDemoQuickStartPanelContainer" type="PanelContainer" parent="."]
layout_mode = 2
[node name="DialogueEngineDemoQuickStartLabel" type="Label" parent="DialogueEngineDemoQuickStartPanelContainer"]
layout_mode = 2
text = "Press <Enter> or <Space> to progress the dialogue."
horizontal_alignment = 1
[node name="VBox" type="VBoxContainer" parent="."]
layout_mode = 2
[node name="LogRichTextLabel" type="RichTextLabel" parent="VBox"]
layout_mode = 2
text = "Log:"
fit_content = true
[node name="ProgressBar" type="ProgressBar" parent="."]
visible = false
custom_minimum_size = Vector2(0, 16)
layout_mode = 2
max_value = 1.0
show_percentage = false

View File

@@ -0,0 +1,37 @@
extends DialogueEngine
enum {
DEFAULT_TOPIC = 0, # this is the branch used by default unless set_branch_id() is used
WATCH_THE_STORM,
GO_BACK_TO_SLEEP,
KEEP_WORKING,
}
func _setup() -> void:
var entry : DialogueEntry = add_text_entry("The storm rages right outside the window. I should...")
var option_id_1 : int = entry.add_option("Wait for storm to finish.")
var option_id_2 : int = entry.add_option("Go back to sleep.")
var option_id_3 : int = entry.add_option("Get back to work.")
var option_id_4 : int = entry.add_option("Hidden option -- this should not be shown on the UI")
entry.set_metadata("dont_show_options", [option_id_4])
entry.set_metadata("auto_choose", option_id_4)
var option_id_2_entry : DialogueEntry = add_text_entry("That's right, sleep is for the strong 💪.", GO_BACK_TO_SLEEP)
entry.set_option_goto_id(option_id_2, option_id_2_entry.get_id())
var option_id_3_entry : DialogueEntry = add_text_entry("That's right, let's get back to work 🫡", KEEP_WORKING)
entry.set_option_goto_id(option_id_3, option_id_3_entry.get_id())
var option_id_4_entry : DialogueEntry = add_text_entry("I think I'll enjoy watching the storm for a bit...", WATCH_THE_STORM)
entry.set_option_goto_id(option_id_4, option_id_4_entry.get_id())
# Join branches into the default topic (i.e. branch id 0)
var default_topic : DialogueEntry = add_text_entry("Some time passes...")
entry.set_option_goto_id(option_id_1, default_topic.get_id())
option_id_2_entry.set_goto_id(default_topic.get_id())
option_id_3_entry.set_goto_id(default_topic.get_id())
option_id_4_entry.set_goto_id(default_topic.get_id())
add_text_entry("<Press 'Space' or 'Enter' to quit>")

View File

@@ -0,0 +1,10 @@
extends DialogueEngine
func _setup() -> void:
var first_entry : DialogueEntry = add_text_entry("This is an example of...")
add_text_entry("This text will be shown on the debugger connected to branch ID 0")
add_text_entry("This text will be shown on the debugger as a separate graph node not connected to branch id 0", 1)
var first_entry_goto : DialogueEntry = add_text_entry("a skipped dialogue! Check the debugger out!")
first_entry.set_goto_id(first_entry_goto.get_id())
add_text_entry("Press <Enter> or <Space> to exit.")

View File

@@ -0,0 +1,27 @@
extends VBoxContainer
@export var dialogue_gdscript : GDScript = null
var dialogue_engine : DialogueEngine = null
func _ready() -> void:
dialogue_engine = dialogue_gdscript.new()
dialogue_engine.dialogue_continued.connect(__on_dialogue_continued)
dialogue_engine.dialogue_finished.connect(__on_dialogue_finished)
func _input(p_input_event : InputEvent) -> void:
if p_input_event.is_action_pressed(&"ui_accept"):
dialogue_engine.advance()
var enabled_buttons : Array[Button] = []
func __on_dialogue_continued(p_dialogue_entry : DialogueEntry) -> void:
var label : RichTextLabel = RichTextLabel.new()
label.set_use_bbcode(true)
label.set_fit_content(true)
label.set_text(" > " + p_dialogue_entry.get_text())
add_child(label)
func __on_dialogue_finished() -> void:
get_tree().quit()

View File

@@ -0,0 +1,26 @@
[gd_scene load_steps=3 format=3 uid="uid://dkfjywgk5gykn"]
[ext_resource type="Script" path="res://demos/2. same branch goto/same_branch_goto_log.gd" id="1_biw57"]
[ext_resource type="Script" path="res://demos/2. same branch goto/same_branch_goto_dialogue.gd" id="2_bn40h"]
[node name="DialogueEngineDemoVBoxContainer" type="VBoxContainer"]
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
script = ExtResource("1_biw57")
dialogue_gdscript = ExtResource("2_bn40h")
[node name="DialogueEngineDemoQuickStartPanelContainer" type="PanelContainer" parent="."]
layout_mode = 2
[node name="DialogueEngineDemoQuickStartLabel" type="Label" parent="DialogueEngineDemoQuickStartPanelContainer"]
layout_mode = 2
text = "Press <Enter> or <Space> to progress the dialogue."
horizontal_alignment = 1
[node name="LogRichTextLabel" type="RichTextLabel" parent="."]
layout_mode = 2
text = "Log:"
fit_content = true

View File

@@ -0,0 +1,16 @@
extends DialogueEngine
enum { DEFAULT_BRANCH = 0, DIFFERENT_BRANCH_ONE, DIFFERENT_BRANCH_TWO, DIFFERENT_BRANCH_THREE }
func _setup() -> void:
var first_entry : DialogueEntry = add_text_entry("This is an example of...", DEFAULT_BRANCH)
first_entry.set_goto_id(add_text_entry("how gotos work against different branch IDs", DIFFERENT_BRANCH_TWO).get_id())
add_text_entry("Once you jump to a different branch ID, the DialogueEngine will only consider entries in that branch ID unless you jump to a different one.", DIFFERENT_BRANCH_TWO)
add_text_entry("If, for example, you add another text entry to a branch ID that is empty, it will show up in Debugger/DialogueEngine as such.", DIFFERENT_BRANCH_TWO)
add_text_entry("For example, this text will be shown on branch ID %d in the debugger and not connected to anything. It won't show up in the interaction either." % DIFFERENT_BRANCH_ONE, DIFFERENT_BRANCH_ONE)
add_text_entry("You can also create full branches in a different branch ID", DIFFERENT_BRANCH_THREE)
add_text_entry("But since there's no jump to this branch (i.e. no goto set to this branch ID)", DIFFERENT_BRANCH_THREE)
add_text_entry("It won't show up in the interaction", DIFFERENT_BRANCH_THREE)
add_text_entry("See the auto-generated graph in Debugger/DialogueEngine.", DIFFERENT_BRANCH_TWO)
add_text_entry("Press <Enter> or <Space> to exit.", DIFFERENT_BRANCH_TWO)

View File

@@ -0,0 +1,27 @@
extends VBoxContainer
@export var dialogue_gdscript : GDScript = null
var dialogue_engine : DialogueEngine = null
func _ready() -> void:
dialogue_engine = dialogue_gdscript.new()
dialogue_engine.dialogue_continued.connect(__on_dialogue_continued)
dialogue_engine.dialogue_finished.connect(__on_dialogue_finished)
func _input(p_input_event : InputEvent) -> void:
if p_input_event.is_action_pressed(&"ui_accept"):
dialogue_engine.advance()
var enabled_buttons : Array[Button] = []
func __on_dialogue_continued(p_dialogue_entry : DialogueEntry) -> void:
var label : RichTextLabel = RichTextLabel.new()
label.set_use_bbcode(true)
label.set_fit_content(true)
label.set_text(" > " + p_dialogue_entry.get_text())
add_child(label)
func __on_dialogue_finished() -> void:
get_tree().quit()

View File

@@ -0,0 +1,26 @@
[gd_scene load_steps=3 format=3 uid="uid://clxd85by8v506"]
[ext_resource type="Script" path="res://demos/3. different branch goto/different_branch_goto_log.gd" id="1_et54s"]
[ext_resource type="Script" path="res://demos/3. different branch goto/different_branch_goto_dialogue.gd" id="2_x1mbj"]
[node name="DialogueEngineDemoVBoxContainer" type="VBoxContainer"]
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
script = ExtResource("1_et54s")
dialogue_gdscript = ExtResource("2_x1mbj")
[node name="DialogueEngineDemoQuickStartPanelContainer" type="PanelContainer" parent="."]
layout_mode = 2
[node name="DialogueEngineDemoQuickStartLabel" type="Label" parent="DialogueEngineDemoQuickStartPanelContainer"]
layout_mode = 2
text = "Press <Enter> or <Space> to progress the dialogue."
horizontal_alignment = 1
[node name="LogRichTextLabel" type="RichTextLabel" parent="."]
layout_mode = 2
text = "Log:"
fit_content = true

View File

@@ -0,0 +1,21 @@
extends DialogueEngine
var have_we_talked_before : bool = false
enum branch {
STRANGERS,
ACQUAINTANCES,
}
func __have_we_talked_before() -> bool:
return have_we_talked_before
func _setup() -> void:
add_text_entry("Hello!")
var condition_entry : DialogueEntry = add_conditional_entry(__have_we_talked_before)
var if_true : DialogueEntry = add_text_entry("Hey! We meet again!", branch.STRANGERS)
var if_false : DialogueEntry = add_text_entry("It's nice to meet you!", branch.ACQUAINTANCES)
condition_entry.set_condition_goto_ids(if_true.get_id(), if_false.get_id())
add_text_entry("<Press 'Enter' or 'Space' to exit>")
dialogue_finished.connect(func() -> void: have_we_talked_before = true)

View File

@@ -0,0 +1,38 @@
extends VBoxContainer
@export var dialogue_gdscript : GDScript = null
var dialogue_engine : DialogueEngine = null
var dialogue_finished_count : int = 0
func _ready() -> void:
dialogue_engine = dialogue_gdscript.new()
dialogue_engine.dialogue_continued.connect(__on_dialogue_continued)
dialogue_engine.dialogue_finished.connect(__on_dialogue_finished)
func _input(p_input_event : InputEvent) -> void:
if p_input_event.is_action_pressed(&"ui_accept"):
dialogue_engine.advance()
var enabled_buttons : Array[Button] = []
func __on_dialogue_continued(p_dialogue_entry : DialogueEntry) -> void:
var label : RichTextLabel = RichTextLabel.new()
label.set_use_bbcode(true)
label.set_fit_content(true)
label.set_text(" > " + p_dialogue_entry.get_text())
add_child(label)
func __on_dialogue_finished() -> void:
if dialogue_finished_count > 0:
get_tree().quit()
return
dialogue_finished_count += 1
var label : RichTextLabel = RichTextLabel.new()
label.set_fit_content(true)
label.set_text("<Some time passes>")
add_child(label)

View File

@@ -0,0 +1,26 @@
[gd_scene load_steps=3 format=3 uid="uid://bl1d3ktmm0s7f"]
[ext_resource type="Script" path="res://demos/4. branching condition/branching_condition_log.gd" id="1_7x7u2"]
[ext_resource type="Script" path="res://demos/4. branching condition/branching_condition_dialogue.gd" id="2_yhcl2"]
[node name="DialogueEngineDemoVBoxContainer" type="VBoxContainer"]
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
script = ExtResource("1_7x7u2")
dialogue_gdscript = ExtResource("2_yhcl2")
[node name="DialogueEngineDemoQuickStartPanelContainer" type="PanelContainer" parent="."]
layout_mode = 2
[node name="DialogueEngineDemoQuickStartLabel" type="Label" parent="DialogueEngineDemoQuickStartPanelContainer"]
layout_mode = 2
text = "Press <Enter> or <Space> to progress the dialogue."
horizontal_alignment = 1
[node name="LogRichTextLabel" type="RichTextLabel" parent="."]
layout_mode = 2
text = "Log:"
fit_content = true

View File

@@ -0,0 +1,31 @@
extends DialogueEngine
enum {
DEFAULT_TOPIC = 0, # this is the branch used by default unless set_branch_id() is used
GO_BACK_TO_SLEEP = 1,
KEEP_WORKING = 2
}
func _setup() -> void:
var entry : DialogueEntry = add_text_entry("The storm rages right outside the window. I should...")
var option_id_1 : int = entry.add_option("Go back to sleep.")
var option_id_2 : int = entry.add_option("Get back to work.")
var option_id_1_entry : DialogueEntry = add_text_entry("That's right, sleep is for the strong 💪.", GO_BACK_TO_SLEEP)
entry.set_option_goto_id(option_id_1, option_id_1_entry.get_id())
var option_id_2_entry : DialogueEntry = add_text_entry("That's right, let's get back to work 🫡", KEEP_WORKING)
entry.set_option_goto_id(option_id_2, option_id_2_entry.get_id())
# Join branches into the default topic (i.e. branch id 0)
var default_topic : DialogueEntry = add_text_entry("Some time passes...")
option_id_1_entry.set_goto_id(default_topic.get_id())
option_id_2_entry.set_goto_id(default_topic.get_id())
# None of the following entries will be connected on the graph and won't be shown when advancing the dialogue
add_text_entry("A sleep entry skipped due to missing goto against this entry.", GO_BACK_TO_SLEEP)
add_text_entry("A working entry due to missing goto against this entry.", KEEP_WORKING)
add_text_entry("<Press 'Space' or 'Enter' to quit>")

View File

@@ -0,0 +1,65 @@
extends VBoxContainer
@export var dialogue_gdscript : GDScript = null
var dialogue_engine : DialogueEngine = null
func _ready() -> void:
dialogue_engine = dialogue_gdscript.new()
dialogue_engine.dialogue_started.connect(__on_dialogue_started)
dialogue_engine.dialogue_continued.connect(__on_dialogue_continued)
dialogue_engine.dialogue_finished.connect(__on_dialogue_finished)
dialogue_engine.dialogue_cancelled.connect(__on_dialogue_cancelled)
func _input(p_input_event : InputEvent) -> void:
if p_input_event.is_action_pressed(&"ui_accept"):
dialogue_engine.advance()
accept_event() # to avoid hidding an button due to the input event travelling through the children
func __on_dialogue_started() -> void:
print("Dialogue Started!")
var enabled_buttons : Array[Button] = []
func __on_dialogue_continued(p_dialogue_entry : DialogueEntry) -> void:
var label : RichTextLabel = RichTextLabel.new()
label.set_use_bbcode(true)
label.set_fit_content(true)
label.set_text(" > " + p_dialogue_entry.get_text())
add_child(label)
if p_dialogue_entry.has_options():
for option_id : int in range(0, p_dialogue_entry.get_option_count()):
var option_text : String = p_dialogue_entry.get_option_text(option_id)
var button : Button = Button.new()
button.set_text(option_text)
add_child(button)
if option_id == 0:
button.grab_focus()
button.pressed.connect(__advance_dialogue_with_chosen_option.bind(option_id))
enabled_buttons.push_back(button)
set_process_input(false)
func __advance_dialogue_with_chosen_option(p_option_id : int) -> void:
for button : Button in enabled_buttons:
button.set_disabled(true)
enabled_buttons.clear()
var current_entry : DialogueEntry = dialogue_engine.get_current_entry()
current_entry.choose_option(p_option_id)
dialogue_engine.advance()
set_process_input(true)
func __on_dialogue_finished() -> void:
print("Dialogue Finished! Exiting...")
get_tree().quit()
func __on_dialogue_cancelled() -> void:
print("Dialogue Cancelled! Exiting...")
get_tree().quit()

View File

@@ -0,0 +1,26 @@
[gd_scene load_steps=3 format=3 uid="uid://c1803quu2vtn"]
[ext_resource type="Script" path="res://demos/5. branching options dialogue/branching_options_dialogue_log.gd" id="1_0yldt"]
[ext_resource type="Script" path="res://demos/5. branching options dialogue/branching_options_dialogue.gd" id="2_h7u25"]
[node name="DialogueEngineDemoVBoxContainer" type="VBoxContainer"]
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
script = ExtResource("1_0yldt")
dialogue_gdscript = ExtResource("2_h7u25")
[node name="DialogueEngineDemoQuickStartPanelContainer" type="PanelContainer" parent="."]
layout_mode = 2
[node name="DialogueEngineDemoQuickStartLabel" type="Label" parent="DialogueEngineDemoQuickStartPanelContainer"]
layout_mode = 2
text = "Press <Enter> or <Space> to progress the dialogue."
horizontal_alignment = 1
[node name="LogRichTextLabel" type="RichTextLabel" parent="."]
layout_mode = 2
text = "Log:"
fit_content = true

View File

@@ -0,0 +1,7 @@
extends DialogueEngine
func _setup() -> void:
# Use DialogueEntry.set_metadata for data that must be available through DialogueEngine.dialogue_continued signal.
# The metadata handling per DialogueEntry must be implemented by the user.
add_text_entry("[i]We won! Let's goooo!![/i]").set_metadata("author", "Gary")
add_text_entry("Press <Enter> or <Space> to exit.")

View File

@@ -0,0 +1,43 @@
extends VBoxContainer
@export var dialogue_gdscript : GDScript = null
var dialogue_engine : DialogueEngine = null
func _ready() -> void:
dialogue_engine = dialogue_gdscript.new()
dialogue_engine.dialogue_started.connect(__on_dialogue_started)
dialogue_engine.dialogue_continued.connect(__on_dialogue_continued)
dialogue_engine.dialogue_finished.connect(__on_dialogue_finished)
dialogue_engine.dialogue_cancelled.connect(__on_dialogue_cancelled)
func _input(p_input_event : InputEvent) -> void:
if p_input_event.is_action_pressed(&"ui_accept"):
dialogue_engine.advance()
func __on_dialogue_started() -> void:
print("Dialogue Started!")
func __on_dialogue_continued(p_dialogue_entry : DialogueEntry) -> void:
var label : RichTextLabel = RichTextLabel.new()
label.set_use_bbcode(true)
label.set_fit_content(true)
if p_dialogue_entry.has_metadata("author"):
var author : String = p_dialogue_entry.get_metadata("author")
label.set_text(" > " + author + ": " + p_dialogue_entry.get_text())
else:
label.set_text(" > " + p_dialogue_entry.get_text())
add_child(label)
func __on_dialogue_finished() -> void:
print("Dialogue Finished! Exiting...")
get_tree().quit()
func __on_dialogue_cancelled() -> void:
print("Dialogue Cancelled! Exiting...")
get_tree().quit()

View File

@@ -0,0 +1,26 @@
[gd_scene load_steps=3 format=3 uid="uid://jeupskd3oea8"]
[ext_resource type="Script" path="res://demos/6. handling metadata/handling_metadata_log.gd" id="1_0ot0q"]
[ext_resource type="Script" path="res://demos/6. handling metadata/handling_metadata_dialogue.gd" id="2_tq44t"]
[node name="DialogueEngineDemoVBoxContainer" type="VBoxContainer"]
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
script = ExtResource("1_0ot0q")
dialogue_gdscript = ExtResource("2_tq44t")
[node name="DialogueEngineDemoQuickStartPanelContainer" type="PanelContainer" parent="."]
layout_mode = 2
[node name="DialogueEngineDemoQuickStartLabel" type="Label" parent="DialogueEngineDemoQuickStartPanelContainer"]
layout_mode = 2
text = "Press <Enter> or <Space> to progress the dialogue."
horizontal_alignment = 1
[node name="LogRichTextLabel" type="RichTextLabel" parent="."]
layout_mode = 2
text = "Log:"
fit_content = true

View File

@@ -0,0 +1,8 @@
extends DialogueEngine
var player_name : String # will be set by the UI code
func _setup() -> void:
add_text_entry("Welcome adventurer. May I know you name?").set_metadata(&"get_player_name", "The UI code will act accordingly and inject player_name into DialogueEngine.")
add_text_entry("The legendary {player_name}!? Please, follow me this way. I will personally show you our guild.").set_format({"player_name" : get.bind("player_name")}, DialogueEntry.FORMAT_FUNCTION)
add_text_entry("Press <Enter> or <Space> to exit.")

View File

@@ -0,0 +1,64 @@
extends VBoxContainer
@export var dialogue_gdscript : GDScript = null
var dialogue_engine : DialogueEngine = null
func _ready() -> void:
dialogue_engine = dialogue_gdscript.new()
dialogue_engine.dialogue_started.connect(__on_dialogue_started)
dialogue_engine.dialogue_continued.connect(__on_dialogue_continued)
dialogue_engine.dialogue_finished.connect(__on_dialogue_finished)
dialogue_engine.dialogue_cancelled.connect(__on_dialogue_cancelled)
func _input(p_input_event : InputEvent) -> void:
if p_input_event.is_action_pressed(&"ui_accept"):
dialogue_engine.advance()
accept_event()
func __on_dialogue_started() -> void:
print("Dialogue Started!")
func __on_dialogue_continued(p_dialogue_entry : DialogueEntry) -> void:
var label : RichTextLabel = RichTextLabel.new()
label.set_use_bbcode(true)
label.set_fit_content(true)
if p_dialogue_entry.has_metadata("author"):
var author : String = p_dialogue_entry.get_metadata("author")
label.set_text(" > " + author + ": " + p_dialogue_entry.get_formatted_text())
else:
label.set_text(" > " + p_dialogue_entry.get_formatted_text())
add_child(label)
if p_dialogue_entry.has_metadata(&"get_player_name"):
__get_player_name()
func __on_dialogue_finished() -> void:
print("Dialogue Finished! Exiting...")
get_tree().quit()
func __on_dialogue_cancelled() -> void:
print("Dialogue Cancelled! Exiting...")
get_tree().quit()
# Must return player name to update the variable within DialogueEngine
func __get_player_name() -> void:
var line_edit : LineEdit = LineEdit.new()
add_child(line_edit)
var p_data : Array = []
line_edit.text_submitted.connect(func(text : String) -> void:
p_data.push_back(text)
)
line_edit.grab_focus()
line_edit.set_placeholder("Enter your name.")
set_process_input(false)
await line_edit.text_submitted
line_edit.set_editable(false)
@warning_ignore("unsafe_property_access")
dialogue_engine.player_name = p_data[0]
set_process_input(true)
dialogue_engine.advance()

View File

@@ -0,0 +1,26 @@
[gd_scene load_steps=3 format=3 uid="uid://cc22y2sa14se6"]
[ext_resource type="Script" path="res://demos/7. handling input/handling_input_log.gd" id="1_4r4r1"]
[ext_resource type="Script" path="res://demos/7. handling input/handling_input_dialogue.gd" id="2_ti25x"]
[node name="DialogueEngineDemoVBoxContainer" type="VBoxContainer"]
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
script = ExtResource("1_4r4r1")
dialogue_gdscript = ExtResource("2_ti25x")
[node name="DialogueEngineDemoQuickStartPanelContainer" type="PanelContainer" parent="."]
layout_mode = 2
[node name="DialogueEngineDemoQuickStartLabel" type="Label" parent="DialogueEngineDemoQuickStartPanelContainer"]
layout_mode = 2
text = "Press <Enter> or <Space> to progress the dialogue."
horizontal_alignment = 1
[node name="LogRichTextLabel" type="RichTextLabel" parent="."]
layout_mode = 2
text = "Log:"
fit_content = true

View File

@@ -0,0 +1,8 @@
extends DialogueEngine
var player_name : String # will be set by the UI code
func _setup() -> void:
add_text_entry("Welcome adventurer. May I know you name?").set_metadata(&"get_player_name", "The UI code will act accordingly and inject player_name into DialogueEngine.")
add_text_entry("The legendary %s!? Please, follow me this way. I will personally show you our guild.").set_format([get.bind("player_name")], DialogueEntry.FORMAT_OPERATOR)
add_text_entry("Press <Enter> or <Space> to exit.").set_name("Exit")

View File

@@ -0,0 +1,146 @@
extends VBoxContainer
@export var dialogue_gdscript : GDScript = null
var dialogue_engine : DialogueEngine = null
@onready var animation_player : AnimationPlayer = find_child("LogAnimationPlayer")
func _ready() -> void:
dialogue_engine = dialogue_gdscript.new()
dialogue_engine.dialogue_started.connect(__on_dialogue_started)
dialogue_engine.dialogue_continued.connect(__on_dialogue_continued)
dialogue_engine.dialogue_finished.connect(__on_dialogue_finished)
dialogue_engine.dialogue_cancelled.connect(__on_dialogue_cancelled)
func _input(p_input_event : InputEvent) -> void:
if p_input_event.is_action_pressed(&"ui_accept"):
if not animation_player.is_playing():
dialogue_engine.advance()
else:
# Player is inpatient -- auto-advance the text
var animation_name : StringName = animation_player.get_current_animation()
var animation : Animation = animation_player.get_animation(animation_name)
animation_player.advance(animation.get_length()) # this will fire the animation_finished signal automatically
accept_event() # accepting input event here to stop it from traversing into into buttons possibly added through the interaction
func __on_dialogue_started() -> void:
print("Dialogue Started!")
func __on_dialogue_continued(p_dialogue_entry : DialogueEntry) -> void:
# Add the text to the log:
var label : RichTextLabel = RichTextLabel.new()
label.set_use_bbcode(true)
label.set_fit_content(true)
if p_dialogue_entry.has_metadata("author"):
var author : String = p_dialogue_entry.get_metadata("author")
label.set_text(" > " + author + ": " + p_dialogue_entry.get_formatted_text())
else:
label.set_text(" > " + p_dialogue_entry.get_formatted_text())
add_child(label, true)
# Setup the animation:
animation_player.stop(true) # internally some timers do not reset properly unless we do this
if not animation_player.has_animation_library(&"demo"):
var new_animation_library : AnimationLibrary = AnimationLibrary.new()
animation_player.add_animation_library(&"demo", new_animation_library)
var animation_library : AnimationLibrary = animation_player.get_animation_library(&"demo")
var animation : Animation = create_visible_characters_animation_per_character(label.get_text(), 0.045, true)
animation_player.set_root_node(label.get_path())
animation_library.add_animation(&"dialogue", animation)
animation_player.play(&"demo/dialogue")
# Setup the post dialogue callback
if p_dialogue_entry.has_metadata(&"get_player_name"):
animation_player.animation_finished.connect(__on_animation_finished.bind(__get_player_name), CONNECT_ONE_SHOT)
func __on_dialogue_finished() -> void:
print("Dialogue Finished! Exiting...")
get_tree().quit()
func __on_dialogue_cancelled() -> void:
print("Dialogue Cancelled! Exiting...")
get_tree().quit()
# Must return player name to update the variable within DialogueEngine
func __get_player_name() -> void:
# Get player name into the current stack:
var line_edit : LineEdit = LineEdit.new()
add_child(line_edit)
var p_data : Array = []
line_edit.text_submitted.connect(func(text : String) -> void:
p_data.push_back(text)
)
line_edit.grab_focus()
line_edit.set_placeholder("Enter your name.")
# Disable input processing by this node to avoid calling DialogueEngine.advance if the user presses space or enter
set_process_input(false)
await line_edit.text_submitted
line_edit.set_editable(false)
# Allow the user to progress the dialogue
set_process_input(true)
# Auto-advance the dialogue so the user does not have to press space or enter again
@warning_ignore("unsafe_property_access")
dialogue_engine.player_name = p_data[0]
dialogue_engine.advance()
func __on_animation_started(p_animation_name : StringName) -> void:
print("Animation started:", p_animation_name)
func __on_animation_finished(p_animation_name : StringName, p_post_dialogue_callback : Callable) -> void:
if p_animation_name == &"demo/dialogue":
p_post_dialogue_callback.call()
# Utility function to animate the text at a constant speed
func create_visible_characters_animation_per_character(p_text : String, p_time_per_character : float, p_instant_first_character : bool = false, p_time_whitespace : bool = false) -> Animation:
# Do initial calculations
var whitespace_regex : RegEx
if not p_time_whitespace or p_instant_first_character:
whitespace_regex = RegEx.new()
whitespace_regex.compile("\\s")
# Create animation and track
var animation : Animation = Animation.new()
var track_index : int = animation.add_track(Animation.TYPE_VALUE)
animation.track_set_path(track_index, ".:visible_characters")
animation.track_set_interpolation_type(track_index, Animation.INTERPOLATION_LINEAR)
# Configure keys
var total_time : float = 0.0
var total_visible_characters : int = 0
var whitespace_time_offset : float = 0.0
animation.track_insert_key(track_index, total_time, 0)
var total_animation_length : float = 0.0
for character : String in p_text:
total_time += p_time_per_character
total_visible_characters += 1
if not p_time_whitespace and whitespace_regex.sub(character, "", true).is_empty():
whitespace_time_offset += p_time_per_character
continue
total_animation_length = total_time - whitespace_time_offset
animation.track_insert_key(track_index, total_animation_length, total_visible_characters)
animation.set_length(total_animation_length)
if p_instant_first_character:
if animation.track_get_key_count(track_index) > 0:
# Shift all the keys back in time according to the time it took per character
for key_index : int in animation.track_get_key_count(track_index):
var key_time : float = animation.track_get_key_time(track_index, key_index)
animation.track_set_key_time(track_index, key_index, key_time - p_time_per_character)
animation.set_length(total_animation_length - p_time_per_character)
return animation

View File

@@ -0,0 +1,29 @@
[gd_scene load_steps=3 format=3 uid="uid://djujyea67gj55"]
[ext_resource type="Script" path="res://demos/8. handling animations/handling_animations_log.gd" id="1_0567n"]
[ext_resource type="Script" path="res://demos/8. handling animations/handling_animations_dialogue.gd" id="2_4ky6k"]
[node name="DialogueEngineDemoVBoxContainer" type="VBoxContainer"]
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
script = ExtResource("1_0567n")
dialogue_gdscript = ExtResource("2_4ky6k")
[node name="DialogueEngineDemoQuickStartPanelContainer" type="PanelContainer" parent="."]
layout_mode = 2
[node name="DialogueEngineDemoQuickStartLabel" type="Label" parent="DialogueEngineDemoQuickStartPanelContainer"]
layout_mode = 2
text = "Press <Enter> or <Space> to progress the dialogue."
horizontal_alignment = 1
[node name="LogRichTextLabel" type="RichTextLabel" parent="."]
layout_mode = 2
text = "Log:"
fit_content = true
[node name="LogAnimationPlayer" type="AnimationPlayer" parent="."]
root_node = NodePath("../LogRichTextLabel")

View File

@@ -0,0 +1,161 @@
extends Control
@export var dialogue_gdscript : GDScript = null
var dialogue_engine : DialogueEngine = null
@onready var animator : AnimationPlayer = $Animator
@onready var vbox : VBoxContainer = $Center/Box/Margin/VBox/VBox
@onready var peter : TextureRect = $Center/Box/Margin/VBox/Peter
@onready var john : TextureRect = $Center/Box/Margin/VBox/John
@onready var audio_player : AudioStreamPlayer = $AudioStreamPlayer
var beep : PackedVector2Array = []
func _ready() -> void:
dialogue_engine = dialogue_gdscript.new()
dialogue_engine.dialogue_started.connect(__on_dialogue_started)
dialogue_engine.dialogue_continued.connect(__on_dialogue_continued)
dialogue_engine.dialogue_finished.connect(__on_dialogue_finished)
dialogue_engine.dialogue_cancelled.connect(__on_dialogue_cancelled)
# Generate a beep to use when displaying the characters
var audio_frame_being_filled : int = 2730
var total_audio_frames_to_fill : int = audio_frame_being_filled
var beep_softener_ratio : float = 0.05
var sample_hz : float = 22050.0 # Keep the number of samples to mix low, GDScript is not super fast.
var pulse_hz : float = 440.0
var phase : float = 0.0
var increment : float = pulse_hz / sample_hz
while audio_frame_being_filled > 0:
var _ignore : bool = beep.push_back(Vector2.ONE * beep_softener_ratio * sin(phase * TAU) * audio_frame_being_filled/total_audio_frames_to_fill)
phase = fmod(phase + increment, 1.0)
audio_frame_being_filled -= 1
func _input(p_input_event : InputEvent) -> void:
if p_input_event.is_action_pressed(&"ui_accept"):
if not animator.is_playing():
dialogue_engine.advance()
else:
# Player is inpatient -- auto-advance the text
var animation_name : StringName = animator.get_current_animation()
var animation : Animation = animator.get_animation(animation_name)
animator.advance(animation.get_length()) # this will fire the animation_finished signal automatically
accept_event() # accepting input event here to stop it from traversing into into buttons possibly added through the interaction
func __on_dialogue_started() -> void:
print("Dialogue Started!")
func __on_dialogue_continued(p_dialogue_entry : DialogueEntry) -> void:
# Add the text to the log:
var label : RichTextLabel = RichTextLabel.new()
label.set_use_bbcode(true)
label.set_fit_content(true)
if p_dialogue_entry.has_metadata("author"):
var author : String = p_dialogue_entry.get_metadata("author")
label.set_text(" > " + author + ": " + p_dialogue_entry.get_formatted_text())
else:
label.set_text(" > " + p_dialogue_entry.get_formatted_text())
vbox.add_child(label, true)
# Setup the animation:
animator.stop(true) # internally some timers do not reset properly unless we do this
if not animator.has_animation_library(&"demo"):
var new_animation_library : AnimationLibrary = AnimationLibrary.new()
animator.add_animation_library(&"demo", new_animation_library)
var animation_library : AnimationLibrary = animator.get_animation_library(&"demo")
var animation : Animation = create_visible_characters_animation_per_character(label.get_text(), 0.045, true)
animator.set_root_node(label.get_path())
animation_library.add_animation(&"dialogue", animation)
animator.play(&"demo/dialogue")
# Hide all portraits
peter.hide()
john.hide()
# Show author portrait
if p_dialogue_entry.has_metadata("author"):
if p_dialogue_entry.get_metadata("author") == "Peter":
peter.show()
else:
john.show()
func __on_dialogue_finished() -> void:
print("Dialogue Finished! Exiting...")
get_tree().quit()
func __on_dialogue_cancelled() -> void:
print("Dialogue Cancelled! Exiting...")
get_tree().quit()
func __on_animation_started(p_animation_name : StringName) -> void:
print("Animation started:", p_animation_name)
# Utility function to animate the text at a constant speed
func create_visible_characters_animation_per_character(p_text : String, p_time_per_character : float, p_instant_first_character : bool = false, p_time_whitespace : bool = false) -> Animation:
# Do initial calculations
var whitespace_regex : RegEx
if not p_time_whitespace or p_instant_first_character:
whitespace_regex = RegEx.new()
whitespace_regex.compile("\\s")
# Create animation and track
var animation : Animation = Animation.new()
var track_index : int = animation.add_track(Animation.TYPE_VALUE)
animation.track_set_path(track_index, ".:visible_characters")
animation.track_set_interpolation_type(track_index, Animation.INTERPOLATION_LINEAR)
var sound_track_index : int = animation.add_track(Animation.TYPE_METHOD)
# The path provided here points to the root node (i.e. ComplexDialogue node)
animation.track_set_path(sound_track_index, "../../../../../..")
# Configure keys
var total_time : float = 0.0
var total_visible_characters : int = 0
var whitespace_time_offset : float = 0.0
animation.track_insert_key(track_index, total_time, 0)
var total_animation_length : float = 0.0
for character : String in p_text:
total_time += p_time_per_character
total_visible_characters += 1
if not p_time_whitespace and whitespace_regex.sub(character, "", true).is_empty():
whitespace_time_offset += p_time_per_character
continue
total_animation_length = total_time - whitespace_time_offset
animation.track_insert_key(track_index, total_animation_length, total_visible_characters)
# Simple sound animation:
var should_play : bool = total_visible_characters % 3 == 0 and total_visible_characters < p_text.length() - 1
if should_play:
animation.track_insert_key(sound_track_index, total_animation_length, {"method": "__play_sound", "args": []})
animation.set_length(total_animation_length)
if p_instant_first_character:
if animation.track_get_key_count(track_index) > 0:
# Shift all the keys back in time according to the time it took per character
for key_index : int in animation.track_get_key_count(track_index):
var key_time : float = animation.track_get_key_time(track_index, key_index)
animation.track_set_key_time(track_index, key_index, key_time - p_time_per_character)
animation.set_length(total_animation_length - p_time_per_character)
return animation
func __play_sound() -> void:
# Create the audio stream dynamically
var audio_stream : AudioStreamGenerator = AudioStreamGenerator.new()
const mix_rate : float = 22050.0
audio_stream.set_mix_rate(mix_rate)
audio_player.set_stream(audio_stream)
# Fill in the playback audio frames:
audio_player.play()
var playback : AudioStreamGeneratorPlayback = audio_player.get_stream_playback()
var total_available_frames : int = playback.get_frames_available()
var _success : bool = playback.push_buffer(beep)
var _frames_filled : int = total_available_frames - playback.get_frames_available()

View File

@@ -0,0 +1,84 @@
[gd_scene load_steps=5 format=3 uid="uid://djkxscwuw617h"]
[ext_resource type="Texture2D" uid="uid://cr0rdb3ab8xwg" path="res://icon.svg" id="1_5ptqw"]
[ext_resource type="Script" path="res://demos/9. complex dialogue/complex_dialogue.gd" id="1_qenq2"]
[ext_resource type="Script" path="res://demos/9. complex dialogue/dialogue.gd" id="2_buq0b"]
[sub_resource type="StyleBoxFlat" id="StyleBoxFlat_hqwn4"]
bg_color = Color(0, 0, 0, 1)
[node name="ComplexDialogue" type="Control"]
layout_mode = 3
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
script = ExtResource("1_qenq2")
dialogue_gdscript = ExtResource("2_buq0b")
[node name="DialogueEngineDemoQuickStartPanelContainer" type="PanelContainer" parent="."]
layout_mode = 1
anchors_preset = 10
anchor_right = 1.0
offset_bottom = 23.0
grow_horizontal = 2
[node name="DialogueEngineDemoQuickStartLabel" type="Label" parent="DialogueEngineDemoQuickStartPanelContainer"]
layout_mode = 2
text = "Press <Enter> or <Space> to progress the dialogue."
horizontal_alignment = 1
[node name="Center" type="CenterContainer" parent="."]
layout_mode = 1
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
[node name="Box" type="PanelContainer" parent="Center"]
layout_mode = 2
theme_override_styles/panel = SubResource("StyleBoxFlat_hqwn4")
[node name="Margin" type="MarginContainer" parent="Center/Box"]
custom_minimum_size = Vector2(384, 384)
layout_mode = 2
theme_override_constants/margin_left = 16
theme_override_constants/margin_top = 16
theme_override_constants/margin_right = 16
theme_override_constants/margin_bottom = 16
[node name="VBox" type="VBoxContainer" parent="Center/Box/Margin"]
layout_mode = 2
theme_override_constants/separation = 32
[node name="VBox" type="VBoxContainer" parent="Center/Box/Margin/VBox"]
layout_mode = 2
size_flags_vertical = 3
[node name="Text" type="RichTextLabel" parent="Center/Box/Margin/VBox/VBox"]
layout_mode = 2
text = "Log:"
fit_content = true
[node name="Peter" type="TextureRect" parent="Center/Box/Margin/VBox"]
visible = false
custom_minimum_size = Vector2(96, 96)
layout_mode = 2
size_flags_horizontal = 0
texture = ExtResource("1_5ptqw")
expand_mode = 1
[node name="John" type="TextureRect" parent="Center/Box/Margin/VBox"]
visible = false
modulate = Color(0.980392, 0.631373, 1, 1)
custom_minimum_size = Vector2(96, 96)
layout_mode = 2
size_flags_horizontal = 8
texture = ExtResource("1_5ptqw")
expand_mode = 1
[node name="Animator" type="AnimationPlayer" parent="."]
[node name="AudioStreamPlayer" type="AudioStreamPlayer" parent="."]

View File

@@ -0,0 +1,13 @@
extends DialogueEngine
func _setup() -> void:
add_text_entry("Hello, how are you?").set_metadata("author", "Peter")
add_text_entry("I'm fine, thank you! And you?").set_metadata("author", "John")
add_text_entry("I'm fine too! Thank you!").set_metadata("author", "Peter")
add_text_entry("What's your name?").set_metadata("author", "John")
add_text_entry("I'm Peter, and you?").set_metadata("author", "Peter")
add_text_entry("Nice to meet you Peter! I'm John!").set_metadata("author", "John")
var entry: DialogueEntry = add_text_entry("Nice to meet you John!")
entry.set_metadata("author", "Peter")
entry.set_name("Exit")