Godot Version
4.6
Question
I am making a game where the player can build a base out of tiles. Enemies will attack the base and be able to destroy these tiles. The tiles will have different durability and texture values, for example: wood with a durability of 2, and bricks with a durability of 4. I need a way to have many similar, but different tiles and am trying to find an efficient implementation. I am working in 2D
I was considering having an overarching Tile class that takes in a resource that stores durability and texture. I attach the script to a new scene, then add the resource. After saving this scene I would add it to a TileMapLayer that stores what tiles the player can place. I’ve hesitated implementing this because it seems like it will create a lot of extra scenes and resources.
Are there better, more efficient implementations? Thank you in advance!
This is so broad and has so many equally valid solutions. Maybe just make your Tile class extend Resource directly and refer to it from another overarching resource singleton for TileConfigurations?
Saving it as a scene does not entirely parse for me yet.. will this Tile class refer to atlases in your Tileset?
Maybe the most important patterns to remember in challenges like this are:
- composition over inheritance
- data driven design
Not all challenges require the same thinking, but one like this is best tackled by completely modeling out the data structures that you need. Ergo: compose a set of resource classes first, before writing code that handles the data.
You only need a way to store data in accesible way, use dictionary for storing each tile’s data this way:
var tiles_data = {}
func _ready():
tiles_data.set(Vector2i(x, y), ["Wood", 2])
and you can use resource to store texture & max hp, then compare if tiles_data.get(Vector2i(x, y)) have 0 hp, then do what you want to do when monsters break the tile, hope this helped
I just implemented it and it seems to be working, thank you!