how to code a button to turn on and off blinking in a sprite?

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

I’m new at coding, so I was looking for how to do a code for a sprite node (named Godot) with a single button (named One) that toggles if it blinks or not.

Here is the code I have:

var seen = 0
    
func _on_Timer_timeout():
	if seen % 2 != 0:
		visible = not visible
	if seen % 2 == 0:
		pass

func _on_One_pressed():
	var godot = get_parent().get_node("Godot")
	godot.visible = not false
	seen += 1

This works as I wanted but I was wondering if there exists a more efficient way to do it rather than constantly checking if the variable seen is an odd number or not

both the timer and One nodes are connected to the sprite node

:bust_in_silhouette: Reply From: jgodfrey

Yeah, that can definitely be cleaned up. Here’s an example using a scene tree that looks like this:

Node2D
    Button
    Sprite
    Timer

The following script is associated with the Node2D node:

extends Node2D

var blink = true

func _on_Timer_timeout():
	if blink:
		$Sprite.visible = !$Sprite.visible

func _on_Button_pressed():
	$Sprite.visible = true
	blink = !blink

Thanks! it’s much better

Cloudurian | 2023-03-19 15:55