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:
- How to replicate this structure better?
- How to pass map data to child layer nodes?
It seems, that I can, at least:
- 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.
-
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. -
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.