I recently implemented a dialogue system into my 3D game. It works based off a collision attached to my ray cast already on my player. Then it uses masks with other collisions to then say things from the json file.
Code for the Area3D collision set up on multiple objects in my scene.
extends Area3D
@export var dialog_key = ""
var area_active = false
func _input(event: InputEvent) -> void:
if area_active and event.is_action_pressed("action_interact"):
SignalBus.emit_signal("display_dialog", dialog_key)
func _on_area_entered(area: Area3D) -> void:
area_active = true # Replace with function body.
func _on_area_exited(area: Area3D) -> void:
area_active = false # Replace with function body.
This is the code for the dialogue player itself.
extends CanvasLayer
@export_file("*json") var scene_text_file: String
var scene_text: Dictionary = {}
var selected_text: Array = []
var in_progress: bool = false
@onready var background = $Background
@onready var text_label = $TextLabel
func _ready():
background.visible = false
scene_text = load_scene_text()
SignalBus.connect("display_dialog", Callable(self, "on_display_dialog"))
func load_scene_text():
if FileAccess.file_exists(scene_text_file):
var file = FileAccess.open(scene_text_file, FileAccess.READ)
var test_json_conv = JSON.new()
test_json_conv.parse(file.get_as_text())
return test_json_conv.get_data()
func show_text():
text_label.text = selected_text.pop_front()
func next_line():
if selected_text.size() > 0:
show_text()
else:
finish()
func finish():
text_label.text = ""
background.visible = false
in_progress = false
get_tree().paused = false
func on_display_dialog(text_key):
if in_progress:
next_line()
else:
get_tree().paused = true
background.visible = true
in_progress = true
selected_text = scene_text[text_key].duplicate()
show_text()
This is just a script called SignalBus.
extends Node
signal display_dialog(text_key)
Any and all help would be appreciated. Also I can show more stuff if needed.