This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
0 votes

Im trying to make a button appear when you press a key, i want to make it so when you press the key it appears and plays the animation, and if you press the same key again it plays another animation and turns off visible, i dont know how to make my code work since im pretty new at godot. Please help me! Thanks

This is the code

**

extends Button
var EscPressed = false
func _ready():
    visible = false
func _physics_process(delta):
    if Input.is_action_just_pressed("ui_cancel") and (EscPressed == false):
        visible = true
        $FadeIn.play("FadeIn")
        EscPressed = true
    if Input.is_action_just_pressed("ui_cancel") and (EscPressed == true):
        $FadeIn.play("FadeOut")
        visible = false
        EscPressed = false

**

Godot version 3.3.2
in Engine by (15 points)

2 Answers

+1 vote
Best answer

You don't need to check if the action is pressed twice. Truth be told, I'm not exactly sure why it happens either but if you check if the action is pressed twice, it won't work as expected.
Also, you don't really need the EscPressed variable. You can just use visible instead.

The code can look something like this:

extends Button

func _ready():
    visible = false

func _physics_process(delta):
    if Input.is_action_just_pressed("ui_cancel"):
        if not visible:
            # Do stuff
            visible = true
        else:
            # Do stuff
            visible = false
by (182 points)
selected by

Wow, thanks! This works just as expected. Thanks again.

At the end where the else was, my thing was just doing visible = false as soon as input was pressed and didnt wait for other animation to finish, but i did my research and found that yield can fix it

This is my final code, it now works perfectly ;)

extends Button

func _ready():
    visible = false

func _physics_process(delta):
    if Input.is_action_just_pressed("ui_cancel"):
        if not visible:
            visible = true
            $FadeIn.play("FadeIn")
        else:
            $FadeOut.play("FadeOut")
            yield($FadeOut, "animation_finished")
            visible = false
0 votes

I am not sure if it makes a difference but remove the () with (EscPressed == false)

by (164 points)

Nope didn't make a difference, but thanks for pointing this out i didnt notice i put that there my self

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.