Godot Version
4.3
Question
I am running into some trouble when instantiating a bunch of scenes. Here is my code:
func load_saved_particles():
for particle_id in particle_data.keys():
if not particle_data[particle_id]["collected"]:
var particle = sense_particle.instantiate()
add_child(particle)
particle.particle_id = particle_id
particle.original_pos = particle_data[particle_id]["position"]
particle.global_position = particle_data[particle_id]["position"]
I am getting an error on this line every time it is called:
add_child(particle)
The error I am getting is as follows:
E 0:00:13:0535 salmon_sense.gd:33 @ generate_new_particles(): Condition "!is_inside_tree()" is true. Returning: false
<C++ Source> scene/main/node.cpp:835 @ can_process()
<Stack Trace> salmon_sense.gd:33 @ generate_new_particles()
salmon_sense.gd:15 @ _ready()
scene_manager.gd:66 @ _set_current_scene()
scene_manager.gd:47 @ _on_poll_timer_timeout()
When looking in the remote inspector, this error appears immediately when entering the add child function and upon starting the ready function for the particle scene. Interestingly, there are absolutely no problems with functionality. All of the scenes are created added to the scene tree correctly and are all running exactly as expected, they just throw this error when they are created.
None of the forum posts I have found have offered a solution that has worked for me, and while this isn’t blocking me from continuing to work, I would love to stop having 400+ errors in my debug console from this function. Any help would be greatly appreciated.
Thank you so much for your time!
Edit: It is worth noting that I instantiate and creates scenes in nearly identical ways elsewhere in my project with no issues. When stepping through this process with the debugger I can see that this error is thrown as soon as add_child is called, and the particle is added to the scene tree, but before the ready function for the particle is called.
There error says something about “generate_new_particles()”. What does that function look like and where is it called?
1 Like
always thing you should try is adding child by deferred call
add_child.call_deferred(particle)
(should work)
if not help,
connect
code that edits particle to its ready
signal
var particle = sense_particle.instantiate()
particle.ready.connect(func()->void:
particle.particle_id = particle_id
particle.original_pos = particle_data[particle_id]["position"]
particle.global_position = particle_data[particle_id]["position"])
add_child(particle)
1 Like
Ah that was my mistake. There are two functions calls in this node’s ready function, both do very similar things and both create the same error.
Here is the ready function:
func _ready():
Globals.salmon_sense = self
if GameSaveLoad.player_data.salmon_sense_data:
particle_data = GameSaveLoad.player_data.salmon_sense_data
load_saved_particles()
else:
generate_new_particles()
And here is the generate_new_particles() function.
func generate_new_particles():
var count = curve.point_count
for p in range(count):
for i in range(particle_mult):
var particle = sense_particle.instantiate()
add_child(particle)
var pos = curve.get_point_position(p) + Vector2(randi_range(-15, 15), randi_range(-15, 15))
var particle_id = str(i) + "_" + str(p)
particle.particle_id = particle_id
particle.original_pos = pos
particle.global_position = pos
particle_data[particle_id] = {
"collected" = false,
"position" = pos
}
The primary difference being that the first one loads from the particle_data dictionary, while the other one populates the dictionary. But regardless, both functions will show the same error every time add_child() is called. Thank you for taking a look!
Thank you for the suggestions! We have already tried add_child.call_deferred(particle)
, with no effect.
As for your second suggestion, I gave that a try as well and it did not produce different results. In my mind this was expected because the error is not to do with the assigning of values to the node, in fact regardless if I call add_child before or after assigning the particle’s properties, we still get this error and the particles all still behave in the correct fashion.
As I said in my edit, when stepping through the process using the debugger I am able to see that this error is thrown immediately when stepping into add_child(), before any @onready vars are populated and before the ready function is called.
Honestly, the thing that confuses me most is that have nearly identical code elsewhere in our project for instantiating scenes, that runs very similarly, and yet does not produce the same errors.
We found the solution! Leaving here for reference for the weary traveler who may come across a similar problem.
Our issue was not in my “salmon sense” script but rather in the particle scene I was having trouble instantiating. Among other things the scene contains an AudioStreamPlayer and a VisibleOnScreenEnabler2D. As detailed in this open Github issue, the solution was to make sure that the VisibleOnScreenEnabler sits below the AudioStreamPlayer in the scene tree.
Here is what ours looked like before:
And here is how we rearranged the scene to fix the bug:
Hope this helps someone in the future!