Best Architectual Practices for Using the `TileMapLayer` Node Programmatically?

Godot Version

4.4.1

Context

Hi! I’m new to using the Godot game engine. I have experience programming in C++, and I completed the GDExtension tutorial from the official documentation.

I’m interested in using the TileMapLayer node in my two-dimensional game.

I’m sorry if my questions are too vague; I’m trying to understand the best architectural practices for using the TileMapLayer node in code. I would appreciate any insights or examples from community members who have used the TileMapLayer node programmatically.

Questions

Suppose I want to enforce invariants on a grid that’s managed by a TileMapLayer node. For example, this could mean only allowing tiles to break under certain conditions, or enforcing that it’s impossible for certain tile types to be adjacent to each other. What’s the best way to encapsulate the behavior of a TileMapLayer node?

Should I extend the TileMapLayer node in a GDScript class or C++ class, or should I keep it as a member variable inside a wrapper class? In C++, I know that one can enforce state invariants using private members that can only be interacted with through the class’s public interface, but I don’t know if there is an idiomatic approach for this in Godot. Is it idiomatic in Godot to encapsulate a node like TileMapLayer, or is it better to extend it directly?

Additionally, I would like to write an algorithm that generates tile-based areas. I’ll probably write the algorithm in C++. Then, I think that I should expose a C++ class to Godot that implements the algorithm and interacts with a TileMapLayer? In that case:

  • Should my algorithm class own a TileMapLayer node as a member?
  • Should it extend TileMapLayer?
  • Or, should it be a factory that creates and returns TileMapLayer nodes for use in the scene?

Even if I’m creating tile maps procedurally, I would still like to benefit from the tile map editor in Godot. For example, suppose my procedural generator assembles areas composed of hand-designed rooms that were created in the Godot tile map editor. What’s the best way to reuse or reference those hand-designed rooms in code?

Please feel free to let me know if I should clarify or narrow down my questions. I’m grateful for any guidance.

2 Likes