Hi all, I'm new to Godot and recently returned to my project that I have started last year. I've been trying to create a reusable button and use it in my GUI scenes. I created a separate scene round_button.tscn which has the following structure:
Control
└─ RoundTextureButton
└─ CenterContainer
└─ IconTextureRect
RoundTextureButton is a TextureButton that reacts to mouse hover and IconTextureRect is a TextureRect that is meant to be an icon with texture set as an AtlasTexture. I made the following script that is attached to Control:
extends Control
class_name RoundButton
enum IconID { play, pause, back, forward, … }
const ICON_REGIONS = [
Vector2i(64, 0),
Vector2i(64, 16),
…]
@export var icon_id: IconID = IconID.play
@onready var icon_rect: TextureRect = $RoundTextureButton/CenterContainer/IconTextureRect
func _ready() -> void:
icon_rect.texture.region = Rect2(ICON_REGIONS[icon_id], Vector2i(16, 16))
This creates a dropdown in Godot GUI which allows me to comfortably set the type of a button. It works as expected when I run the button scene.
Then I Instantiate Child Scene… in my menu scene menu.tscn to add a few buttons to my game menu. One button works fine, but when I add two or more, they all have the same icon, the last button that I added and set. What am I missing here with sharing attributes? I read that exported arrays are shared by all instances because the variables are really pointers to some memory location, but my icon_id is an enum value. To test this I have put a print statement in _ready() and it seems like the buttons have distinct values, but the icons are still shared by all of them.
I'm guessing it something about the texture you use not being a unique resource. You mighwant to learn about "make unique" and the like, but i'm not sure.
It was "Local to Scene" tick in the AtlasTexture's Resource group that had to be checked to make the atlas unique to an instance. Thank you.