Godot Version
4.2.1
Question
I’m relatively new to Godot, but quite experienced in C# and have been giving my first game a go. I’ve created a Node2D for handling character animations, which creates several child AnimatedSprite2D (extended by a small script) as well as an AnimationHandler based on a config file for the character. This is working well, but I’m seeing some errors when trying to use callback functions with the animation handler.
I’ve given the AnimationHandler an animation called “Turn”, which scales the canvas when the player changes direction, and calls a method at the middle point to update the sprites, like so
turnPlayer = new AnimationPlayer() {
SpeedScale = 1,
RootNode = GetPath(),
Name = "TurnPlayer"
};
AnimationLibrary aniLib = new AnimationLibrary();
turnAnimation = new Animation() {
Length = 0.2f,
LoopMode = Animation.LoopModeEnum.None
};
int scaleTrack = turnAnimation.AddTrack(Animation.TrackType.Value);
turnAnimation.TrackSetPath(scaleTrack, ".:scale");
turnAnimation.TrackInsertKey(scaleTrack, 0, Vector2.One);
turnAnimation.TrackInsertKey(scaleTrack, 0.1, new Vector2(0,1));
turnAnimation.TrackInsertKey(scaleTrack, 0.2, Vector2.One);
int functionTrack = turnAnimation.AddTrack(Animation.TrackType.Method);
turnAnimation.TrackSetPath(functionTrack, ".");
turnAnimation.TrackInsertKey(functionTrack, 0.1, new Godot.Collections.Dictionary<string, Variant>() { { "method", "ChangeSpriteDirection" }, { "args", new Godot.Collections.Array<Variant>() { } } });
aniLib.AddAnimation("Turn", turnAnimation);
turnPlayer.AddAnimationLibrary("", aniLib);
AddChild(turnPlayer);
The function to play this animation saves a direction to a property, and the callback function simply sets the animations for each Sprite to face that direction
private void ChangeSpriteDirection() {
foreach (ActionAnimatedSprite sprite in sprites.Values) {
sprite.SetDirection(queuedDirectionChange);
}
}
ActionAnimatedSprite then follows some table lookups to pick the animation, and plays it
public void SetDirection(CompositonNode.Direction direction) {
if (!canRotate) { return; }
//Update our lookup table with the new direction state
((DirectionalAnimationTable)actionTable).SetDirection(direction);
PlayAnimationFromAction(currentAction);
}
private void PlayAnimationFromAction(Action action) {
//Look up the animation from the table, based on the current action
string animation;
float speed;
if (action == null) {
animation = actionTable.GetAnimation(actionTable.defaultAction);
speed = 1;
} else {
animation = actionTable.GetAnimation(action.name);
speed = action.animationSpeedModifier;
}
if (animation == null || animation == "") {
animation = "default";
}
//Play the animation
Play(new StringName(animation));
}
This is, for the most part is working fine. The signals all trigger and the animation plays correctly. However, whenever it’s triggered I get these errors in my debugger. The stack trace shows that the errors are originating from the Play() call in PlayAnimationFromAction(). These errors each show up 11 times, each time my character changes direction, which aligns with my test character having 11 AnimatedSprite components.
While this isn’t impeding the program, it is flooding my error long which I’d like to keep free for actual errors. I can’t find a way to trap these errors since they originate from C++ and don’t seem to come back to the C#, nor is there any noticeable side effect I can look for.
Interestingly, if I assign AnimatedSprite2D.Animation a value just before the call to Play(), the second error doesn’t appear. If I call SetDirection() from anywhere besides the animation callback, it produces no errors at all.
So, the question is: Does anyone know of a way to A) modify my design to avoid this error or B) trap and hide the error?