Dialogue system instancing problem

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

Hello everyone! Hope all is going well for everyone :slight_smile: Anyways, I’m currently trying to implement a simple dialogue system. Where when the player walks up to an NPC and presses the talk button, a dialogue box pops up, shows it’s message, and then disappears. Pretty standard stuff. My problem is that when I try to instance the dialogue box using the signals "_on_Area2D_area_entered(_area: Area2D), and _on_Area2D_area_exited(_area: Area2D) the dialogue box will still appear even after I have exited the Area2d. An example of what I mean is, I’ll walk up to the NPC till I am in the area box(I have it print true, so I know I have activated the signal and false when I have left) then I’ll walk to the other side of the map and press the talk button and the dialog box pops up. What I would like to know is how I can have the dialogue box appear only when I’m inside the NPC’s Area2d and press the talk button. For whatever reason it can still be called after I have left the Area2d. Thanks in advance! I’ll add my code in another comment to keep this post clean and easy to read!

[1]: https://sta.sh/0lxys26jxas - What I want it to do

[2]: https://sta.sh/01bb40bktkzy - Still able to activate DB even over here

[3]: https://sta.sh/0x72tkml4u8 - Big collision areas are not problem either

well it seems I can’t add comments XD

here’s the code :slight_smile:

Dialogue

extends Popup

var dialog = [“dialog” , “new dialog”]
var dialog_index = 0
var dialog_fin = false

func _process(_delta):
$Text_Arrow/arrow_anim.play(“arrow_anim”)
if Input.is_action_just_pressed(“ui_accept”):
popup()
load_dialog()
pass

func load_dialog():
get_tree().paused = true
if dialog_index < dialog.size():
dialog_fin = false
$Textbox/BodyText.bbcode_text = dialog[dialog_index]
$Textbox/BodyText.percent_visible = 0
$Tween.interpolate_property($Textbox/BodyText , “percent_visible”
, 0,1,1, Tween.TRANS_LINEAR , Tween.EASE_IN_OUT)
$Tween.start()
else:
queue_free()
get_tree().paused = false
dialog_index += 1

func _on_Tween_tween_all_completed() → void:
dialog_fin = true
pass # Replace with function body.

NPC

extends StaticBody2D

const DIALOG = preload(“res://Assest/Scenes/Dialgoue_test 2.tscn”)

var can_speak = false

func _ready():
pass

func _process(_delta):
$“NPC sprite_G”.play(“NPC_idle”)

func speech_loop():
var dialog = DIALOG.instance()
get_parent().add_child(dialog)

func _on_Area2D_area_entered(_area: Area2D) → void:
can_speak = true
if can_speak == true:
speech_loop()
print(“true”)

func _on_Area2D_area_exited(_area: Area2D) → void:
can_speak = false
print(“false”)

:bust_in_silhouette: Reply From: RakuNana

Never mind. I figured it out after a lot of experimentation. The NPC and dialogue box should both be in the same scene and then instanced over. Simply use an export on your string var for your dialogue. And you will be able to change the dialogue from NPC to NPC. Now for the reason why my code did not work as I wanted it to. Simple, because I was instancing the dialogue from another scene. The dialogue box was being called every time I entered the Area2D. Even though I never pushed the talk button, it didn’t matter since the DB was already in the new scene waiting for the talk button to be called. This is why I was able to get the dialogue box to pop up , even though I was on the other side of the map. By simply putting both the dialogue box and Character in the same scene, it’s not necessary for the DB to be instanced into a new scene. Hope this helps someone else out , cheers :3