0 votes

Hi everyone,

how can I enable or disable a CheckButton via code? Is there any simple way like "input pickable true/false" I use with collision shapes? I played around with control focus modes but didn't succeed.

For example: I'd like to have a timer starting when button down / holding down and after a certain time the button shouldn't react at release anymore. Just as if it was never put down in the first place...

There must be a simple command for that, right?

in Engine by (492 points)

How about changing the button mask to 0 and restoring it to the previous value to enable it?

Hi Splunk, that's another hint I never thought of, thank you. "Mouse Left" is checked... how could I change "button_mask" to 0 and back on again in code? That wouldn't be a true/false case, would it? (I am indeed a beginner!)

1 Answer

0 votes

If you want to disable the button, you can use your_button.disabled = true

by (3,495 points)

Hi p7f,

this leads to the error
Invalid set index 'disabled' (on base: 'GDScriptNativeClass') with value of type 'bool'.

I'm not sure what I'm doing wrong here...

Can you share your code?

Certainly!

extends CheckButton

var timer

func _ready():
    timer = Timer.new()
    timer.set_wait_time(2)
    add_child(timer)
    timer.connect("timeout", self, "_on_timer_timeout")

func _on_CheckButton_button_down():
    print("button down / timer started")
    timer.start()
    CheckButton.disabled = true

func _on_CheckButton_button_up():
    print("button released / timer stopped")
    timer.stop()

func _on_timer_timeout():
    timer.stop()
    print ("Time is up!")

func _on_CheckButton_gui_input(_event):
    if Input.is_action_just_pressed("mousebuttonclick"):
        print ("action just pressed")

func _on_CheckButton_mouse_exited():
    print ("mouse exited")

you are using CheckButton.disabled. CheckButton is a clase, not a node. If that checkbutton is in your tree, then it should be $CheckButton.disabled

But i guess you want to disable the scene having that script (as the script extends checkbutton), then you should simply call disabled = true

Thank you! disabled = true indeed works (it appears that sometimes I don't seem to trust things to be possible in the simplest way...).

(On a sinde note: It's so weird that $ all too often leads to another error of "Node not found". I stumble over this all the time and I'm sure I'm not alone. Is it because I'm trying to find the same node I attached the script to? Can I only find different nodes?)

What I see now is that even when disabled the mouse-exited still works. I thought that any input to the node should be completely turned off, say, until I turn it on again. (Just as it works fine with CollisionShapes and input_pickable.) Is there any way to do that?

I guess the only way to do that woud be, inside the _on_CheckBox_mouse_exited funciton, return if you see the button is disabled.. like

func _on_CheckBox_mouse_exited():
    if disabled:
        return

    # do all the stuff here

About the side note, you use $ to find nodes that are children of the node with the script attached. Is like a shortcut of get_node

It seems much clearer now, thanks a lot! I think I should be able to tweak things with some variables now that I know how to turn the thing off and on : )

$ is only for children then, but I can reach other nodes with their (complete) path or "find node", right? Addressing the node I work within doesn't need a "path" to be found, clearly. I need more practice... ; )

There is a loosly connected issue I'd like to ask you about if you don't mind. Maybe you'd have an idea I couldn't think of yet... is there any way to directly send you a message here?

you can contact me on Twitter (@AzagayaVJ) or discord (azagaya#5894).

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.