0 votes

Hi, I'm fairly new to Godot, and I've been working on a game with a friend. She's insistent on using sprites with unique animations, and I'm fine with it, but I'm fairly sure my current code isn't the best way to do it. I've been looking at anim trees, or maybe doing a switch case, but would like some feedback on what I already have.

 func get_new_animation(is_shooting = false):
    var animation_new = ""
    if Input.is_action_pressed("game_right"):
        direction = Vector2.RIGHT
    elif Input.is_action_pressed("game_left"):
        direction = Vector2.LEFT
    if is_on_floor():
        if not is_shooting:
            if _velocity.x > 0.1:
                animation_new = "Right Run"
            elif _velocity.x < 0:
                animation_new = "Left Run"
            elif _velocity.x == 0:
                if direction == Vector2.LEFT:
                    animation_new = "Idle Left"
                else:
                    animation_new = "Idle Right"
        else:
            if _velocity.x > 0.1:
                animation_new = "Right Run Shoot"
            elif _velocity.x < 0:
                animation_new = "Left Run Shoot"
            elif _velocity.x == 0:
                if direction == Vector2.LEFT:
                    animation_new = "Left Shoot"
                else:
                    animation_new = "Right Shoot"
    else:
        if not is_shooting:
            if _velocity.y > 0:
                if direction == Vector2.LEFT:
                    animation_new = "Left Fall"
                else:
                    animation_new = "Right Fall"
            else:
                if direction == Vector2.LEFT:
                    animation_new = "Left Jump"
                else:
                    animation_new = "Right Jump"
        else:
            if direction == Vector2.LEFT:
                animation_new +=  "Left Jump Shoot"
            else:
                animation_new +=  "Right Jump Shoot"
    return animation_new

The function just checks which animations should play, and determines which side is facing by a Vector based on last input.
Thanks in advance!

Godot version 3.3.4
in Engine by (12 points)

1 Answer

0 votes

I find it OK :)
Of course if You want to spare this many lines of code You should use animation tree, but I don't think it will help in performance so much.

I will give You a hint on what I do to reduce code for such cases. I name my animations meaningfully like "WalkShootLeft1" "JumpIdleIdle1" and construct animation name from components :

if abs(velocity.x) > 5  :
 anim = "Run" ( when slower its Walk, when vel.y is > 0 it is Jump )
if is pressed shoot :
  anim += "Shoot" , else anim += "Idle"
if velocity.x > 0 :
  anim += "Right", elif < 0 "Left" else "Idle"
return anim
by (5,607 points)
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 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 webmaster@godotengine.org with your username.