0 votes

So I have this dialogue box made and its script is this:

extends Control

var dialog = [
"Hello World",
"You are stupid",
"Boo"
]

var dialog_index = 0

func _process(delta):
if Input.is_action_just_pressed("ui_accept"):
    visible = true
    load_dialog()


func load_dialog():
if dialog_index < dialog.size():
    $RichTextLabel.bbcode_text = dialog[dialog_index]
    $RichTextLabel.percent_visible = 0
    $Tween.interpolate_property(
        $RichTextLabel, "percent_visible", 0, 4, 1,
        Tween.TRANS_LINEAR, Tween.EASE_OUT_IN
    )
    $Tween.start()
else:
  queue_free()  
  visible = false
dialog_index += 1

and the dialogue box is working fine, but then i have this StaticBody2D object that has this script attached

extends StaticBody2D


func interact():
    load("res://scripts/DialogBox.gd")

now what happening is that I have a character who has a raycast, and if raycast is colliding with the StaticBody and Z is pressed, this function gets called, when i press it once everything works but I cant make it work the second time and im really frustrated xD
how do i fix this? ;-;

in Engine by (27 points)

1 Answer

+1 vote
Best answer

Why are you doing it like this? You have a very weird approach. You are loading a script, but you're not instancing it. The reason it only works the first time is that by the second time, the script is already loaded (I don't really understand what you mean by "it's working fine", as there's no way this actually works since the dialog isn't added to the tree. Perhaps you mean that the dialog scene is working on its own, but that's not what you're loading, you're only loading a script...). I suggest you take some time to read the tutorials, they're very well done and you shouldn't skip them.

In your current case, you should look into scene instancing: https://docs.godotengine.org/en/stable/getting_started/step_by_step/scripting_continued.html#instancing-scenes

What you should be loading (and then instancing) is the scene (probably named DialogBox.tscn). A script is, well, just a script file. It could be attached to any node, in any scene. You can't load a script and expect it to fetch the scene you think it should be attached to. Instead you load the scene itself and instance it, and add it as a child.

"im really frustrated"

No wonder, you don't understand the basics and you skipped the tutorials. I would be frustrated too.

by (1,246 points)
selected by

OH SO THATS WHERE THE TUTORIALS WERE
okay thank you so much :0
im gonna go and read them

No problem! And sorry if I sounded snappy, I wasn't in my best mood. Best of luck!

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.