0 votes

Hello Godoters,
I have been creating a text game by following the tutorials on youtube(here is the link https://www.youtube.com/watch?v=5gP1eOtR5Kg&t=1205s ) and I came to the point where I have to save the reusable scene by adding it to the GDScript here is the code:
extends Control

const InputResponse = preload("res://InputResponse.tscn")

onready var history_rows = $Background/MarginContainer/Rows/GameInfo/HistoryRows

func onInputtextentered(newtext: String) -> void:
var input
response = InputResponse.instance()
historyrows.addchild(input_response)

The problem is whenever I launch the game,the debugger pops up with the error message: attempt to call function ‘add_child ’ in base ‘null instance ’ on a null instance.

Godot version v3.2.2.stable.official
in Engine by (18 points)

1 Answer

0 votes
Best answer

A node doesn't exist at the node path Background/MarginContainer/Rows/GameInfo/HistoryRows at the time your control node that the script is attached to is _ready().

First try make sure that the NodePath listed above is valid.
https://docs.godotengine.org/en/stable/classes/class_nodepath.html#class-nodepath

If the NodePath above is valid and you are still getting the error, move the node that the script is attached to up in the scene tree so its _ready() function gets called later. Alternatively change your function, so you know that all nodes will be in the scenetree when you attempt to get them:

func _on_Input_text_entered(newtext: String) -> void:
    if not history_rows:
        history_rows = get_node("Background/MarginContainer/Rows/GameInfo/HistoryRows")
    var input_response = InputResponse.instance()
    history_rows.add_child(input_response)

More about how nodes are added to the scenetree:
https://docs.godotengine.org/en/stable/getting_started/step_by_step/scene_tree.html#tree-order

by (3,389 points)
selected by
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.