DEV Community

Cover image for Let’s Learn Godot 4 by Making an RPG — Part 3: Player Animations🤠

Let’s Learn Godot 4 by Making an RPG — Part 3: Player Animations🤠

christine on June 21, 2023

Our player has been set up, and it moves around the map when we run the game. There is one problem though, and that is that it moves around statica...
Collapse
 
jknech profile image
jknech

Thank you for this great tutorial Christine - it’s very well organized and detailed… very much appreciated by this beginner!

I am having an issue (perhaps) after completing lesson 4 (animating the player). Everything runs fine, but I do get a message in the debugger anytime two direction keys are held or pressed at the same time (so moving diagonally) - it repeats the error message that in the player_animations() function there is no animation named walk (it references line 49 from your finished code). As I said, it doesn’t appear to impact functionality (although perhaps it may be and I’m not yet aware) but I wonder if I’ve missed something and what I might do to troubleshoot and eliminate the error.

Thank you
J

Collapse
 
christinec_dev profile image
christine • Edited

Hey there! Yes, don't worry about this issue as it doens't break the game - but if it bothers you I do have a fix for you :)

In your player_animations() function, you create an animation name like this:

animation = "walk_" + returned_direction(new_direction)
Enter fullscreen mode Exit fullscreen mode

The returned_direction(new_direction) function can return "up", "down", "side", or "" - but never "walk". It seems the latter case "walk_" is causing the error, because there's no animation direction defined with that name. This causes the returned_direction(new_direction) function to return an empty string, which causes "walk_" to be passed into the play function. This happens when the direction is not perfectly up, down, left, or right (when you're pressing two buttons together).

To fix this, we can set replace our "" with a direction instead in our returned_direction() function. Do this:

#returns the animation direction
func returned_direction(direction : Vector2):
    var normalized_direction  = direction.normalized()

    if normalized_direction.y >= 1:
        return "down"
    elif normalized_direction.y <= -1:
        return "up"
    elif normalized_direction.x >= 1:
        $AnimatedSprite2D.flip_h = false
        return "side"
    elif normalized_direction.x <= -1:
        $AnimatedSprite2D.flip_h = true
        return "side"

    #default value is a direction from above
    return "side"
Enter fullscreen mode Exit fullscreen mode

Now when you press two buttons together, your side animation should always play and the walk error should be fixed. If you don't want the side animation to always play, you could always return "walk".

#returns the animation direction
func returned_direction(direction : Vector2):
    var normalized_direction  = direction.normalized()

    if normalized_direction.y >= 1:
        return "down"
    elif normalized_direction.y <= -1:
        return "up"
    elif normalized_direction.x >= 1:
        $AnimatedSprite2D.flip_h = false
        return "side"
    elif normalized_direction.x <= -1:
        $AnimatedSprite2D.flip_h = true
        return "side"

    #default value
    return "walk"
Enter fullscreen mode Exit fullscreen mode

Which will loop your walk animation if you press two inputs. To stop it from looping even after you've released your inputs, you can add this code to your physics_process() function:

func _physics_process(delta):
       #older code  
       if !Input.is_anything_pressed():
        animation  = "idle_" + returned_direction(new_direction)    
Enter fullscreen mode Exit fullscreen mode

Using any of the two fixes above should resolve your error. Give it a try, and let me know if it works. Happy coding! 😊

Collapse
 
jknech profile image
jknech

Thanks for the quick reply Christine - very much appreciated!

I tried the first of the two solutions you recommended and it seems to work like a charm - the debugger has stopped spitting out errors :)

I plan on continuing through the tutorial (planning on paying for the PDF guide as well). Looking forward to the journey and any future content/support you provide!

Best
J

Thread Thread
 
christinec_dev profile image
christine

I'm glad it worked for you. Have a happy week of coding ahead! 😁

Thread Thread
 
sirneij profile image
John Owolabi Idogun

Welcome to the community ☺️. Hope you are having a great time.

Thread Thread
 
christinec_dev profile image
christine

Thanks for welcoming people John. 😅

 
sirneij profile image
John Owolabi Idogun

Welcome to the community. Hope you get to enjoy being here.

Collapse
 
fauxpas8008 profile image
Paul

I followed ( I think!) the guide and when I added the default "side" option at the end of func returned_direction I noticed that if I moved left, then pressed up or down + right, the flip_h value was still set to left causing me to moonwalk.
I did the below and no more moonwalking

func returned_direction(direction : Vector2):
    var normalized_direction = direction.normalized()

    if normalized_direction.y >= 1:
        return "down"
    elif normalized_direction.y <= -1:
        return "up"
    elif normalized_direction.x >= 1:
        #right
        $AnimatedSprite2D.flip_h = false
        return "side"
    elif normalized_direction.x <= -1:
        #left
        $AnimatedSprite2D.flip_h = true
        return "side"

    else:
        if Input.is_action_pressed("ui_left"):
            $AnimatedSprite2D.flip_h = true 
        elif Input.is_action_pressed("ui_right"):
            $AnimatedSprite2D.flip_h = false    
        return diagonal_default_player_direction
Enter fullscreen mode Exit fullscreen mode
Collapse
 
fauxpas8008 profile image
Paul

Thank you for this, really helpful and detailed.
I love that this isn't skin deep.

Correction:
attack_up animation has 6 frames not 8

Collapse
 
christinec_dev profile image
christine

Thanks for all of your suggestions Paul! I'll amend it. 😊

Collapse
 
alex2025 profile image
Alex

Hi Christine! Thanks for the tutorial...I learn by building complete projects vs small tutorials, so I was excited when I came across this series.

I ran into an error when adding "func _input(event)". It said that new_direction was not defined...and it certainly is not defined in the scope of this function. I took a look at your script and saw that you had it defined at the top of the script, but there is no mention of that in the tutorial itself. It may confuse those who are just following along and may not know much about programming yet. Just an FYI!

Collapse
 
tavernsagas profile image
James

Aka me. lmao im yelling IT IS DEFINED IM LOOKING RIGHT AT IT! xD

Collapse
 
cosmicgraveyard215 profile image
Tanner West

Thank you for this! You resolved my issue!

Collapse
 
mat_venable_564e3ce8c59c0 profile image
Mat Venable • Edited

It appears the creation of these variables from the final code are missing from the steps above:

#direction and animation to be updated throughout game state
var new_direction = Vector2(0,1) #only move one spaces
var animation
Enter fullscreen mode Exit fullscreen mode
Collapse
 
vincenzo_pepperpony_1f7e profile image
Vincenzo Pepper-Pony

Insanely confusing. If you're going to post screenshots with minor changes in code, then at the end you should have a single screenshot with the final product. Otherwise junior game devs are going to be playing "where's waldo" looking for flaws in their code BEFORE they have any real concrete understanding of how to find those flaws, or what they even mean.