Godot timer that changes the countdown

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

I am currently working on a project where the player will come across a green light. This is meant to flicker on and off at certain times for the player to work out a code that will unlock a door. for example. - On (wait 0.5 seconds) off (wait 0.5 seconds) on (wait 2 seconds) on
so on. I just need help with the code to use. i have tried using the timer node but i wanted to know if there is an easier way. i can provide more info but just seeing what people know thanks so much.

:bust_in_silhouette: Reply From: Magso

Create an array of floats for the code.

var code : Array = []
export var code_length : int
export var min : float
export var max : float
var button_held : float
var code_number : int

func _ready():
    #create the code
    for i in code_length:
        code.append(randf()*(max+1)+min)
    light_code()

#custom function to flash the light
func light_code():
    for i in code_length:
        if $Light.light_energy < 1:
            $Light.light_energy = 1
        else:
            $Light.light_energy = 0
        yield(get_tree(). create_timer(code[i]), "timeout")

#repeat the code back with input
func _process(delta):
    if input.is_action_just_released("your input"):
        if button_held > code[code_number]-0.1 && button_held < code[code_number]+0.1:
            code_number+=1
        button_held = 0
    elif input.is_action_just_pressed("your input"):
        if button_held > code[code_number]-0.1 && button_held < code[code_number]+0.1:
            code_number+=1
        button_held = 0
    else:
        button_held += delta