0 votes

Hello! I am creating a farming game and I am currently working on creating farmland with instancing. Whenever I make an instance of an object, it appears in-front of my player instead of behind it. That is, whenever I walk onto the farmland, it goes in front of my player instead of my player going on top of it! I was wondering if there is a way to configure it so that a newly instanced scene can be drawn behind another node.

Godot version 3.4.4
in Engine by (12 points)

2 Answers

0 votes

You can control global position of any object, just do it at the moment of instantiating it. If I misunderstood - show the code

by (7,447 points)
0 votes

It sounds like your problem is related to where you're adding the new scene in your scene tree. Essentially, the items in the tree are drawn from the top of the tree down. So, things that are "lower" in the tree are drawn later (and therefore on top of) items that are higher in the tree.

Armed with that knowledge, you should see that you can control the draw order of items (and thus, which items are on top) by controlling the order in which they're shown in the tree.

When inserting the new scene, you're probably using add_child(). That adds the child at the bottom of the tree - effectively making it draw on top of other nodes. To impact that, you have a few choices...

First - you can use add_child_below_node(), which will insert the new node directly below the specified node. Using that, you can control exactly where you want your node to be added (by locating it with respect to some other node).

Or, you can move an already added node in the tree via move_child().

However, possibly even simpler would be to create a container node to hold your "farm objects" and always add your farm nodes to that container. As long as that container node is above the Player node in the tree, everything it contains will be behind the player when the scene is rendered. So, something like this:

Main Scene
   +- FarmObjects
   +- Player

Then, when you add new farm objects, do it like $FarmObjects.add_child(). That way, it'll be a child of the FarmObjects node and will always be drawn behind the player.

by (14,876 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.