Godot Version
v4.1.3.
Question
Hi, I am trying to make a game with a format similar to an RPG, and I am trying to have two options appear when the player enters an object, select one of the two and it will show the selected option. And then when you hit enter again the dialog closes and can be reactivated when you approach the object and hit enter again.
I have the code made of the object, with its collision, also of the options to be displayed and it works in part, when you press enter on the object, it shows the dialog, it shows the two options, but if you press enter on the first option , the dialogue is supported.
That is, I have labels created for the text and the options, and another label for the text that appears when the option is selected, and these labels overlap when the first option is selected, the strange thing is that this does not happen when the second option is selected option, with the second option everything works correctly and I don’t know why.
I hope I am explaining myself well, I’m very new to this
This is the code I have for the object
extends StaticBody2D
@onready var dialog_scene = preload("res://Scenes/OpcionMult.tscn")
var dialogo_instance
var opciones = ["Lorem", "Lorem2"]
var dialogo_abierto = false
func interactuar(): *[This is a function that allows object collision to be detected in another code for the player]*
if Input.is_action_just_pressed("ui_accept"):
if not dialogo_instance or not dialogo_instance.mostrar or not dialogo_abierto:
dialogo_instance = dialog_scene.instantiate()
add_child(dialogo_instance)
dialogo_instance.connect("opcion_seleccionada", _on_opcion_seleccionada)
dialogo_instance.mostrar("Lorem ipsum dolor sit amet, consectetur adipiscing elit", opciones[0], opciones[1])
func _on_opcion_seleccionada(seleccion):
if seleccion == 0:
dialogo_instance.mostrar_imagen("res://Sprites/Portadas/image1.png", "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.")
else:
dialogo_instance.mostrar_imagen("res://Sprites/Portadas/image2.png", "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.")
dialogo_instance.cerrar_dialogo()
And this is the code I have for the options (OpcionMult.tscn):
extends CanvasLayer
signal opcion_seleccionada(int)
@onready var label = $TextboxContainer/MarginContainer/VBoxContainer/Texto
@onready var option1_label = $TextboxContainer/MarginContainer/VBoxContainer/OpcA
@onready var option2_label = $TextboxContainer/MarginContainer/VBoxContainer/OpcB
@onready var portada = $Portada
@onready var resumen = $TextboxContainer/MarginContainer/Label
var seleccion = 0
var dialogo_abierto = false
func mostrar(texto, opcion1, opcion2):
label.text = texto
option1_label.text = opcion1
option2_label.text = opcion2
option1_label.modulate = Color(1, 1, 1)
option2_label.modulate = Color(0.5, 0.5, 0.5)
$TextboxContainer.visible = true
resumen.visible = false
dialogo_abierto = true
func cerrar_dialogo():
$TextboxContainer/MarginContainer/VBoxContainer.visible = false
func seleccionar_opcion(direccion):
if dialogo_abierto:
seleccion = seleccion + direccion
actualizar_opciones()
func actualizar_opciones():
if seleccion == 0:
option1_label.modulate = Color(1, 1, 1)
option2_label.modulate = Color(0.5, 0.5, 0.5)
else:
option1_label.modulate = Color(0.5, 0.5, 0.5)
option2_label.modulate = Color(1, 1, 1)
func _input(event):
if event.is_action_pressed("ui_up"):
seleccionar_opcion(-1)
elif event.is_action_pressed("ui_down"):
seleccionar_opcion(1)
elif event.is_action_pressed("ui_accept"):
if resumen.visible:
portada.visible = false
$TextboxContainer.visible = false
elif dialogo_abierto:
emit_signal("opcion_seleccionada", seleccion)
func mostrar_imagen(ruta_imagen, texto):
resumen.visible = true
var textura = load(ruta_imagen)
if textura:
portada.texture = textura
resumen.text = texto
else:
print("Error: No se pudo cargar la textura ", ruta_imagen)
If anyone knows whats wrong or can help me, I’ll be very greatful, thank you