How would I set the atlas region for a texture button?
-
so without going into too many details(Details are below the line) 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?
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)
-
If you make the textures for the buttons, atlas textures you can then go
Button.texture_normal.region = Rect2(x,y,width,height)The same goes for
Button.texture_disabled
Button.texture_focused
Button.texture_hover
Button.texture_pressed -
@Salodo .texture_normal.region doesn't seem to be a method
(it says .texture_normal.region is an invalid set index and when I try to Print() it says .texture_normal.region is an invalid get index, I have also replaced the . with an _ with the same negative results)
do you have any other ideas?
-
It is not a method it is a property of the atlasTexture
Please remember to make the texture an atlasTexture
I have made a video to show this, Pleas take not I am using godot 4.1 but it should not matter