Godot Engine error: Invalid set index 'text' (on base: 'null instance') with value of type 'String'

518 views Asked by At

i'm trying to create a text "cutscene" into my game. i added an instance of the scene with my textbox (which works) to use it in this case as well but the same code gives me an error message in the title reffering to the line 42 (label.text = ""). i tried to comment out the part that gives me an error but it doesn't seem to work.

extends Control


const CHAR_READ_RATE = 0.05

onready var textbox_container = $TextboxContainer
onready var label = $TextboxContainer/MarginContainer/HBoxContainer/Label

enum State {
    READY,
    READING,
    FINISHED
}

var current_state = State.READY
var text_queue = []

func _ready():
    print("Starting state: State.READY")
    hide_textbox()
    queue_text("placeholder text")

func _process(delta):
    match current_state:
        State.READY:
            if !text_queue.empty():
                display_text()
        State.READING:
            if Input.is_action_just_pressed("ui_accept"):
                label.percent_visible = 1.0
                $Tween.remove_all()
                change_state(State.FINISHED)
        State.FINISHED:
            if Input.is_action_just_pressed("ui_accept"):
                change_state(State.READY)
                hide_textbox()

func queue_text(next_text):
    text_queue.push_back(next_text)

func hide_textbox():
    label.text = ""
    textbox_container.hide()

func show_textbox():
    textbox_container.show()

func display_text():
    var next_text = text_queue.pop_front()
    label.text = next_text
    label.percent_visible = 0.0
    change_state(State.READING)
    show_textbox()
    $Tween.interpolate_property(label, "percent_visible", 0.0, 1.0, len(next_text) * CHAR_READ_RATE, Tween.TRANS_LINEAR, Tween.EASE_IN_OUT)
    $Tween.start()

func change_state(next_state):
    current_state = next_state
    match current_state:
        State.READY:
            print("Changing state to: State.READY")
        State.READING:
            print("Changing state to: State.READING")
        State.FINISHED:
            print("Changing state to: State.FINISHED")

func _on_Tween_tween_completed(object, key):
    change_state(State.FINISHED)
    get_tree().change_scene("res://scenes/room1.tscn")

1

There are 1 answers

2
Theraot On

Godot is telling you that it cannot set the property text of a null instance (to which you are trying to assign an String).

Since this is happening in this line:

label.text = ""

We must conclude that label is null here.

When is this running? I see the line belongs to the hide_textbox method which is called from _ready and _process...

This suggest that either:

  • This failed:

    onready var label = $TextboxContainer/MarginContainer/HBoxContainer/Label
    

    I have to point out a couple things about this:

    • You have your question tagged as Godot 4, however in Godot 4 we use an @onready annotation not an onready keyword.
    • I would have expected this line to fail with an error.

    Regardless, I'm going to suggest to use an Scene Unique Nodes for the label. This feature was also added to Godot 3.

  • The label was set to null by another script, which I would not be able to tell you about.

  • The method hide_textbox is being called by some other script, before @onready and _ready gets to run, which I cannot tell you about either.