Control and 2D nodes

Godot Version

4.4.1

Question

So I’ve heard you’re not supposed to make node 2Ds the children of control nodes and vice versa. Is there any specific reason for this? If its not a particularly big deal, it’ll stop me from rewriting quite a bit of code, so I’d appreciate someone helping me understand the main reasoning behind it.
(Specifically, I’m using labels as children of node 2Ds and Node2Ds as children of containers, these are two separate cases)

This mostly depends on the context.
There is nothing stopping you from mixing node types, if there is a good use case.
E.g. sometimes you want to have a control node as a child of a Node2D, when you display a label or health bar on top of the player, moving with the player on screen.

I’m not sure when you’d want a Node2D as a children of a container, but I might have done something similar before.

Mixing different types of nodes can be an indicator for issues with the architecture, but still, there are use cases - it depends on what you are trying to achieve.

1 Like

I’ve got a game board, where the boar dis made from Hboxcontainers and Vboxcontainers. My entities that would go on the board are node 2D scenes. I’m wondering if I can contain these entities within the gameboard made from control nodes

Node2Ds do not obey Control node sizing/containers so that will not work.

What solution will work then? Is there a possible workaround to this? Could I easily transfer the scripts from 2D to control?

Control and Node2D both inherit from CanvasItem, and have a Position/Global Position property, you may be able to use the same script on either node or change what the script extends.

If you want to use UI containers then you will have to use Control nodes, otherwise you can calculate a grid position using the snapped function, but its tough to say what the best solution is without much more detail.

There’s a few specific reasons:

  1. As @gertkeno mentioned, if you plan on Node2D objects to be inside Controls, they don’t respect sizing. So that’s the main reason not to do that combination.
  2. A common question asked on here (I’ve answered the same question at least twice in the last week alone) is, "Why can’t I click on my Control that’s a child of a Node2D? Since both take clicks, you have to be very careful about the order of nodes in your tree. But if you place a Control under a Node2D, the Node2D is going to intercept clicks - if they overlap on the screen.
  3. This also goes for other things in the scene tree that are lower in the order (higher in the scene tree). You can get around this by making a CanvasLayer object and putting the Control objects underneath that, but it still needs to be above everything else in the z-order, which generally means it needs to be lower in the scene tree.
  4. Canvas Item (2D) Shaders are really meant for certain Node2D nodes (like Sprite2D), and do not work the same for all Control nodes. In particular, they do really weird things when applied to things like Buttons and HSliders (sometimes doing nothing at all when they work everywhere else), but generally work fine for things like Panels and Labels.
  5. Weird things happen when you combine node types. There are certainly times where it can work and makes sense, such as putting a label above a character’s head in a 2D game. As a rule of thumb, it’s usually ok to mix node types as long as you don’t expect the player to interact with them. For example, a HUD that isn’t interactable can be placed pretty much anywhere in the node hierarchy as long as it’s visible, and as long as nothing is clickable on it. An inventory screen however, will have issues if there are things on top of it based on the z-order.

Those are the things I can think of off the top of my head.

In the game you are describing, I’d recommend figuring out how to make your game objects work as Control nodes if you like the organization that Control nodes provide.