Replicating hierarchy with nodes / scenes

Godot Version

4.1.2 .NET

Question

This question has probably been asked before, but it seems I cannot find it.

I am new to Godot and its Scene / Node / Script / Class architecture confuses me a bit, even after following official documentation.

Let’s say I have a game entity called Map. Map consists of a few layers, e.g.

  • Oceans and landmasses
  • Temperature overlay
  • Precipitation overlay
  • Winds overlay
  • Bioms overlay

The number of layers is known and fixed. The layers, however, are not static and may change over time.

In Godot I present (render) these layers as meshes (MeshInstance2D). So, under “root” node I created “Map” node. Under this “Map” node I create a number of MeshInstance2D nodes which represent the aforementioned layers:

Main
|-- Map
    |-- LandMesh
    |-- TemperatureMesh
    |-- PrecipitationMesh
    |-- WindsMesh
    |-- BiomsMesh
    |-- ...
|-- Camera
|-- ...

All these layers need some data, which they use to construct corresponding meshes. This data is created by the script attached to “Map” node.

The questions are:

  1. How to replicate this structure better?
  2. How to pass map data to child layer nodes?

It seems, that I can, at least:

  1. Add nodes in the editor, attach a script to each node, provide a function (e.g. “Setup”) in each script and then call this from “Map” node _Ready function. Aking to this:
// script LayerN : extends MeshInstance2D
CreateMeshFromData(MapData data)
{
  // This differs from layer to layer
}
Setup(MapData data)
{
  Mesh = CreateMeshFromData(data);
}

// script Map: extends Node
_Ready()
{
  MapData data = CreateMapData(...);
  GetNode("LandMesh").Setup(data);
  GetNode("TemperatureMesh").Setup(data);
  GetNode("PrecipitationMesh").Setup(data);
  GetNode("WindsMesh").Setup(data);
  GetNode("BiomsMesh").Setup(data);
  ...
}

This is the approach I am currently using.

  1. Create and add layer nodes dynamically, like mentioned here: When to use scenes versus scripts — Godot Engine (stable) documentation in English This still boils down to having a custom function (e.g. Setup) and calling it from “Map” node, so I do not feel the difference, except for possible performance penalty when creating node hierarchy from a script.

  2. Make a scene out of each layer node, load and instantiate it from “Map” node, passing map data as necessary (see e.g. When to use scenes versus scripts — Godot Engine (stable) documentation in English):

// script Map: extends Node
_Ready()
{
  MapData data = CreateMapData(...);
  landMesh = preload("res://LandMesh.tscn").instance()
  landMesh.Setup(data)
  temperatureMesh= preload("res://TemperatureMesh.tscn").instance()
  temperatureMesh.Setup(data)
  ...
}

Again, I fail to see the difference between this and #1 and #2.

I guess, I am missing a number of other ways of making this.

There is no logical difference between a node in a tree and a scene instantiated as a node in a tree. The root of the instantiated scene is a node and acts as a node for all intents and purposes. The only difference is how you create it at runtime and how you store it in your sources. The method to duplicate nodes is agnostic to this. All 3 of the ways you described are equivalent, so just do what makes more sense to you from a programming or organization point.
I would inquire about why you are using a mesh to store data, but I am scared of the answer.

1 Like

Thanks for the input. I was thinking, indeed, if these methods are equivalent or if there is “one right” ™ way to do this.

As to your question:

I would inquire about why you are using a mesh to store data, but I am scared of the answer.

I do not use meshes to store the data, I use data to construct the meshes. For example, consider data to be a set of points. I use this set of points to generate a point mesh to render it later. I then can triangulate these very points to make “regions” and I construct another mesh from data structure that represents this triangulation (like, edges and vertices) and so on and so forth. Hope it answers your question.

As long as you’re not doing this every frame I guess it’s fine. Otherwise it may be worth considering doing this in shaders.

Nope, meshes are generated once and mostly static (can only change rarely, i.e. not every frame). Thanks for your answer, I shall mark it as solution.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.