How do I call the buttons pressed() in code

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By funlamb
:warning: Old Version Published before Godot 3 was released.

I have made some buttons.

enter image description here

I have the black bar move to the selected button with code.

var list_position #keeps track of where we are in the list
var list_buttons #buttons in the list
var list_size # size of the list so we don't call list_buttons.length

func _input(ev):
    if ev.type == InputEvent.KEY &&  not ev.is_echo() && ev.is_pressed():
	    if ev.scancode == KEY_DOWN:
	    	list_position += 1
	    elif ev.scancode == KEY_UP:
    		list_position -= 1
    	if list_position > list_size:
    		list_position = 0
    	elif list_position < 0:
    		list_position = list_size
    	if ev.scancode == KEY_DOWN || ev.scancode == KEY_UP:
    		set_selector_pos(list_position)
    	if ev.scancode == KEY_RETURN:
    		list_buttons[list_position].pressed() # error message I'm getting here is:
# Invalid call. Nonexistent function 'pressed' in base 'Button'.
    		print ("Enter")
	pass

I have connected my button in the editor like so:

enter image description here

func _on_one_player_pressed():
	    print ("Hello")
	    pass # replace with function body

The button works when I use my mouse to click on it.

What am I don’t wrong?
Is there a better way to make a list of buttons that can be selected with a keyboard?

:bust_in_silhouette: Reply From: eons

Button does not have a function “pressed”, is is_pressed (read docs, believe autocomplete).


With the help of vbox you don’t need to set the key up and down behaviour, just the neighbour of the buttons to get the focus (not sure how works but works).
Then just use button’s focus_enter signal to move the bar (only one will get focus at time).

And there are some properties you can toggle to prevent controls from getting focus and leave only what you want to in case your menu got more complex.

Button doesn’t have pressed() but BaseButton does. It’s actually _pressed(). I tried that to but that didn’t work either.

funlamb | 2017-03-24 17:46

_pressed() is a virtual function, you want to set_pressed(bool) in that line?

eons | 2017-03-24 19:18

It still doesn’t seem to connect to what I want it to do.

			list_buttons[0].set_pressed(true)

funlamb | 2017-03-24 20:41

I think maybe a better way to word my question is this.

I need to press a button using my mouse or keyboard. How do I get the same effect of scrolling through my list and then hitting enter?

I can’t use a VButtonArray because I have all the buttons animated.

funlamb | 2017-03-24 21:06

:bust_in_silhouette: Reply From: Noshyaar

As far as I know, virtual functions like _pressed() cannot be called directly unless you override it with your own handler.
Try using emit_signal(“pressed”) function to simulate button pressing action. Still, emitting signal this way won’t trigger _pressed() virtual function.

This is it. I just needed a way to “press” the button with the enter key.

funlamb | 2017-03-25 19:13