Changing an export variable in instantiated scene

Godot Version

v4.6.1 stable

Question

ok so i have a function

func itemload():
	for i in globalvariables.inventory.size():
		var keynode = load("res://scenes/menustuff/menu_keyboard_key.tscn")
		var key = keynode.instantiate()
		$UI/ITEMS/ScrollContainer/GridContainer.add_child(key)

i wanted to change an export variable called “icon” in the scene i just instantiated to a different one. i tried

keynode.icon = value

but it shows error

Invalid assignment of property or key ‘icon’ with value of type ‘CompressedTexture2D’ on a base object of type ‘PackedScene’.

how do i do this?

update:

changed keynode.icon = value to

$UI/ITEMS/ScrollContainer/GridContainer.get_child(i).icon = value

it seems to target the correct thing now, but it displays error

Invalid assignment of property or key ‘icon’ with value of type ‘CompressedTexture2D’ on a base object of type ‘PanelContainer (menu_keyboard_key.gd)’.

i still dont understand whats wrong

It’s trying to assign a texture to your panel still, which is what your error states.

What kind of node is icon? Is the icon a child of grid container?

I’d do something like this in your situation. you want to have variable that you change when you instantiate your scene, you’re going to have something like @onready var icon_sprite = node_path_for_node_to_change inside your key node script. Then in your UI script after you instantiate you update that that nodes texture 2d variable.

Var key = Keynode.instantiate()

Key.icon_sprite.Texture2D = desired texture

(likely a preloaded resource at the top of your script.)

1 Like

icon is an export variable inside the panel node which is the node im instantiating

ok so the issue was that i was changing keynode.icon instead of key.icon…

thanks for help anyways!

1 Like