Trouble getting animation script to work properly

Hello, I’ve run into a trouble, when trying to play every animation attached to an entity:

Here’s what I’m trying to do:

I have a set of entities with animations attached to them and on a certain event, I want to play all of them, regardless of the names and things like that. Judging from this link:

The code should look somewhat like this:

for (var animIndex = 0; animIndex < this.animations.length; animIndex++) {
    var animComponent = this.animations[animIndex].animation;
    for (var assetIndex = 0; assetIndex < animComponent.assets.length; assetIndex++) {
        var animAsset = animComponent.assets[assetIndex];
        //console.log("Asset: " + animAsset.toString() + "; name: " + animAsset.name);
        animComponent.play(animAsset.name);
    }
}

the problem is, that the animAsset.name is undefined and when I try to print animAsset, it prints only the id (which is correct, by the way) and the expected fields it should have are missing.

I made an assumption, that the documentation might be incorrect and there are only ids and nothing else in animComponent.assets and when trying to get Asset from id using the things from this link: https://developer.playcanvas.com/en/api/pc.AssetRegistry.html#get nothing worked, because ‘app’ is no more a thing in playcanvas.

Can anyone provide some help?

Well… As it turns out, app is now pc.app and the animComponent.assets thing is a list of asset ids. Would anyone bother with updating the documentation?

It’s a little difficult because it depends on what context the code is being ran in. The sample code was meant to show that you should be using an instance of the app (which could be made more clear I guess?). In some cases where you are using the engine without the editor, you may also have a reference to the app somewhere already. (You can see an example here: https://github.com/playcanvas/engine/blob/master/examples/load_model/index.html)

If you are in a pc.script, it would be this.app. There is a ‘global’ instance of at pc.app but that is only assigned on the first tick (I can’t quite remember) of the engine running. pc.app really shouldn’t be used as there may be more than one app running on the page (rare but possible).

Thanks for the information

And how is the solution for this issue? I run into the same right now.

What’s the problem you are running into?

I want to play the first animation (or the second) of an entity. I tried this:

myEntity.animation.play(myEntity.animation.assets[0],1);

But myEntity.animation.assets[0] does return the id and play can’t work with it. So how do I get the name out of this?

Turns out there are properties that you could use instead (not public, but far easier to use): https://github.com/playcanvas/engine/blob/master/src/framework/components/animation/component.js#L117

Not tested, but I imagine it be something like this:

myEntity.animation.play(myEntity.animation.animationsIndex((myEntity.animation.assets[0]),1);
1 Like

Thank you very much!
One small correction cause animationsIndex is an array:

myEntity.animation.play(myEntity.animation.animationsIndex[(myEntity.animation.assets[0]],1);
1 Like

Any updates on this?
I’ve tried:

this.entity.animation.play(this.entity.animation.animationsIndex[this.entity.animation.assets[0]], 1)

But animationIndex[this.entity.animation.assets[0]] is undefined.

Hi @Dan_Organ,

Where are you trying this? Testing on the Animation Blending tutorial it seems t o work as expected and return the asset ids in the animation list:

Yeah, but all animation clips in the Animation Blending tutorial are set in the Editor manually.
I want to attach all animations in my script and play them. Because, you know, it is not very convenient to attach animation clips manually if I have 100+ of them.
I have no problems loading animation assets (console.log(this.entity.animation.assets[0].name) is working), but I can’t play this animation clip.

This is my test project: https://playcanvas.com/editor/scene/1075989

I think you will have to add the animation asset to the animation component before trying to play them.

1 Like

I’ve attached the Animation component to the entity. Empty component without clips.

this.entity.animation.assets[0] = this.anim_asset;

But is not this code adding the animation asset to the component?

Hi @Dan_Organ,

You shouldn’t add the animations directly to the component assets array, but set it to look to a new one:

this.entity.animation.assets = myAnimationAssetsArray;
1 Like

Thanks, Leonidas! It is working this way.
But now I am playing animations as it said in the documentation:

this.entity.animation.play(this.entity.animation.assets[0].name, 1);