+2 votes

Hi . How to independent Child position from Parent Position !?

by default , child node position in Godot is relative to its Parent node , but I want to move the child node in global coordinates without moving its parent , so what should I do !?

asked Jun 26, 2016 in Engine by RezaPouya (110 points)
edited Jun 26, 2016 by RezaPouya

then, why did you put that child node to parent node?
why not just move child node to grand parent node?

because I want to know there is any possible way to do this or not !?
and maybe I want to control them through their specific parent rather to access them one by one in root node in some point of my game ....

for example ; I have a star ship that fire Lasers , I want to add laser to Star_Ship node rather than root node of level ...

my game work fine by adding generated child node ( for example : lasers ) to root node but having this possibility may be useful in later ...

3 Answers

+2 votes
Best answer

I believe you put the parent-child relationship for easy access. A parent-child relationship is primarily implemented for the ease of transformation, so if a parent's transform changes, all children are affected by it.

Solution 1: You need not implement any parent-child relationship if you don't want the transformation hierarchy.

Solution 2: Godot has additional features like Groups for easier node access. So you can group all the enemies to an "enemy" group and you can call a method on all the enemies at once by

get_tree().call_group()

Solution 3: If you want to keep your parent-child relationship for any other matter and also want to move items globally, all you need to put the parent in the world origin i.e., Vector2(0,0) for 2D and Vector3(0,0,0) for 3D, set scale to 1 and set rotation to 0. Then all your child transformations will become the same as global ones.

Solution 4: Then again, if you want the solution 3 and also can't keep the parent on origin, you can use global transformation functions like

global_translate()
set_global_transform()

etc..

answered Jun 26, 2016 by vinod (746 points)
selected Jun 26, 2016 by RezaPouya

In Godot, hierarchy is far more important than just transforming. It also affects how objects are structured, how they are accessed and inherited. So it makes sense to have a child node drawing things in global space, if that node is part of a scene.

A typical example is particle systems. They explicitely have a local_space flag just for that.
I'm struggling myself trying to use the _draw() method to display custom things in global space.
The workaround I use is this:

var inv = get_global_transform().inverse()
draw_set_transform(inv.get_origin(), inv.get_rotation(), inv.get_scale())

And it doesn't even works if the parent has a negative scale, my drawings get reflected far away beyond the origin of the world...

Note: however, I don't think bullets should be child of their shooter. 1) because they are instanced, 2) because they interact with the world, not just the shooter.

OK. I didn't think that deep into custom drawing.

So if we build a particle system, with children drawing in global space, how about putting a node at world origin and draw into that.

I usually keep a good structure but will be a little bit flexible. For my projects it does work better.

Thanks for your help , this is an answer that I will bookmark it in my browser ...

0 votes

Well, that's the essence of parent-child relationship. Is there a reason your child needs that specific parent? You should make a one common root node and reparent the child. A nasty hack would be to counter the parent movement by negative movement.

answered Jun 26, 2016 by GlaDOSik (810 points)

thanks , this would work but I just want to know :

is there a way to break position relationship between parent and child !? ( just Position relationship ) ...
I like to access to a function that allow me move my child node relative to root node ( world ) ...

look like currently there is not such functionality in Godot ...

a situation :
in RPG game , there are a Witch that can make magical monsters which follow player and attack him independently from their parent ( witch ) , if player kill the witch then all of her/his magical monsters should die with him/her .
for sure we can spawn magical beast in world and just as separate and independent entity/node and in every frame check if their logical parent is alive or not and act acordingly .... ( there should be extra coding and using more resource for this )

but it would be better if we add them Parent node ( Witch ) and move them independently to parent node and if the parent dies , they they will die as well ( if parent get Node.free() then all of his children will die as well )

Then you create a node called "Witch", a child called "Main" and another one called "Summons". That way, once the "Main" died (which would be the main body) you just delete "Witch".

I don't think that is a good use of parent/child nodes. If you have an RPG game with enemy characters, they should all be siblings and be the children of one main node that holds all of them. If a witch spawns magical monsters under her control those monsters should be children of the main node that holds all the enemies, when they are spawned they can be added to an array of all her summoned monsters, and when she dies it's just a simple for loop through the array to kill all the summoned monsters as well. I think this would make a lot more sense than making the monsters direct children of the witch node, and it does not take up more processing power, because you don't need to check it every single frame, you just do it once when the witch dies.

Your example with Witch is not a good example for parent-child relationship. There are more better ways of how to do it. I would make a scene called World, another scene Witch would be a child of World and Monster would be also a child of World. That's a proper game structure.

There is a powerfull feature called groups. Simply add spawned beasts to the group and when the Witch dies, just call your free function over the whole group. No need to check condition every frame. You can do something similar with signals. Every beast listens for your custom free signal and when the Witch dies, just emit the free signal.

+6 votes

You can call set_as_toplevel on any CanvasItem (or anything that inherits from that class) to make it ignore its parent's transformations. There's also the RemoteTransform node, but that's more about imposing an extra transform than removing a parent one.

Check these docs for (slightly) more info.

answered Jul 12, 2016 by jackmakesthings (314 points)

For the father of Godot, why this one not selected as best answer?
For the most cases you will need to reset nodes global position after that, but it's not a big deal. This answer clearly covers the topic instead of selected one. Thanks, jackmakesthings.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.