How do you use button press/mouse click to move an image (e.g. on/off of switch)

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

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!

:bust_in_silhouette: Reply From: kidscancode

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

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.

hinasis | 2018-09-12 07:12

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.

kidscancode | 2018-09-12 16:34

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.

hinasis | 2018-09-12 16:55