Newbie having issues with 2d animation script

Godot version 4.3

I keep having the same issues with the latest part of my script. I am new and have basically zero experience in coding and am currently following a tutorial but even with it I am having issues with my animations. Here is the code I have written so far.

extends CharacterBody2D
@onready var sprite_2d: AnimatedSprite2D = $CharacterBody2D/CollisionShape2D/Sprite2D

const GRAVITY = 1000

enum State {Idle, Run }

var current_state

func _ready():
current_state = State.Idle

func _physics_process(delta):
player_falling(delta)
player_idle(delta)
player_run(delta)

move_and_slide()

player_animations()

func player_falling(delta):
if is_on_floor():
velocity.y += GRAVITY * delta

func player_idle(delta):
if is_on_floor():
current_state = State.Idle

func player_run(delta):
var direction = input.get_axis(“move_left”, “move_right”)

if direction:
	velocity.x = direction + 300
else:
	velocity.x = move_toward(velocity.x, 0, 300)

func player_animations():
if current_state == State.Idle:
AnimatedSprite2D.play(“idle”)
elif current_state == State.Run:
AnimatedSprite2D.play(“run”)

The main issue is this one right here /\ Func player_animations.

I keep getting errors that line 40, 45,46,47 are in some way not indented properly and then it creates a bigger mess everytime I try and fix it. Any idea on how to resolve it?

There a couple of issue which I see:
@onready var sprite_2d: AnimatedSprite2D = $CharacterBody2D/CollisionShape2D/Sprite2D - your node here is named Sprite2D, double check if its just a sprite or an AnimatedSprite2D, one is static while the other can store animation frames, make sure the node is actually AnimatedSprite2D

Here:
AnimatedSprite2D.play(“idle”) - you are trying to call play on basically a node type, instead of this use: sprite_2d.play("idle) - and make sure you have an animation named “idle” the same goes for “run”

Everything else looks +/- ok. Also edit the code section in the post as it has different fonts, makes it hard to read

Heya sorry for the super late reply but been trying to get it to work and I still havent gotten it. I rewrote TWICE to the dot from the tutorial. I don’t understand how I am struggling this much ;-;

I even tried using: sprite_2d.play(“idle”) - and it still keeps talking about indenting is incorrect or its unindent isnt matching previous indent level which it is.

SOS needed for a true noob (;–;)/

For indentation errors make sure to paste code with proper formatting.


Generally you will need to add indentation for code you want to be part of the function or if statement.

# not part of function

# function start
func _ready() -> void:
    # part of _ready()
    print("hi from ready!")

    # part of _ready and starting a if block
    if false:
        # part of the if block, shouldn't print
        print("nobody will see this")

    # no longer part of if block
    print("hello again from ready, where is that last print?")

# no longer part of the function, script ends too.

This one is GDS problem is lack of braces, which I personaly do not like as such errors will arise as yours, you will need to check if there is no addtional space or tab in code, as it may have incorrect format.