So I’ve been following Michael Games rpg making tutorial on youtube to practice for my own games. I have followed it to the letter and rewound and reviewed all my script and I cannot find a single difference. The problem is I keep getting an error when I run my game that says it cannot find “walk_AnimDirection()” or cannot find “idle_AnimDirection()”
In the script we made a function so that AnimDirection() was supposed to be replaced by a direction which I put in the animations name. The script works perfectly for him but for me it does nothing in game and gives me those errors when I move…
Here is the whole player script… I am tired and taking a break. Hopefully someone is able to see the issue…
class_name player extends CharacterBody2D
var cardinal_direction : Vector2 = Vector2.DOWN
var direction : Vector2 = Vector2.ZERO
var move_speed : float = 100.0
var state : String = "idle"
@onready var animation_player : AnimationPlayer = $AnimationPlayer
@onready var sprite_2d : Sprite2D = $Sprite2D
# Called when the node enters the scene tree for the first time.
func _ready():
pass # Replace with function body.
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(_delta):
direction.x = Input.get_action_strength("right") - Input.get_action_strength("left")
direction.y = Input.get_action_strength("down") - Input.get_action_strength("up")
velocity = direction * move_speed
if SetState() == true || SetDirection() == true:
UpdateAnimation()
pass
func _physics_process(_delta):
move_and_slide()
func SetDirection() -> bool:
var new_dir : Vector2 = cardinal_direction
if direction == Vector2.ZERO:
return false
if direction.y == 0:
new_dir = Vector2.LEFT if direction.x < 0 else Vector2.RIGHT
elif direction.x == 0:
new_dir = Vector2.UP if direction.y < 0 else Vector2.DOWN
if new_dir == cardinal_direction:
return false
cardinal_direction = new_dir
sprite_2d.scale.x = -1 if cardinal_direction == Vector2.LEFT else 1
return true
func SetState() -> bool:
var new_state : String = "idle" if direction == Vector2.ZERO else "walk"
if new_state == state:
return false
state = new_state
return true
func UpdateAnimation() -> void:
if state == "idle":
animation_player.play( "idle_" + AnimDirection() )
else:
animation_player.play( state + "_" + AnimDirection() )
pass
func AnimDirection() -> String:
if cardinal_direction == Vector2.UP:
return "up"
elif cardinal_direction == Vector2.LEFT or cardinal_direction == Vector2.RIGHT:
return "side"
else:
return "down"
sorry i was wondering how to do that. I was annoyed with how it pasted. Thanks for sharing. Also I had already figured out to remove the qoutes… But now my problem is this.
The animations when walking each direction play correctly. The animation when facing up plays correctly. But when facing left, right or down. It cant decide which idle animation to play and just cycles through every direction of idle animations… Here is the new script hopefully correctly formatted this time…
class_name player extends CharacterBody2D
var cardinal_direction : Vector2 = Vector2.DOWN
var direction : Vector2 = Vector2.ZERO
var move_speed : float = 100.0
var state : String = "idle"
@onready var animation_player : AnimationPlayer = $AnimationPlayer
@onready var sprite_2d : Sprite2D = $Sprite2D
# Called when the node enters the scene tree for the first time.
func _ready():
pass # Replace with function body.
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(_delta):
direction.x = Input.get_action_strength("right") - Input.get_action_strength("left")
direction.y = Input.get_action_strength("down") - Input.get_action_strength("up")
velocity = direction * move_speed
if SetState() == true || SetDirection() == true:
UpdateAnimation()
pass
func _physics_process(_delta):
move_and_slide()
func SetDirection() -> bool:
var new_dir : Vector2 = cardinal_direction
if direction == Vector2.ZERO:
return false
if direction.y == 0:
new_dir = Vector2.LEFT if direction.x < 0 else Vector2.RIGHT
elif direction.x == 0:
new_dir = Vector2.UP if direction.y < 0 else Vector2.DOWN
if new_dir == cardinal_direction:
return false
cardinal_direction = new_dir
sprite_2d.scale.x = -1 if cardinal_direction == Vector2.LEFT else 1
return true
func SetState() -> bool:
var new_state : String = "idle" if direction == Vector2.ZERO else "walk"
if new_state == state:
return false
state = new_state
return true
func UpdateAnimation() -> void:
if state == "idle":
animation_player.play( "idle_" + AnimDirection() )
else:
animation_player.play( state + "_" + AnimDirection() )
pass
func AnimDirection() -> String:
if cardinal_direction == Vector2.UP:
return "up"
elif cardinal_direction == Vector2.LEFT or cardinal_direction == Vector2.RIGHT:
return "side"
else:
return "down"
When im walking north, south, east and west the correct animations play for walking. When I am idle facing north, the correct idle animation plays when I stop. But when I stop facing east, west, or south, that’s when it just cycles through EVERY idle animation for every direction and cant decide on the correct one…
So, I’ve been trying things with the Animation player to help figure this out. I’ve noticed now, that when I turn my idle_side animation off loop, the animations in game cycle through and then stop at the end of the side animation. So that confirms that for some reason it is looping very fast through every idle animation repeatedly. Now how to make it just loop through the 1 correct animation, I’m not sure… I thought that was covered in the script, from what I can tell with my newbie eyes it should be working… I’ve been stuck on this for 3 days now and I can’t progress further in my learning until I get this figured out.
OK I have FINALLY figured it out… All I had to do was make the property discrete and voila… Now they all work as intended… Ugh, so simple, yet so difficult to find… Case closed… For now…