Cant call the script more than once

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By literallynick_

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

extends Control

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

var dialog_index = 0

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

func load_dialog():
if dialog_index < dialog.size():
	$RichTextLabel.bbcode_text = dialog[dialog_index]
	$RichTextLabel.percent_visible = 0
		$RichTextLabel, "percent_visible", 0, 4, 1,
  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():

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? ;-;

:bust_in_silhouette: Reply From: Bernard Cloutier

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:

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.

okay thank you so much :0
im gonna go and read them

literallynick_ | 2020-11-11 15:22

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

Bernard Cloutier | 2020-11-15 20:26