Hello, this is my first time on this website, I have been using godot engine to learn more on it's tools and making games in general but when I tried making a mock project to practice with sprite movement and sprite animation, I've hit a roadblock and unfortunately took a while to consider asking for help.
My problem is about being able to change the animation depending on whether the key is pressed or not. I've learned how to move the sprite and activate the animation but I'm trying to give logic to it so depending where it's facing and if the movement key is not pressed, it will go in an idle animation instead of staying in it's walking animation.
My question is,
How do I change the animation through user input on GDScript so that when I stop pressing the key, it defaults back to it's idle animation? I've tried searching the same questions to this problem with the answers not giving any fruitful results. I've tried with looped animations and it kept looping the animation forever without changing the animation while the non-looped animation couldn't be looped through GDScript to my best of my little knowledge in general. What should I add or change in my code to make it possible? Maybe my arrangement of nodes in my scene is wrong? I appreciate any answers that could nudge me in the right direction.
I'll attach my current work scene and my current code below.
My current work scene setup
const playerspeed = 150
var anim = ""
var new_anim = anim
var userpos = get_pos()
userpos.y += -playerspeed*delta
new_anim = "go_up"
userpos.y += playerspeed*delta
new_anim = "playermovement"
userpos.x += -playerspeed*delta-2
new_anim = "go_left"
userpos.x += playerspeed*delta+2
new_anim = "go_right"