Setting (Sub?) values of custom resources

Godot Version

4.3

Question

I have a custom resource that contains some variables and I couldn’t figure out how to give them a value

this is the script I have

var Tiles : Array[Tile] #Tile is the class_name of my resource

func _ready() -> void:
	for y in 10:
		for x in 10:
			var NewTile : Tile = [ #this doesn't work
				Position : Vector2(x, y) #Position is a variable on the Resource
			]
			
			Tiles.append(NewTile)

I looked it up but couldn’t find a solution

This code doesn’t make much sense, you created NewTile variable and did this as static type as Tile, but you’re trying to assign an Array for this variable and inside this array you’re writing like a Dictionary would be. Try this instead:

var Tiles : Array[Tile] #Tile is the class_name of my resource

func _ready() -> void:
	for y in 10:
		for x in 10:
			# You need to first create a new instance of your resource
			var NewTile := Tile.new()
		
			# Now you can set the variable of your resource
			NewTile.Position = Vector2(x, y)
			
			# And append the new tile
			Tiles.append(NewTile)

But you store anything else than a Vector2 inside this resource? Otherwise do this as a resource is just make things overcomplicated, you could just store the vector directly. Also i strongly recommend you study more about GDScript before attempt to do anything.

Thanks for the help
I think the part I was missing was the Tile.new()

I really hadn’t looked at how funny that code was

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.