is the code broken? if so how would I fix it?

Godot Version

4.5

Question

`I am trying to figure out why this code isn’t working. I recently found a tutorial about state machines from two years ago. The idle is working, but for some odd reason, it won’t go back to idle! Im trying to learn everything I can about Godot for future games! ’

parts of the codes listed below:
if direction == Vector2.UP:
animated_sprite_2d.play(“Idle_Back”)
elif direction == Vector2.RIGHT:
animated_sprite_2d.play(“Idle_Right”)
elif direction == Vector2.DOWN:
animated_sprite_2d.play(“Idle_Front”)
elif direction == Vector2.LEFT:
animated_sprite_2d.play(“Idle_Left”)
func _on_next_transitions() → void:
GameInputEvents.movement_input()
if GameInputEvents.is_movment_input():
transition.emit(“Walk”)
__

func _on_physics_process(_delta : float) → void:
var direction: Vector2 = GameInputEvents.movement_input()
if direction == Vector2.UP:
animated_sprite_2d.play(“Up walk”)
elif direction == Vector2.RIGHT:
animated_sprite_2d.play(“Right Walk”)
elif direction == Vector2.DOWN:
animated_sprite_2d.play(“Down walk”)
elif direction == Vector2.LEFT:
animated_sprite_2d.play(“Left walk”)
player.velocity = direction * speed
player. move_and_slide()
func _on_next_transitions() → void:
if !GameInputEvents.is_movment_input():
transition.emit(“Idle”)

From the code you posted, it’s not really possible to tell what the issue is.

  1. What function receives the transition signals and are they properly connected?
    (Especially from the walk state object to that function, since this seems to be the transition not working.)
  2. What value does direction receive in the idle state script?
  3. What does the GameInputEvent’s functions do exactly?

One additional note:
In general, expecting a Vector2 to be exactly something like Vector2.UP is a bad idea. I’m assuming GameInputEvents.movement_input() is specifically returning one of those constant Vectors, and in that case it should usually work, but it would still be better to use an enum or at least Vector2i for this. (Or change the if-statement to always play the animation of the closest direction, even if the value doesn’t exactly match.)

1 Like

Thanks for the help! Im trying to make a simple top-down walk cycle! Are there other ways I can do this? im using an animated sprite 2d for this as it’s one of the better ways to animate!

This does help, by the way! Thanks for troubleshooting!

1 Like

Using an AnimatedSprite2D is fine, but for differentiating idle and walk animations there are probably easier ways than a state machine. You can likely combine the two states, and Instead of changing between them you just change which animation is used.

State machines can be useful if the states behave very differently, but for idle/walk I doubt that will be the case.

1 Like

You might want to look into state machines and blend spaces in the AnimationTree. I found them super helpful for creating animations that respond to Vector2 changes and for handling state changes without having to manually code them all.

1 Like