Trying to set a TextureButton's ShortCut with GDScript

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

Trying to be able to toggle between modes with this button so that I can change it’s details (including which shortcut triggers it) depending on mode but it’s currently not working. I can set any details I want to in the mode function but the game is ignoring my attempts to set a button short cut.

onready var button_1 = TextureButton.new()

func set_button_mode( mode: int ) -> void :
	#combat mode
	if mode == 0 :
		var button_key = InputEventKey.new()
		button_key.set_scancode(KEY_Q)
		button_1.set_shortcut( button_key )


func _ready() -> void:
	set_button_mode(0)

Also I notice that when I set the key with the editor, it shows in the the tool tip. Does any one know if this behavior is automatic or if I need to manually set this with GDScript if I change it latter?

I found a way. if I build a shortcut in the editor, then save it, Then clear the short cut in the editor, I can load the saved file it into my button. It works but I’m not sure what the difference is??? I wish the documentation was clearer…

func set_button_mode( mode: int ) -> void :
#combat mode
if mode == 0 :
	button_1.set_shortcut(load("res://UI/ButtonShortcuts/Q_KEY.tres"))

monsterousoperandi | 2022-10-15 22:19

:bust_in_silhouette: Reply From: monsterousoperandi

I ended up attaching a script to my buttons to make it work.

In my button manager node I did this:

func set_button_mode( mode: int ) -> void :
	#combat mode
	if mode == 0 :
		button_1.set_skill_shortcut(KEY_Q)

Then in the button script I created a method that would set the short cut:

func set_skill_shortcut( scancode : int) -> void :
		var button_shortcut : ShortCut = ShortCut.new()
		var button_event : InputEventKey = InputEventKey.new()
		button_event.scancode = scancode
		button_shortcut.set_shortcut(button_event)
		self.set_shortcut(button_shortcut)

Seems odd that you have to create a shortcut inside the short cut… Yo dog, heard you liked short cuts…
Any who, it worked!

1 Like