When trying to play an animation it is saying the animation does not exist (C#)

Godot Version

4.1

Question

I’m trying to make a cutscene system with custom resources but when I try to use my code to run an animation it is sending an error message, which appears to be saying the animation does not exist, but in the animation editor it does show up, with the same name.

Here is a video showing what is going on

And here is the script that runs the cutscene

using Godot;
using System;
using System.Collections;
using System.Threading.Tasks;

public partial class mainChara : Node2D
{
	[Export] public Camera2D Camera;
	[Export] public int CameraLimitLeft = -152;
	[Export] public int CameraLimitTop = -114;
	[Export] public int CameraLimitRight = 152;
	[Export] public int CameraLimitBottom = 114;
	[Export] public PackedScene SpeechBox;
	// Called when the node enters the scene tree for the first time.
	public override void _Ready()
	{
		Camera.LimitLeft = CameraLimitLeft;
		Camera.LimitTop = CameraLimitTop;
		Camera.LimitRight = CameraLimitRight;
		Camera.LimitBottom = CameraLimitBottom;
	}

	// Called every frame. 'delta' is the elapsed time since the previous frame.
	public override void _Process(double delta)
	{
		
	}

	public async void RunCutscene(CutsceneResource Resource)
	{
		for(int i = 0; i < Resource.Actions.Length; i++)
		{
			if(Resource.Actions[i] is DialogueResource)
			{
				DialogueResource Dialoge = Resource.Actions[i] as DialogueResource;
				await TextBox(Dialoge);
			}
			else if(Resource.Actions[i] is CutsceneAnimation)
			{
				CutsceneAnimation Animation = Resource.Actions[i] as CutsceneAnimation;
				PlayAnimation(Animation);
			}
			else
			{
				GD.PrintErr("Error " + Resource.CutsceneName + " at index " + i + " is of type " + Resource.Actions[i].GetType() + " which is invalid.");
			}
		}
		return;
	}
	public async Task TextBox(DialogueResource action)
	{
        Node2D Object = (Node2D)SpeechBox.Instantiate();
	    Object.Set("WhatToSay", action.dialogue);
        Object.Set("FaceSprite", action.FaceSprite);
	    Object.Set("FaceSprite2", action.FaceSprite2);
    	Object.Set("TextSound", action.TextSound);
        Object.Set("Font", action.Font);
	    Object.Name = "SpeechBox";
        AddChild(Object);
    	while (GetNodeOrNull("SpeechBox") != null)
    	{
        	await ToSignal(GetTree().CreateTimer(0.001), "timeout");
        }
		return;
    }

	public IEnumerator PlayAnimation(CutsceneAnimation action)
	{
		action.AnimPlayer.Play(action.AnimationName);
		return null;
	}
    
}

Are you sure the AnimationPlayer you target is the right one ?
With your script i cannot tell what contains your action parameters line 69.
Try printing action.AnimPlayer just before the play call.
The name of your animation is right so problems is somewhere else.

1 Like

I have done that, and it is returning the correct animation player, it just won’t play the animation

what’s inside CutsceneResource resource code/tres
especially the Actions array variable

1 Like

It’s essentially just an array of other resources

using Godot;
using System;
using System.Collections.Generic;

[Tool]
public partial class CutsceneResource : Resource
{
    [Export] public string CutsceneName; //Used so the debug code works properly
    [Export] public Resource[] Actions = new Resource[0];
}

The issue is probably arising from the CutsceneAnimation resource

using Godot;
using System;
using System.Collections.Generic;

[Tool]
public partial class CutsceneAnimation : Resource
{
    [Export] public AnimationPlayer AnimPlayer;
    [Export] public string AnimationName;
}

looks nothing wrong with it, did you check if the AnimationName value is actually correct and added?

this said animation not found for walkleft,
do the AnimationPlayer has animation called walkleft?

also this error showed first

Can you send us the .tscn file containing the scene where this occurs ? With it we can check if the name is correctly spelled and if your resource content match the AnimationPlayer.

It doesn’t let me upload .tscn files, but I was planning on settitng up a github repo of the project, I will do that today if I can, but if I can’t do it today it won’t be until at least sunday that I can

You can use this :

Okay I’ve already set up the git repo and pushed to github so here is the link

Ok so it’s missing the project.godot file but figured out how to launch it.
I have used godot 4.2.1 for it just to quickly know and i cannot compile the game because of this error :

GD0107: Types not derived from Node should not export Node fields C:\project_path\UndertaleFangameFramework\Scripts\Custom Resoruces\CutsceneAnimation.cs(8,37)

So maybe in Godot 4.1 you don’t get error but the problem still here, You cannot set a node type object in a resource as resource are not node themself.

EDIT :
Ok so indeed, it’s the fact that your resource contains a Node. I have find a workaround :
In your CutsceneAnimation resource I have changed the line with the node by this one (line 8) :
[Export] public string AnimPlayerPath;
And in mainChara.cs where you start the animation change the line by this one (line 71) :
GetNode<AnimationPlayer>(action.AnimPlayerPath).Play(action.AnimationName);
After that the only thing you need to change is in sans area2d resources you defined to the Anim Player Path to /root/TestRoom/Toriel/Sprite2D/AnimationPlayer
And everything work as expected.

1 Like

Thanks! Also thank you for pointing out I forgot the project.godot file in the initial commit, I’ll need to add that!

1 Like