How do you only delete one duplicate of a node when using add_child()?

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

I’m making a small farming game to get a better understanding of the godot engine, and I made it so there is only a certain number of seeds one packet would have. Everything runs smoothly until I buy two seed packets but use up one. When I do that, the other packet deletes as well. I’m not sure how to fix this, so if anyone does that would be a big help :D.

(code for deleting the packet, inside the object itself)

func _process(delta):
if Global.cornseeds == 0:
	queue_free()

(code for adding in the seed)

if $shopmenu.item3owned == true:
		get_tree().current_scene.add_child(corn)
		$shopmenu.item3owned = false

check if you’ve instanced all your seeds or seed packets individually, or maybe if you’re using duplicate() add the DUPLICATE_USE_INSTANCING flag. That way if you change the original object you’ve duplicated from, it does not change the new object

CakeLover | 2022-12-15 20:46

:bust_in_silhouette: Reply From: jgodfrey

Based on the above code, the problem would seem to be that all instances of your seed packets are looking at the same, global seed count value. And, when that value reaches 0, each instance automatically deletes itself.

To fix that, you’ll want each independent instances to track its own seed count. So, rather than having a single, global variable - each seed packet object should have its own script that tracks its own number of remaining seed. With that, the instance will delete only itself when its seed count reaches 0, but the others will remain.

I really appreciate this! I am using signals now to connect, so this could be the root of my problem, but it still has yet to work. I’m not sure how clear I was with my wording, because it isn’t every type of seed packet that it deleting, but ones of the same type. The others remain just fine but the ones of the same type, say corn, would delete both packets even though I’m only using one. If you have anything that could be helpful that would be great!

raeeeeeeeeee | 2022-12-16 22:21

So, looking at the code you posted above, here’s what I think is happening:

You have multiple “Corn” seed packets, each with this script:

func _process(delta):
    if Global.cornseeds == 0:
        queue_free()

So, in every frame, every single “corn seed” packet looks at the single, global value Global.cornseeds and when its value reaches 0, the object deletes itself. So, as soon as that variable reaches 0, every packet that’s monitoring that variable will delete itself.

You can’t have MULTIPLE instances of same object all monitor the SAME value and expect them to behave independently.

Again, to solve this, you’ll need to track the count in each packet independently. That could be done in the the local script associated with each packet, or in a global script, where each packet has its own unique counter to reference and update (likely in a dictionary).

It’s hard to make any more specific suggestions without seeing all the related code.

If the project is available somewhere for inspection, I’d be happy to take a look and (potentially) offer additional input.

jgodfrey | 2022-12-16 22:45