How would you remove a single object from a bunch of instantiated objects?

let’s say you instantiate a bunch of objects in a scene (2d mostly), how would you go about removing a single object from them without deleting the whole lot?

been thinking about it for a few days and i can’t really see an answer for it anywhere on the internet. please let me know if anyone has an answer! thank you :slight_smile:

Iterate through get_children(), check if the current element has the name of the node I want to delete, then queue_free that node? The answer depends greatly on your specific situation.

2 Likes

There are several ways to do it. I tend to favor having my objects determine when they are no longer needed, and having them queue_free() themselves. That’s not always practicable, but it’s very useful when it can be done.

1 Like

i was using queue_free(), but it was sort of removing everything instantiated which i didn’t want. the mechanic is basically picking up water droplets to grow something, and i don’t all the objects disappearing at once lol

Presumably, you catch the droplet with something? Like a bucket or something? In that case, if the droplet has a collider, iterate through get_overlapping_bodies and maybe use a deferred queue_free?

If you want the water droplets to go away when you pick them up, then queue_free() them when your pickup object collides with them.

i used this tutorial if that helps
what i just added from my end was a function to instantiate water droplets with a timer and the player picks and drops by pressing a button

In IMO that is not a good tutorial - to instantiate then remove and instantiate an instance for the purpose of moving/hiding/showing it, makes no sense at all.

You can instantiate and handle the position property of that instance to move it on the screen. Also, if you want to show or hide a Sprite2D or any other node that inherits from CanvasItem you can toggle the visibility property, like so:

var some_sprite_2d: Sprite2D = $reference

func hide_sprite() -> void:
     some_sprite_2d.visible = false

func show_sprite() -> void:
     some_sprite_2d.visible = true

In regards to your question, it has already been answered, so I’m not going to repeat it. It was pretty well explained by @TokyoFunkScene

2 Likes

alright, thank you all for your help!

1 Like

Were you able to make it work ? … Don’t hesitate on making farther questions. I would suggest on going small and build upon that.

What I mean is: you could first try to create a drop item without worrying about any other thing. No character or other objects. Try changing its position, try showing it and hiding it. Get to understand HOW to do those things. Once you understand that part, you can jump into adding a bit more complexity by creating a character on the screen that interacts with this object. Go step by step, even more experienced developers do it exactly like that. If it takes long, that is fine. To build - even simple things, takes time :slightly_smiling_face:

ill be giving it a shot this week! recovering from some medical stuff but i thought id try to atleast figure out what i would need look into the documentation or where to begin when i start to try it atleast!
and thank you for your advice! im sure ill get there soon :smiley:

1 Like

Let me know how it goes - if you want of course :grimacing:

i will!

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