Trouble getting animation script to work properly


#1

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: https://developer.playcanvas.com/en/api/pc.AnimationComponent.html#assets 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?


#2

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?


#3

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).


#4

Thanks for the information


#5

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


#6

What’s the problem you are running into?


#7

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?


#8

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);

#9

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

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