Problem with Adding and Removing child nodes

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Zerfex

So I’m very new at this, so any help is gladly welcome. I’ve run into a problem when wanting to add an instantiated scene as a child node, getting the error that ‘“p_child” is null’. Also, when wanting to delete that child node, by using ‘queue_free()’ it appears this error: ‘Invalid call. Nonexistent function ‘queue_free’ in base ‘Nil’’.

For context, I want to select the scene corresponding to a random generated number, to later add it as a child to an already created node, for it to appear in the game. I suppose there will be better ways to do it, so I’ll appreciate them.

First of all, I preloaded all the scenes I was going to use:

var F_T0 = preload("res://Scenes/Terrains/First/F_Terrain0.tscn")
var F_T1 = preload("res://Scenes/Terrains/First/F_Terrain1.tscn")
var F_T2 = preload("res://Scenes/Terrains/First/F_Terrain2.tscn")
var F_T3 = preload("res://Scenes/Terrains/First/F_Terrain3.tscn")
var F_T4 = preload("res://Scenes/Terrains/First/F_Terrain4.tscn")
var F_T5 = preload("res://Scenes/Terrains/First/F_Terrain5.tscn")
var F_T6 = preload("res://Scenes/Terrains/First/F_Terrain6.tscn")
var F_T7 = preload("res://Scenes/Terrains/First/F_Terrain7.tscn")
var F_T8 = preload("res://Scenes/Terrains/First/F_Terrain8.tscn")
var F_T9 = preload("res://Scenes/Terrains/First/F_Terrain9.tscn")

Then, depending on the number generated, I assigned a variable previously written to that instantiated scene:

match terrainFirst:
		nextTerrainOne = F_T0.instance()
		nextTerrainOne = F_T1.instance()
		nextTerrainOne = F_T2.instance()
		nextTerrainOne = F_T3.instance()
		nextTerrainOne = F_T4.instance()
		nextTerrainOne = F_T5.instance()
		nextTerrainOne = F_T6.instance()
		nextTerrainOne = F_T7.instance()
		nextTerrainOne = F_T8.instance()
		nextTerrainOne = F_T9.instance()

Lastly, I added that variable as a child of the node I wanted:


And when the camera leaves the corresponding added scene, I make it dissappear, because the game is an infinite runner:


I’ve done my research but I’m not yet able to solve it.

:bust_in_silhouette: Reply From: LeslieS

If the error is saying (exactly) p_child is null then the variable p_child has not been given a value.
It will be impossible for anyone to debug and help you if you don’t show us the code that contains the variable p_child.
This is why over on reddit, you were asked to show us the code with that variable.
Is it possible that you are thinking the error is in one place, when it actually is in another?
You can click on the error message and the editor will take you to the exact line it fouls on.

As far as I know, the error appears where I added the variable that contains the corresponding instanstiated scene as a child of the node. The one I showed earlier:


The lines of code where that variable is used are those which I’ve shown (except of course of the usual declaration). For that reason, I believe the variable has been given a value, the scene, so I don’t know what I fail to understand.

Zerfex | 2022-12-18 22:03

The error is telling you that a variable named exactly p_child is null.
Nowhere in the code you are showing is that variable.
It is irrelevant what tPositionOne is or what nextTerrainOneis because the error is saying p_child.
If tPositionOne was the problem the error would say tPositionOne. But it doesn’t. It says p_child.
Find in your code the variable p_child
Scour your code, somewhere there is a variable named p_child. That is the problem.

Regarding the queue_free() error.
If terrainFirst does not equal 0,1, 2, … 9 then nextTerrainOne will be null.
When using match you should usually have the default match even if it is to just print out an error message.

    print("no terrain match ", terrainFirst)

LeslieS | 2022-12-19 00:11

Okay, I just noticed my error. It was very simple in reality, it had nothing to do with any
p_child variable or anything like that. It was all well written. I suppose I had a rookie mistake with the assignation of non-existent values to some variables.
Thanks for the replys, I really appreciate it!

Zerfex | 2022-12-19 17:13

it had nothing to do with any

The error message you posted must have just been a diversion tactic by the developers of GODOT.

Glad you got past that trap.

LeslieS | 2022-12-19 18:52