Godot Version
4.3-stable_linux.x86_64
Question
` I’ve attempted to make my own dialogue system in my 2d platformer. I tried to design it to be fairly flexible. Seeing as all of the dialogue would have accompanying animated sprites with variable names, I based it around using get_parent().name, and making a single area 2d called “DialogueInteraction”. I’d give the animated sprite a name, then set “DialogueInteraction” as the child, then use get_parent().name to find the name of the animated sprite, then match that name to a parsed JSON file, which would then realy the text.
Unfortunately I’ve encountered a problem with this system where get_parent().name causes every instance of the DialogueInteraction to name it’s parent, then the text displayer loads all of them and just shows whichever one came last. I’ve attempted to find ways to only get one name, like calling the get_parent.name only when the area is entered, which looks like it should work when I use print(get_parent.name) but then breaks as soon as it’s actually run.
If anyone has a solution for me I’d greatly appreciate it. This is my first project where I’m actually trying to do it without relying on any tutorials, and only using my own old tutorial projects to reference code, and it’s been going pretty alright until now. `
DialogueInteraction:
Example of how DialogueInteraction is used:
DialogueInteraction Code:
class_name Dialogue_interaction extends Area2D
@onready var animated_sprite_2d: AnimatedSprite2D = $AnimatedSprite2D
var current_name : String
func _on_area_entered(area: Area2D) -> void:
DialogueBox.chat_zone = true
animated_sprite_2d.show()
pass
func _on_area_exited(area: Area2D) -> void:
print(current_name)
DialogueBox.chat_zone = false
animated_sprite_2d.hide()
func _process(delta: float) -> void:
if Input.is_action_just_pressed("Interact") and DialogueBox.chat_zone == true:
interact_pressed()
else:
pass
func interact_pressed() -> void:
DialogueBox.UpdateDialogue(get_parent().name)
pass
This is the code that runs the actual dialogue:
class_name Dialogue_Box extends CanvasLayer
signal dialogue_finished
@export_file("*.json") var d_file
#@onready var portrait = $Dialogue/NinePatchRect/HBoxContainer/Portrait
@onready var chat: RichTextLabel = $Dialogue/Chat
const DIALOGUE_INTERACTION = preload("res://Dialouge/dialogue_interaction.tscn")
var is_active : bool = false
var dialogue_interaction : Dialogue_interaction
var chat_zone : bool = false
var current_dialogue_id = -1
var dialogue_choice = []
var chat_name
@onready var dialogue: Control = $Dialogue
func _ready() -> void:
current_dialogue_id = -1
hide_dialogue()
pass
func start():
chat.visible_ratio = 0
current_dialogue_id = -1
if is_active:
return
is_active = true
show_dialogue()
func _process(delta: float) -> void:
#print(chat.visible_ratio)
chat.visible_ratio += 0.05
func _unhandled_input(event : InputEvent) -> void:
#if is_active == false:
#return
if event.is_action_pressed("Interact") and chat_zone == true and GlobalPlayerManager.player.is_on_floor():
#print(len(dialogue_choice))
#print(current_dialogue_id)
if is_active == false:
show_dialogue()
if is_active == true:
if current_dialogue_id >= len(dialogue_choice) - 1 and chat.visible_ratio == 1:
current_dialogue_id = -1
hide_dialogue()
else:
if chat.visible_ratio == 1:
current_dialogue_id += 1
chat.visible_ratio = 0
pass
func UpdateDialogue(npcname : String) -> void:
#chat.visible_characters = 0
dialogue_choice = load_dialogue(npcname)
$Dialogue/Name.text = chat_name
$Dialogue/Chat.text = dialogue_choice[current_dialogue_id]['chat']
func show_dialogue() -> void:
is_active = true
dialogue.visible = true
dialogue.process_mode = Node.PROCESS_MODE_ALWAYS
get_tree().paused = true
pass
func hide_dialogue() -> void:
is_active = false
dialogue.visible = false
dialogue.process_mode = Node.PROCESS_MODE_DISABLED
get_tree().paused = false
pass
func load_dialogue( npcname : String ):
if npcname == "Shopkeep":
chat_name = "Shopkeep"
var file = FileAccess.open("res://Dialouge/npc_dialogue/shopkeep/1.json", FileAccess.READ)
var content = JSON.parse_string(file.get_as_text())
print (content)
return content
if npcname == "Blah":
chat_name = "Shopkeep"
var file = FileAccess.open("res://Dialouge/npc_dialogue/shopkeep/2.json", FileAccess.READ)
var content = JSON.parse_string(file.get_as_text())
print (content)
return content
pass