Is there a way to change the visible layer that a object is on?

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

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.

:bust_in_silhouette: Reply From: Inces

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

:bust_in_silhouette: Reply From: jgodfrey

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.