Files
godot-demo-projects/2d/role_playing_game/dialogue/dialogue_player/dialogue_player.gd
2025-10-11 05:03:59 -07:00

45 lines
1.0 KiB
GDScript

extends Node
signal dialogue_started
signal dialogue_finished
@export_file("*.json") var dialogue_file: String
var dialogue_keys := []
var dialogue_name: String = ""
var current := 0
var dialogue_text: String = ""
func start_dialogue() -> void:
dialogue_started.emit()
current = 0
index_dialogue()
dialogue_text = dialogue_keys[current].text
dialogue_name = dialogue_keys[current].name
func next_dialogue() -> void:
current += 1
if current == dialogue_keys.size():
dialogue_finished.emit()
return
dialogue_text = dialogue_keys[current].text
dialogue_name = dialogue_keys[current].name
func index_dialogue() -> void:
var dialogue: Dictionary = load_dialogue(dialogue_file)
dialogue_keys.clear()
for key: String in dialogue:
dialogue_keys.append(dialogue[key])
func load_dialogue(file_path: String) -> Dictionary:
var file := FileAccess.open(file_path, FileAccess.READ)
if file:
var test_json_conv := JSON.new()
test_json_conv.parse(file.get_as_text())
return test_json_conv.data
return {}