Toggle button that changes another button (pressed) function

Godot Version

4

Question

So i have a button (pressed) that has a function but i want to click a toggle button to change the function of that pressed button.

Example:

func _on_button_pressed():
print(“a”)

func _on_check_box_toggled(toggled_on):
if toggled_on:
print(“b”)
if not toggled_on:
print(“a”)

So now the button press would print “b” and if i toggle again it would go back to print(“a”)

This is a basic example but i tried other ways for ~ week and i can’t figure this out.
Any help is appreciated, thanks.

func _on_check_box_toggled(toggled_on):
      if toggled_on:
          $button.button_pressed.connect( FUNCTION_ONE)
      else:
          $button.button_pressed.connect( FUNCTION_TWO)

I’m not sure if this is going to work apropriately, but it worth a try

i get invalid call:

Invalid call. Nonexistent function ‘connect’ in base ‘bool’.

Can you show me your code?

Screenshot_20240218_143013

If i put it like this it prints A but for B i can’t use ’ not toggled_on: ’

The $VBoxContainer/VBoxContainer/gg was a random test name at first, but it’s now
$VBoxContainer/VBoxContainer/button

no need the “()” after pirntA and printB

in the if it should be:

$button.button_pressed.connect(printA)

in the else it should be:

$button.button_pressed.connect(printB)

2

Same result, if i use “else” it gives the error now

So whenever the check box is toggled, you want to connect the signal.
Do you also disconnect the signals at any time?

button_pressed is a boolean. Try the signal pressed instead.

this is very werid, toggled_on is a parameter the system will give you when you changed the toggle button, you shouldn’g do this…just use toggle_on straightly…

toggled_on = printA()

3

Indeed replacing button_pressed with pressed did the trick for thar error but the toggle now prints a b / b a untoggled

I don’t understand what you want to achieve with toggled_on = printA(). This line seems wrong.

The connect function doesn’t accept a function as parameter, but a Callable instead.
You can find some examples of how to correctly connect to signals in the documentation here: Signal — Godot Engine (stable) documentation in English

The toggled_on = printA() was to not get the error, now it’s not needed but i was experimenting

I really don’t get it, at first it prints nothing as i want, then i toggle and prints A as it should as well but then if i untoggle it goes A B.

As I have said previously:

Connecting to the signal doesn’t remove the other connected function.

I tried disconnecting them in the editor and just use the signal from the script but it did nothing, or you mean i also need to disconnect with a similar function?

Yes, there is the Signal.disconnect function.

A different approach would be to connect a single function to the button and within that function test, if the check-box is toggled on or off.

That worked finally, don’t really know if it’s the proper way to do it but it works anyway, much appreciated
4
.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.