Godot Version
4
Question
Sorry if this has been asked before. I am mostly new to video game dev stuff and am learning as I go. I’ve been playing around with instantiate() a bit and have come across that you are able to remove the child node once it’s added with either queue_free() or remove_child(node). I have not been able to get any of these to actually remove the instanced node from the screen once the trigger for it occurs.
For more details, the trigger spawns a sprite from a different scene - works fine, once the trigger to despawn it is met, I am generally faced with an error, specifically:
p_child->data.parent !=this
This error only occurs if I use remove.child(node), if I use queue_free() instead I don’t receive any errors, but neither does the sprite disappear.
Any help on this would be greatly appreciated. The idea is that if a character is a certain distance of the x axis, the trigger should happen, then if they move back, the spawned item should disappear. Sorry if I am missing something that should be inherently obvious.
The test code is as follows:
extends StaticBody2D
@onready var _animated_sprite = $AnimatedSprite2D
@onready var PlayerInfo = get_tree().get_first_node_in_group(“player”)
@onready var text_box_scene = load(“res://Box.tscn”)
var box_spawned = false
var box
func UpdateAnimation():
_animated_sprite.play(“Lant_Idle”)
box = text_box_scene.instantiate()
if PlayerInfo.global_position.x > global_position.x +20:
$AnimatedSprite2D.flip_h = true
box.global_position = PlayerInfo.global_position
box.global_position.y = box.global_position.y -400
if box_spawned == false:
get_tree().root.add_child(box)
box_spawned = true
elif PlayerInfo.global_position.x < global_position.x - 50:
$AnimatedSprite2D.flip_h = false
if box_spawned == true:
box.queue_free()
box_spawned = false
func _physics_process(_delta):
UpdateAnimation()