How to override the global TextureButton on_pressed function to run a custom shrink animation?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By dap404

Currently: I have 2 different buttons, 1 (normal) texture & 1 (pressed) texture that is the original image shrunk 95%

My goal: is to cut down on my images, I have created an animation that shrinks the texture button on press by 95%, but I want that animation to run every time a texture button is pressed. I do not want to manually call the animation in every on_pressed method.

Would like to override the texture button pressed function globally and add the animation there so it happens with all texturebuttons created.

Any help or even alternate solution would be great! Thanks guys.

:bust_in_silhouette: Reply From: a_world_of_madness

You can create a subclass of TextureButton (using the extends keyword) that contains the custom logic you want to run for every button. You can also add the class_name keyword to the top, which will allow you to refer to the custom class in code by name instead of the script path, and also shows it in the class list when creating a new node:

extends TextureButton
class_name ShrinkTextureButton

func _ready() -> void:
	# Connect the button signals to the animations

func _shrink_texture() -> void:
	pass # run shrink animation

func _restore_texture() -> void:
	pass # run restore animation here

If you need to add other custom logic to a button that also shrinks, you just need to make them subclasses of the ShrinkTextureButton. Just make sure to call the super method in any subclasses that also need to override use the _ready method.

extends ShrinkTextureButton

func _ready() -> void:
	# Extra logic here

tried something similar this morning, will give it another go! Don’t believe I used the super function smh. Will let you know how it plays out! Thanks mate

dap404 | 2023-01-08 19:16

good answer here, change any buttons to shrinktextureButtons and just make sure if your using godot4 to use

button_down.connect( Callable(self, "_shrink_texture"))

But is there anyway to make this happen for all texturebuttons by default? Meaning not create a new subclass?

dap404 | 2023-01-12 19:06