Handling item drop across a tree structure several nodes deep

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

I have a few nodes where upon death, need to drop another node:

ie:
A dead tree drops logs.
A dead goblin drops bones.
A dead apple drops a rotten_apple, etc.

My tree looks like: game → map → tree (several instances of tree)

Because a tree, apple, or goblin is an independent scene, I don’t know how to make
it initiate a instance of the item replacing it - into the game.

Should it send a signal to the game or map upon death, and then let the game or map run code to instance the new node?

:bust_in_silhouette: Reply From: Dlean Jeans

Should it send a signal to the game or map upon death, and then let the game or map run code to instance the new node?

That’s one way.

Another way is using Node.owner which is the root node in the saved scene (the Game).

func drop():
    var loot = LootScene.instance()
    owner.add_child(loot)

If the trees, goblins or apples are spawned at runtime, you may need to manually set the owner to the Game node.
Say a tree drops an apple:

func spawn_apple():
    var apple = AppleScene.instance()
    owner.add_child(apple)
    apple.owner = owner # the tree's owner is probably the game

Thank you very much, I’ll try this.

mattkw80 | 2020-08-08 16:01

Thanks Jeans…

I went with the owner.add_child way of doing it.

The signal way might be more proper… but I’m already connecting enough signals with my map does my procgen… I’m getting sick of tracking them all.

Your owner.add_child method is easy enough…

mattkw80 | 2020-08-08 19:44

Hmm had to abandon this, heading back into using Signals.

I realized when I have the ‘apple’ make the new instance of ‘rotten_apple’ and try to push it into ‘Game’… I have no way of hooking ‘rotten_apple’'s signals up to ‘Game’. At least not in anyway that I can make work.

mattkw80 | 2020-08-09 00:22