How to Create Custom Grid Resource?

Godot Version

4.0

Question

Hello,

I have created a custom tile resource, and I would like to create a custom map resource that is essentially a grid of the tile resources. I would like for this map to be saved in the game files so that it does not need to be regenerated every time the game is run.

Normally I would create a grid on runtime using something like

@export var tile: Class_Tile
@export var size:= 9
@export var map = []

func _ready():
	map = []
	for x in range(size+1):
		var x_cont = []
		for y in range(size+1):
			var new_tile = tile.duplicate()
			x_cont.append(new_tile)
		map.append(x_cont)

My problem is that I don’t see a reason to do this every time the game is run, I would like to have this grid structure saved in the game files if possible. How could I go about doing this?

I can clarify if this is confusing, I apologize for not really knowing how to word my problem.

Thanks in advance!

Hi again, answering my own question because I found the solution and want to leave an answer posted in case someone has the same problem.

This is solved using @tool and set. Note that you will have to restart the editor after adding the @tool handle in some versions, I think its a bug. Anyways this code creates a grid in the editor that is saved to the game files as a custom grid resource. :slight_smile:

@tool
extends Resource

class_name Class_Grid

@export var grid: Array
@export var tile: Class_Tile
@export var size: int:
	set(value):
		SetGrid(value)
		size = value
		
func SetGrid(value):
	grid = []
	for x in range(value): 
		var x_cont = []
		for y in range(value):
			var new_tile = tile.duplicate()
			x_cont.append(new_tile)
		grid.append(x_cont)