Move a node in the tree

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

Hello All

I am trying to get a flying monster to leave blood on the ground. I can spawn a sprite with blood and I found in the documentation something called move_child ( Node child_node, int to_position )and I have tried using it. Unfortunately if I put 99 as the int to_position it goes beneath the floor and I dont know how to choose the right number to make sure its above the floor but below my character etc.

Can someone explain how I know which integer to add to this when I spawn the blood?

:bust_in_silhouette: Reply From: jgodfrey

To do this in a flat tree structure (where all nodes of interest have the same parent), you need to have references to a few things:

  1. The parent node of the node you’re trying to move
  2. Some “anchor” node in the tree where you want to move your node (either above or below the anchor). Based on your description above, this could be your “floor” node, and it seems like you could move your “blood” node just above it.

Assuming the script containing the code is already associated with the “parent” node, here’s some code to get you started:

var target_loc = $Floor.get_position_in_parent() # find the position of the Floor in the tree
move_child($Blood, target_loc) # move the Blood node to just above the Floor in the tree

Alternatively, depending on your tree organization, you might be able to create some container node in a different branch of the tree that’s always in the correct “render” location for your blood. Then, when instantiating to blood node, just add it as a child of that container. Assuming the container is in the correct location in the tree, you’re done. No need to “move” the just added blood node secondarily.

Thank you!!!

YuleTide | 2022-04-13 16:04