+1 vote

I'm new in Godot, and I have a specific node for dialog boxes, but how do I make it so that when I click on an NPC or after a certain thing happens, the dialog appears on the screen?

   extends ColorRect

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

   var dialog

   var phraseNum = 0
   var finished = false


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

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

       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 _process(delta):
       $Indicator.visible = finished
       $Indicator/AnimationPlayer.play("Pingu")
       if Input.is_action_just_pressed("click_left"):
           if finished:
               nextPhrase()
           else:
               $Text.visible_characters = len($Text.text)

   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 (13 points)

1 Answer

0 votes

This code is quite advanced, You are asking for easiest part of it :)

One way to do it is to design signal for your dialog events with dialogPath and dialog position passed along signal. This would work with some high scope DialogMenager node, who will be connected to this signal, and will create new DialogBox at dialog position and pass dialogPath to it. Signal will be emitted by whatever You want, be it clicking NPC or reaching some area, dying and so on. Important thing with signals is to use Autoload as absolute emitter, so DialogMenager won't have to connect to every single dialog initiator.

by (5,557 points)
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 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 webmaster@godotengine.org with your username.