0 votes

I have a light switch gif that shows on and off. I've converted this to separate image files.
How can I toggle the switch between on and off by putting a button there to do so?
I don't want to change scenes, I want to keep the base frame (e.g. the room) the same and show the switch as on and off. Also I want to know the state the switch is in so I can use the info for other purposes.

Any help would be most welcome. BTW I'm new to godot.
Thanks!

in Engine by (12 points)

1 Answer

+1 vote

Do you want to click the button? If so, TextureButton is probably your best bet. You can set the texture via code using the pressed signal. Add a script to the button and connect the signal:

extends TextureButton

var on_image = preload("res://path_to_on_image")
var off_image = preload("res://path_to_off_image")

func _ready():
    texture_normal = on_image

func _on_TextureButton_pressed():
    if texture_normal == on_image:
        texture_normal = off_image
    else:
        texture_normal = on_image
by (21,979 points)

There is a simpler way, use the CheckButton node. It has Custom Constants to place on/off texture.
If you want to check if it's on/off, use CheckButton.pressed it returns boolean.

CheckButton does not have any texture-related Custom Constants, only check_vadjust and hseparation. So while you could use this button to do the toggling, you'd need a separate node to display the texture, unless you want to get into custom styles, which is much more complicated than the above.

You could use the "Icon" property, but then you have a button next to a lightswitch, which doesn't seem like what the original question was going for.

It's not correct.
I don't remember it's called constant or style, but it has two image place holder to set off/on. It's not a switch, it changes between two images even if the defaults looks like that.
Come on, i use it, even to simulate something like a switch between, false/true, cannot/can, or even physical and magical damage.

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.