|
|
|
 |
Attention |
Topic was automatically imported from the old Question2Answer platform. |
 |
Asked By |
Kleins |
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
**
|
|
|
 |
Reply From: |
Dragon20C |
I am not sure if it makes a difference but remove the () with (EscPressed == false)
Nope didn’t make a difference, but thanks for pointing this out i didnt notice i put that there my self
Kleins | 2021-06-01 12:26
|
|
|
 |
Reply From: |
PunchablePlushie |
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
Wow, thanks! This works just as expected. Thanks again.
Kleins | 2021-06-01 12:27
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
Kleins | 2021-06-01 12:44