How to make a child position independent from Parent Position !?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By RezaPouya
:warning: Old Version Published before Godot 3 was released.

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 !?

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

volzhs | 2016-06-26 09:20

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 …

RezaPouya | 2016-06-26 10:38

:bust_in_silhouette: Reply From: GlaDOSik

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.

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 )

RezaPouya | 2016-06-26 10:42

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”.

mateusak | 2016-06-26 14:51

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.

CowThing | 2016-06-26 15:50

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.

GlaDOSik | 2016-06-26 16:59

:bust_in_silhouette: Reply From: vinod

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…

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.

Zylann | 2016-06-26 14:47

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.

vinod | 2016-06-26 15:07

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

RezaPouya | 2016-06-26 15:54

:bust_in_silhouette: Reply From: jackmakesthings

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.

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.

deusalgor | 2020-02-15 02:14

:bust_in_silhouette: Reply From: Mrpaolosarino

On the parent, Add a node as a parent of the child.

:bust_in_silhouette: Reply From: MichiruNakam

Add the child you want to be in global coordinates as a child of a plain Node (the most basic node that has the gray icon), so you have your node → a plain Node as child → the node you want in global coords.
I think this solution is the best since it does not require to reparent your parent node