How to access popup button texture

Godot 4

Hello again, I am wondering how to change the texture of the main button (the parent?) of the popup menu button on action. I can now get the ids of the children but am not sure how to grab the main texture. I tried the below code with both (.texture_normal and then .CompressedTexture2D after the errors)

Any help greatly appreciated!



func _ready() -> void:
	var popup: PopupMenu = petmenu.get_popup()
	popup.id_pressed.connect(_on_id_pressed)


func _on_id_pressed(id: int) -> void:
	print(id)
	if id == 0:
		Game.selectedPet = "AHound"
		GameMechanics.SelectActivePet()
		petmenu.CompressedTexture2D = load("res://icons_bee.png")

Look into setting Control’s theme properties.

The relevant parts for your petmenu button is:

Which you would call somewhat like:

# 1. Create stylebox that uses a texture.
var stylebox = StyleBoxTexture.new()
# 2. Assign your new pet image to the texture property.
stylebox.texture = load("res://icons_bee.png")
# 3. Override the "normal" state theme property of button with your new stylebox.
petmenu.add_theme_stylebox_override("normal", stylebox)

You’d probably have to override the button styles (or states) such as “pressed”, “hover”, “focus”, etc, until it looks up to par with what you envision.


An alternative is to use the theme editor to facilitate creating all these states for your textures. And then assign the resulting theme:

if id == 0:
    Game.selectedPet = "AHound"
    GameMechanics.SelectActivePet()
    petmenu.theme = load("res://AHoundTheme.tres")

Lastly, just noticed the res path: res://icons_bee.png. Or do you just want to set the button’s icon? Which looks like the following:

petmenu.icon = load("res://icons_bee.png")
1 Like