How to organize this multi-scene code

Godot Version

4.3 rc2

Question

My game has lots of maps. The controls and logic are mostly the same for each map. So i have a play screen Level that loads the map LevelData. So far so good.

var packed_scene = load(Globals.level_name)
var play_area = packed_scene.instantiate()
%LevelData.add_child(play_area)

LevelData is expected to have certain objects with certain names. Specifically path start and end points. Which are Sprite2Ds. When the Level loads the LevelData it grabs those objects and builds a path.

terrain_tilemap = %LevelData/Map/GroundTileMapLayer
blocker_tilemap = %LevelData/Map/WallsTileMapLayer
group_1_start_marker = %LevelData/Map/Paths/StartGroup1
group_2_start_marker = %LevelData/Map/Paths/StartGroup2
group_1_end_marker = %LevelData/Map/Paths/EndGroup1
etc.

All of this works. But it’s hard coded to exactly 2 entrances and exits. i want this to be configurable but for the life of me i can’t think of an elegant way to do this.

Things i’ve considered:

  • Put the start/end nodes in groups (start, end). But i don’t think group order is guaranteed so i wouldn’t know how to tie them together.
  • Store the points and mappings in a dictionary. But that’s not a node, it’s code. Not sure how Level could get access to code on LevelData.
  • Assume a max of 5 start points, allow them to be null, when Level loads the map it checks each of the expected names to see if they exist. Which works but it’s so ugly.

The whole “Godot likes composition through nodes” thing is taking me a while to get used to.

I can see that your LevelData is a scene made up of nodes. When you instantiate the play_area scene, the nodes in that scene are also instatiated. Then, the Level will go ahead and reference the nodes from LevelData by path. Am I understanding this correctly?

If you want the amount of markers to be dynamic, you’ll probably need to work with arrays. You could have var start_markers: Array = [] in level and populate it with markers from the LevelData.

Does LevelData have a script? You could implement a function called `get_start_markers()’ that returns an array of all markers in that LevelData. You would then have to process the markers in Level

I started out the same way as you, thinking I would be using composition for the most part, but it really depends on your project. Right now I am using all kinds of strategies, from composition to inheritance to state machines. Each time I check what is the most appropriate solution.

1 Like