Why do my scene instantiates disappear after the next one is spawned?

Godot Version

Godot 4.2

Question

So, I decided to add a dust spawner for my pong game so that plenty of dusts could spawn at once without one suddenly disappearing. But it didn’t work

Basically, the dust spawns but when another one spawns the previous one disappears instead of finishing. I even searched up a topic that told me to go check the remote tab of the scene tree, and somehow the dust was actually spawning, yet I still couldn’t see any signs of those

Here’s the code I’m using for this, any suggestions?

Code for the dust function

func Spawn_Dust(collision_point: Vector2) -> void:
	var new_dust = preload("res://Scenes/dust.tscn").instantiate()
	%Dust.global_position = collision_point
	%Dust.emitting = true
	%Dust.restart()
	add_child(new_dust) 

Code that spawns the dust:

var collider_name: String = collision.get_collider().name
	if (collider_name == "Paddle" or collider_name == "Borders"):
		Spawn_Dust(collision.get_position())

what happens if you preload the packedScene outside of Spawn_Dust()?

var dust_scene = preload("res://Scenes/dust.tscn")

func Spawn_Dust(collision_point: Vector2) -> void:
	var new_dust = dust_scene.instantiate()
	%Dust.global_position = collision_point
	%Dust.emitting = true
	%Dust.restart()
	add_child(new_dust) 

also, did you check the remote tab yet while the game is running? Do you see multiple dust scenes inside your SceneTree?

I remember trying that, but it still gave me the same result. I could try again to see if there’s a difference

Yes, and yes

Can I also ask you about this %Dust unique name?
I believe you would set the properties of your new new_dust but you simply add it to the scene without doing anything with it

Oh! now that you mention it that unique name was actually for the original dust particles, I’m instantiating, I’ll edit it and see if it works.

I tried it, but now dust just doesn’t spawn correctly or even show up.

My %Dust uses properties from the GPUParticles2d, so I don’t know if that’s actually the problem or not

This is the code I got from doing what you said btw:

func Spawn_Dust(collision_point: Vector2) -> void:
	var new_dust = preload("res://Scenes/dust.tscn").instantiate()
	new_dust.global_position = collision_point
	new_dust.emitting = true
	new_dust.restart()
	add_child(new_dust)  

Any other suggestions or problems with that code?

Try this. I don’t know what dust.tscn looks like, so I’m making an assumption that it is derived from a particle system:

func Spawn_Dust(collision_point: Vector2) -> void:
	var new_dust = load("res://Scenes/dust.tscn").instantiate()

	add_child(new_dust)
	new_dust.global_position = collision_point
	new_dust.emitting = true
	new_dust.restart()

Sorry if I was late trying out your solution, I’ll see if it works.

IT WORKED!!! So I forgot to change the %Dust to new_dust, and set it’s values after spawning it instead of before it existed. It all makes sense now. Thank you SO much :smiley:

My question now is, how would I delete the dust particles that have finished their animation? Because now the scene tree is just having more and more cloned dust particles that have already finished, which might cause performance issues.

Have your dust.tscn instances queue_free() themselves when the animation finishes.

I’ll try that. Thanks!

It worked! Here’s the newly updated code (with comments) for anyone with the same issue:

	
func Spawn_Dust(collision_point: Vector2) -> void:
	var new_dust = preload("res://Scenes/dust.tscn").instantiate() #create dust instance
	add_child(new_dust) #add that instance to the scene tree
	new_dust.global_position = collision_point #set properties for the instance
	new_dust.emitting = true #^^^
	new_dust.restart() #^^^
	await get_tree().create_timer(2.3).timeout #wait for amount of time in the brackets (one with numbers)
	new_dust.queue_free() #after ^^^, remove dust instance to free space
var collider_name: String = collision.get_collider().name #variable to store the name of the colliders
	if (collider_name == "Paddle" or collider_name == "Borders"):
		Spawn_Dust(collision.get_position()) #spawn that dust instance

Your welcome :smiley:

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.