0 votes

Please help! My dialog box is not working properly. I tried to follow some tutorials on yt and worked but the dialog box seems to stack each other child node. So the dialog box keeps restarting. But when i leave the collision area i can end the dialog

heres the code for the npc i used

    extends Area2D

var can_interact = false
const DIALOG = preload("res://Scenes/DialogueBox.tscn")


func _on_NPC_1_body_entered(body: Node) -> void:
    if body.name == "Player":
        can_interact = true

func _on_NPC_1_body_exited(body: Node) -> void:
    if body.name == "Player":
        can_interact = false

func _input(event: InputEvent) -> void:
    if Input.is_action_just_pressed("ui_accept") and can_interact == true:
        var dialog = DIALOG.instance()
        get_parent().add_child(dialog)

heres the code for the dialogue box

extends ColorRect

export var dialogPath = ""
export(float) var textSpeed = 0.05

var dialog

var phraseNum = 0
var finished = false

func _ready():
    $Timer.wait_time = textSpeed
    dialog = getDialog()
    assert(dialog, "Dialog not found")
    nextPhrase()

func _process(_delta):
    $Indicator.visible = finished
    if Input.is_action_just_pressed("ui_accept"):
        if finished:
            nextPhrase()
        else:
            $Text.visible_characters = len($Text.text)

func getDialog() -> Array:
    var f = File.new()
    assert(f.file_exists(dialogPath), "File path does not exist")

    f.open(dialogPath, File.READ)
    var json = f.get_as_text()

    var output = parse_json(json)

    if typeof(output) == TYPE_ARRAY:
        return output
    else:
        return []

func nextPhrase() -> void:
    if phraseNum >= len(dialog):
        queue_free()
        return

    finished = false

    $Name.bbcode_text = dialog[phraseNum]["Name"]
    $Text.bbcode_text = dialog[phraseNum]["Text"]

    $Text.visible_characters = 0

    while $Text.visible_characters < len($Text.text):
        $Text.visible_characters += 1

        $Timer.start()
        yield($Timer, "timeout")

    finished = true
    phraseNum += 1
    return
in Engine by (19 points)
edited by

1 Answer

0 votes

Can you share a link to the tutorial you used? And try to format the code so it's more legible to us? (Highlight it, then click on the brackets above the text input box here).

by (168 points)

https://www.youtube.com/watch?v=EgHlpayakUA
https://www.youtube.com/watch?v=GzPvN5wsp7Y&t=270s

Here are the two tutorials i used..

sorry for the bad code display. I think i fixed it

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.