How do I make something blink on and off each second?

Godot 4.1

It’s simple. I’m trying to make a “click anywhere to start” label blink on and off each second. It’s just- it turns on, and it turns off… and that’s it. Here’s my limited code, if you need it.
extends Label
var check = 0
func _ready():
get_tree().create_timer(1.0)

func _on_timer_timeout():
if check == 0:
visible = true
check = 1
get_tree().create_timer(1.0)
if check == 1:
visible = false
check = 0
get_tree().create_timer(1.0)

I’d recommend an AnimationPlayer or Tween instead.

func _on_timer_timeout() is by default a linked signal function. Each time you create a new timer, it isn’t going to be linked to your script by default, you would need to do that manually.

But this is “bad code” and is going to result and a new timer node every second your game runs. What you should do instead is user the same timer and continuously pause/unpause it or stop/start it. Both of these are built in features of timer and can be read about in the documentation here: Timer — Godot Engine (4.3) documentation in English

However, if you ensure the timer is NOT a one-shot (either in the inspect panel or explicitly in your script with timer.set_one_shot(false) ), then it will automatically repeat and you won’t need to pause/unpause it every second.

Your final code for this could look something like (assuming you pre-create the timer node and link its on_timer_timeout signal, as well as leave one-shot as false):

func _on_timer_timeout():
if check ==0:
     visible = true
     check = 1
if check == 1:
     visible = false
     check = 0

Create a RichTextLabel and check the BBCode box. Then in the text box add something like this:


[center][color=red]
[pulse freq=1.0 color=#ffffff40 ease=-2.0]Click Anywhere[/pulse]
[/color][/center]

Read up on how to you can make it really flexible here.

Somehow I hadn’t ever thought of using AnimationPlayer! I’ll try it and report back here.

Thanks! The AnimationPlayer worked, sorry for such a simple question! I’m pretty new to Godot, so I’m not the best. Thanks a lot!!!