Noob question about instancing and variables

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.

12 points · 5 comments · view on lemmy.world

5 Comments

polotype@lemmy.ml · 4 pts · 12d (1 reply)

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 pts · 12d

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 pts · 12d (2 replies)

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 · 2 pts · 12d (1 reply)

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 pts · 12d

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.