Am I doing (Loops, Holders, and Windows) correctly or is there a better way?

Godot Version

4.4.1 Stable

Questions

I apologize if this has been asked somewhere else (I couldn’t find it) I’ve been working on a pretty complicated project and I’m new to Godot. I have a lot of experience in other engines/languages and I am writing this in GDScript. I’m trying to determine if there is some function or better way of doing what I’m doing or something obvious I am not using.

The project has a modular UI that controls other modular UI elements and also the stage as well as how NPCs react to it.

  1. I am constantly using for loops to get/set things from my manager objects. Am I missing a better way or is just how it is? See below:
for items in get_tree.get_nodes_in_group("StageObjects"):
     if items.name=="Door":
           items.door_is_open = true

I use a lot of this because game objects access managers, and those managers handle the logic. Is this normal? I’m used to the Gamemaker way which allows you to bulk access by doing something like: Door.door_is_open = true

  1. I am constantly using “Holders” which are Node2Ds and then adding children to them. I do this to make sure certain things are on top of other things. In my levels, I now have a “PlayerHolder” because I want to have a background and a foreground.

    It works, and it’s fine, but is there some other way I should be doing it? I’ve considered making an insert_child(node1,node2) function but that seems to be more work than just using these holders.

  2. I think the game UI is a bit claustrophobic. The game window is 1366x768 and looks good. If I expand the window size to 1920x1080 I don’t want the 1366x768 to stretch or change at all. I just want to add a border on all sides. I’ve naturally tried to change the window size, and the stretch settings, but I don’t really see anything like “Set Main Node2D to center”. So I’m trying to get some ideas on how to resolve this. This would be a lot easier if I wasn’t using pixel art. Is there some setting or anything I’m missing that could help?

  1. What exactly would be the point of iterating here? If you need to access a specific node, keep a reference to it.

  2. No need for any “holders” or similar “tricks”. If you want to insert a node inbetween other nodes, use add_sibling(). However you don’t need to rely on node order for drawing. All canvas item nodes have the z index property that determines their position in the drawing queue. For even more control, you can use CanvasLayers. This may be the best solution if you want some elements sandwiched between background and foreground layers.

  3. You can render the whole game into a SubViewport and use a centered SubViewportContainer node to display it at fixed size at the center of the screen. This way you can even have a paspartout that displays some pattern/graphic.

As a general advice, try to ditch the habit of using “managers”.

  1. The point of iterating is to get all items within a group when you don’t know how many items there are as things are added by the player. But you’re right, I shouldn’t need to do this for truly unique items. Other items will still have to be handled this way as the player adds them to a scene and at best you store the references in an array (which you would have to loop over anyways)
  2. I did try CanvasLayers, but found that this didn’t affect the render order. So there must be a setting somewhere that is toggled or a setting I changed that I forgot about, I’ll keep looking. I did use the z-index for one portion but I didn’t want to rely on it.
  3. Extremely helpful. I’ll look into it.
  1. Post your actual use cases if you need more specific advice. In general, you should minimize iteration in script code as much as possible. Most typical tasks that require iteration can be delegated to engine’s fast native code via various api calls.

  2. CanvasLayer’s layer property determines the drawing order.

1 Like

Thank you!

1 Like