0 votes

I'm trying to add a dialog box to my game, and I'm having trouble implementing it so I can use it with my sign. What should I do?
It keeps spitting this error at me: Invalid set index 'dialog' (on base: 'PackedScene') with value of type 'Array'.
This is my code:

extends Area2D

export(Array, String, MULTILINE) var dialog
export(float) var textSpeed = 0.05
var canInteract = false
const dialogBox = preload("res://Assets/Objects/DialogBox.tscn")

func body_entered(body):
    if body.name == "Player":
        canInteract = true

func body_exited(body):
    if body.name == "Player":
        canInteract = false

func _ready():
    connect("body_entered",self,"body_entered")
    connect("body_exited",self,"body_exited")
    dialogBox.dialog = dialog
    dialogBox.textSpeed = textSpeed

func _input(event):
    if Input.is_action_pressed("interact") and canInteract == true:
        canInteract = false
        var dialogInstance = dialogBox.instance()
        get_parent().add_child(dialogInstance)
Godot version 3.4
in Engine by (27 points)

1 Answer

+1 vote

You can't set variables of a PackedScene. You set the variables of the instance of the PackedScene.

func _ready():
    connect("body_entered",self,"body_entered")
    connect("body_exited",self,"body_exited")

func _input(event):
    if Input.is_action_pressed("interact") and canInteract == true:
        canInteract = false
        var dialogInstance = dialogBox.instance()
        dialogInstance.dialog = dialog
        dialogInstance.textSpeed = textSpeed
        get_parent().add_child(dialogInstance)

Note that you should also make sure to clean up the instances you create at runtime with queue_free().

by (3,665 points)

I was trying that before, and it wasn't working as no box would show up.

It might not be positioned correctly or stuff might be drawn on top of it or something along those lines. Check out the remote scene tree debugger to see exactly what's going on while running your game:

https://docs.godotengine.org/en/stable/tutorials/debug/overview_of_debugging_tools.html#remote-in-scene-dock

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.