How to a a child to position2D

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

I’m working on a 2d shooter and want to add upgrade pods to my ship. I want 3 positions around the ship for upgrade pods. So I’ve added 3 Position2D nodes to my Player (the ship). I started off by instancing a child pod to the ship at the position of the Position 2D nodes. This works fine. Oh, and by the way, I put the Pos2D’s in a plain old Node (container) to keep things tidy.

var w = upgrade_pod.instance()
	w.position = $upgrade_pod_container.get_node("Position2D").position

I then figured what happens if I have all 3 places filled or say position 2 pod gets destroyed. I want to stop adding pods if there are no free spaces or add a new pod to the next available space.

I thought if I instance the pod as a child of the Pos2D, I could check if the Pos2D had a child before trying to add a new one.

This code runs (i.e. doesn’t crash) but the new pod is always added at the top left corner of the screen. I can see it as a child of the Pos2D in the ‘tree’, but it no longer follows and orients to the overall parent, Player (the ship).

var w = upgrade_pod.instance()
	w.position = $upgrade_pod_container.get_node("Position2D").position

Same result if I omit the 3rd line of code

Why doesn’t it work? and how do I make it work?

:bust_in_silhouette: Reply From: jgodfrey

Since you’re adding the pod as a child of the Position2D node, it should already be in the correct position. That is, it’ll be at the position of its parent (the Position2D). So, I think you just want to drop that positioning line. That is, don’t set the position of the newly added instance.

Oh, I see you mention it acts the same way when dropping that 3rd line…

jgodfrey | 2020-02-22 01:57

So, you’re saying that the Position2D node is in the correct position, but your spawned insteance isn’t in the correct position when you make it a child of thePosition2D?

When you run your game, is the spawned instance properly parented in the Remote scene tree?

Does the spawned instance have a 0, 0 transform?

jgodfrey | 2020-02-22 02:03

:bust_in_silhouette: Reply From: njamster

Are you using a Node or a Node2D for the container? The former does not have any transform-properties, thus acts as a barrier between its parent and children: If you move the parent, the children won’t be moved with it. Use a Node2D instead!

In your first example, you’re adding your instances as children of the ship. It is positioned relative to and will move with it’s parent, the ship.

In the second example, you’re adding your instances as children of a Position2D, which is a child of the container. And that container is independent of the ships movement, with its origin defaulting to the upper left corner of the window. So the instance is positioned relative to that corner and only moved when the container moves. However, it doesn’t move, when just the ship moves.