ToggleMode Buttom Check in script

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

Hello guys,

i am with godot since some days and having my troubles with understanding the wiki properly.

I am having a project with a button which is having a onToggled script

I would like to check the state of the toggledmode (actionmode).

Even with my similiar lua knowledge I am not able to code this.

In my opinion it should look like this

Function buttontoggled
If actionmode = enabled (0)
Popup
Else
Hide

This script is not a real one out of the project and just for demonstration my idea of checking the state. Is there a way to find examples of commands like actionmode?

I think the wiki itself is well detailed but as starter you have no chance to find it out yourself.

:bust_in_silhouette: Reply From: johnygames

EDIT: In Godot you do not ask “Is the button toggled?”. The better question is: “Is the button in the pressed state?”. So, you do the following to find out:

func _ready():
    set_toggle_mode(1)

func _on_Button_toggled(button_pressed):
    if button_pressed==true:
        print('hello') # show popup
    else:
        print('goodbye') # hide popup

The idea here is to use signals to check what state your button is in. When you create a button and select it, two tabs appear on the right side of the editor (Godot 3.1 default settings). Those two tabs are: Inspector and Node. Select your button, add a new script to it and go to the Node tab. There you will see a few signals that you can use. Depending on what you want, choose the corresponding sihgnal and double click it. It will ask you to connect the signal to a node. Connect it to the button in question. Open the script you made for the button and you’ll see that a new function was created named after the signal you previously chose. This is where you place your game’s logic.

So to reacap: Signals are emitted when some action takes place. Various bits of your code receive the signal and act upon receiving it. For buttons it is possible to check their state by using such signals.

More info here: BaseButton — Godot Engine (3.0) documentation in English

Thanks for your answer. I already found out about this signals and connected functions.

The main issue is that I can’t work with the wiki properly as there are no real examples of script commands shown (f.e. using action_mode).

So the goal is to check the state of the button istoogles and depending on return to show or hide a connected node (in my case control node)

Fyrion | 2019-08-05 04:24

By action_mode do you mean the state of the button as described here?
"
ACTION_MODE_BUTTON_PRESS = 0 — Require just a press to consider the button clicked.
ACTION_MODE_BUTTON_RELEASE = 1 — Require a press and a subsequent release before considering the button clicked.
"

If so, you can get the action_mode by using get_action_mode(), which returns 0 or 1 depending on the definitions above.

Now, if toggle_mode is what you need, you can read that simply by using is_toggle_mode(). You can set the toggle_mode by using set_toggle_mode(value),
where value is either 0 or 1 BaseButton — Godot Engine (3.1) documentation in English

So, I guess you can do something like this:

func _ready():
	set_toggle_mode(1)

func _on_Button_toggled(button_pressed):
	if button_pressed==true:
		print('hello') # show popup
	else:
		print('goodbye') # hide popup

Is this what you want? If it is, please mark this answer as the best one, so that others can find it.

johnygames | 2019-08-05 05:18