Godot Version
v4.4
Question
I have a small 2D RPG project I’m working on just trying to learn the engine/gscript. I’ve never touched code until about 3 weeks ago, I’ve just messed around in UE5 making project that never functioned. So I’ve been watching tons of tutorials and using that info to test stuff on my own without the mindless copy/paste.
I have run into an issue with an AnimatedSprite2D that uses an animation player. Up until this point I have only used Sprite2D nodes with an animation player attached, but this certain sprite has a TON of sprite sheets, so I made a SpriteFrame and just used that in the AnimationPlayer. I’m sure that’s the most inefficient way to do it, but it worked. LOL Is it possible to use the same enemy script for both a Sprite2D and an AnimatedSprite2D?
I get the codes:
-
E 0:00:01:0039 enemy.gd:19 @ _ready(): Node not found: “AnimatedSprite2D” (relative to “/root/MoonlightMarsh1/Enemies/Slime”).
<C++ Error> Method/function failed. Returning: nullptr
<C++ Source> scene/main/node.cpp:1792 @ get_node()
enemy.gd:19 @ _ready() -
E 0:00:01:0040 enemy.gd:19 @ _ready(): Node not found: “AnimatedSprite2D” (relative to “/root/MoonlightMarsh1/Enemies/Goblin”).
<C++ Error> Method/function failed. Returning: nullptr
<C++ Source> scene/main/node.cpp:1792 @ get_node()
enemy.gd:19 @ _ready() -
E 0:00:01:0040 enemy.gd:16 @ _ready(): Node not found: “Sprite2D” (relative to “/root/MoonlightMarsh1/Golem”).
<C++ Error> Method/function failed. Returning: nullptr
<C++ Source> scene/main/node.cpp:1792 @ get_node()
enemy.gd:16 @ _ready()
Here is the code for the enemy script I am trying to use:
class_name Enemy extends CharacterBody2D
#REFERENCES
signal direction_changed(new_direction : Vector2)
signal enemy_damaged(hurt_box : HurtBox)
signal enemy_destroyed(hurt_box : HurtBox)
const DIR_4 = [Vector2.RIGHT, Vector2.DOWN, Vector2.LEFT, Vector2.UP]
@export var hp : int = 3
var cardinal_direction : Vector2 = Vector2.DOWN
var direction : Vector2 = Vector2.ZERO
var player : Player
var invulnerable : bool = false
@onready var animation_player: AnimationPlayer = $AnimationPlayer
@onready var sprite_2d: Sprite2D = $Sprite2D
@onready var hit_box: HitBox = $Hitbox
@onready var state_machine : EnemyStateMachine = $EnemyStateMachine
@onready var anim_sprite_2d: AnimatedSprite2D = $AnimatedSprite2D
func _ready():
state_machine.initialize(self)
player = PlayerManager.player
hit_box.Damaged.connect(_take_damage)
pass # Replace with function body.
func _process(_delta: float) -> void:
pass
func _physics_process(_delta):
move_and_slide()
func Set_Direction(_new_direction : Vector2) -> bool:
direction = _new_direction
if direction == Vector2.ZERO:
return false
var direction_id : int = int( round(
(direction + cardinal_direction * 0.1 ).angle()
/ TAU * DIR_4.size()
))
var new_dir = DIR_4[direction_id]
if new_dir == cardinal_direction:
return false
cardinal_direction = new_dir
direction_changed.emit(new_dir)
anim_sprite_2d.scale.x = -1 if cardinal_direction == Vector2.LEFT else 1
#getting an error for scale here if I aggro the anim_sprite_2d mob.
return true
func update_animation( state : String) -> void:
print("update_anim")
animation_player.play(state + "_" + anim_direction() )
#error is here I think, it will print "update_anim" 18 times and I get 18 errors every time....
func anim_direction() -> String:
if cardinal_direction == Vector2.DOWN:
return "down"
elif cardinal_direction == Vector2.UP:
return "up"
else:
return "side"
func _take_damage(hurt_box : HurtBox) -> void:
if invulnerable == true:
return
hp -= hurt_box.damage
if hp > 0:
enemy_damaged.emit(hurt_box)
else:
enemy_destroyed.emit(hurt_box)