12

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.

top 5 comments
sorted by: hot top new old
[-] polotype@lemmy.ml 4 points 1 week ago

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.

[-] blamster19@programming.dev 4 points 1 week ago

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.

[-] copygirl@lemmy.blahaj.zone 4 points 1 week ago

I believe it's the icon_rect.texture that is shared across all buttons? In the inspector, select "Local to Scene" for that texture. It'll ensure to create unique copies for each instance of the scene.

[-] blamster19@programming.dev 3 points 1 week ago

Yes, that was it. Thank you! So even though the instance has unique variables, the settings of a texture are not unique by default?

[-] copygirl@lemmy.blahaj.zone 3 points 1 week ago

Yes. Resources are what's know as a "reference type", just like nodes are, and even when you use dictionaries in code. Multiple places can point to the same resource, node or dictionary, and changing a property on it will reflect that change anywhere it is referenced.

For resources this often makes sense, because there are a lot of cases where you want that behavior. For example if you have a background texture that's reused across all buttons (plus an icon that's unique). Maybe down the line you want to update the background texture and have that reflect on all buttons. Then you don't have to go through all your buttons and individually update them.

Reuse also has implications for performance and storage / memory used, but optimization shouldn't concern you as much at this point.

this post was submitted on 01 Aug 2026
12 points (92.9% liked)

Godot

7826 readers
28 users here now

Welcome to the programming.dev Godot community!

This is a place where you can discuss about anything relating to the Godot game engine. Feel free to ask questions, post tutorials, show off your godot game, etc.

Make sure to follow the Godot CoC while chatting

We have a matrix room that can be used for chatting with other members of the community here

Links

Other Communities

Rules

We have a four strike system in this community where you get warned the first time you break a rule, then given a week ban, then given a year ban, then a permanent ban. Certain actions may bypass this and go straight to permanent ban if severe enough and done with malicious intent

Wormhole

!roguelikedev@programming.dev

Credits

founded 3 years ago
MODERATORS