• How would I set the atlas region for a texture button?

I want to set the Region Size for a Texture button and then move that region around. I have tried searching up the answer but I can't. I tried a code like this but I'm pretty sure that's just not right. Can somebody please tell me the way to do this? Thanks

button.region_custom_minimum_size = Vector2(16,16)
button.region_position = Vector2(spriteWidth * (spellIndex - 1), 0)

If you need context, I am making a game where you have spells you can unlock and choose to place in a loadout so you can quickly use that spell I have an array that tells me what the current spell loadout is.
depending on the Array I will have a different Button Texture for each spell slot, All my spell textures are on one sprite sheet and I use an atlas texture for the Textured button. When the loadout changes I want to move the atlas texture region to one of the spells on the SpriteSheet.||spoiler||

This is the full code if you need it also I do know I could set the region in the editor but just to be safe I'm asking here.

extends Control

@export var sword: Sprite2D

@onready var spellloadout = Global.SpellLoadout

func _ready():
	# Sets the spell loadout on another (Delete this later)
	spellloadout = [1, 2, 3, 4, 5, 6]
	
	for i in range(1, 7):  
		var spellIndex = spellloadout[i - 1]  
		var button = get_node("CanvasLayer/SpellButton" + str(i))

		var spriteWidth = 16                                                                                                                      
		var spriteHeight = 16 
		
		button.region_custom_minimum_size = Vector2(16,16)
		button.region_position = Vector2(spriteWidth * (spellIndex - 1), 0)

Do you have an AtlasTexture assigned? You can do it like this:

extends TextureButton

@export var regions:Array[Rect2]
@export var index := 0

var texture:AtlasTexture

func _ready() -> void:
	texture = texture_normal as AtlasTexture
	pressed.connect(on_press)

func on_press() -> void:
	index += 1
	if index >= regions.size():
		index = 0

	texture.region = regions[index]

Here, I'll upload my little test project so you can see the setup:

colors.zip
6kB

You'll want to make sure Normal, Pressed, etc. all reference the same AtlasTexture resource, but also make sure that each button has its own AtlasTexture resource to modify so that they do not modify each other. The Texture2D with your spritesheet though can be shared between them all.

    award it took me a while to make it work but using some your code helped me, Thank you so much.