The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates. | Twitter

0 votes

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?

in Engine by (24 points)

2 Answers

0 votes

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.

by (22,674 points)

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

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?

0 votes

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.

by (10,634 points)
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 Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.