Regarding stylebox

Godot Version

v4.2.1.stable.official

Question

I’m trying to change the stylebox texture dynamically in code using this:

extends Panel

var selected_tex = preload("res://UI/items/HBactiveSlot.png")
var selected_style : StyleBoxTexture = null

func _ready():
	selected_style = StyleBoxTexture.new()
	selected_style.texture = selected_tex

func refresh_style():
	if TrackInventory.active_item_slot == slot_index:
		set('custom_styles/panel', selected_style)

#Then in another script I call this:

extends Node2D

func _ready():
	for i in range(slots.size()):
		TrackInventory.active_item_updated.connect(slots[i].refresh_style)
		slots[i].slot_index = i
	initialize_inventory()
	
func initialize_inventory():
	for i in range(slots.size()):
		if TrackInventory.inventory.has(i):
			slots[i].initialize_item(TrackInventory.inventory[i][0], TrackInventory.inventory[i][1])/>

#I was wondering if maybe its that custom_style property has changed in godot 4 but I dont know where to find the documentation. I tried using setstylebox instead but it didn’t work or maybe my error is in another place all together lol, thanks in advance.

this is the video I was following for reference -

Try changing this line
set(‘custom_styles/panel’, selected_style)
to
set("theme_override_styles/panel", selected_style)

And please use the code tag button </> to format your code when posting here.

PS: If you hover your mouse over the property you are changing it will display the path to it.

Thanks for the tips, I edited the original post for clarity, also what you suggested for the code works perfectly except it made me realize I neglected to implement a way to remove the texture after moving to the next slot. I tried to find the property in style documentation and stylebox etc. but i found something in the theme docs which is a clear() function but I’m not sure how to call it. Doing set(“theme_override_styles/panel”, theme.clear()) returns null error. Would you have any idea about this aspect? Maybe I’m trying the wrong thing with clear(), I’m not sure

I think just setting the theme override to null returns it to its default.
(not 100% sure of it)
set("theme_override_styles/panel", null)

1 Like

It works perfectly lol, I even tried similar lines to the one you suggested but never exactly that one. its always funny to me how close we are to the solutions and how simple they always are, thank you for the help, I appreciate