Testing animation.duration causes uncaught error

When trying to check the duration of an animation which hasn’t been played yet, an error occurs which is awkward to trap.

As the code tries to use animation.data.currAnim, and this is null, you get an error like this:

playcanvas-stable.min.js:825 Uncaught TypeError: Cannot read property 'duration' of undefined
    at c.get (playcanvas-stable.min.js:825)
    at eval (eval at AnimationMonitor.update (animationMonitor.js:58), <anonymous>:1:15)
    at f.AnimationMonitor.update (animationMonitor.js:58)
    at c._scriptMethod (playcanvas-stable.min.js:887)
    at c._onUpdate (playcanvas-stable.min.js:889)
    at c._callComponentMethod (playcanvas-stable.min.js:897)
    at c._onUpdate (playcanvas-stable.min.js:898)
    at Function.fire (playcanvas-stable.min.js:21)
    at Function.update (playcanvas-stable.min.js:815)
    at e.update (playcanvas-stable.min.js:797)

I have got around this by testing for currAnim specifically before trying to reference animation.duration, but it wasn’t obvious why it was blowing up.

I’m pretty sure this only just started happening, as my code (which comes from examples on this forum about firing an event after an animation finishes) worked without this error a few days ago.

ideally, I guess I’d expect a duration of 0 to be returned.

Are you able to link to a repro?

Hey, Will… I’ve added you to our project:

https://playcanvas.com/project/409610/overview/heartofvegas_stg_100

… and commented out the lines in animationMonitor that prevent the error (55-59). Let me know when you get chance to take a look, so I may put them back in.

Thanks :slight_smile:

Ah, so this is happening because the animation component on the entity does not have Activate checked. So pc.AnimationComponent#play never gets called, which means the internal currAnim property never gets set, which in turn causes the query of the duration property to throw an exception.

I’m not quite sure what the fix is. Maybe just always check Activate but set speed to 0 or 1 depends on whether you want to play the animation initially at start? Open to suggestions…

That’s right, I have deliberately not checked Activate - I want the component to be programmatically controlled entirely.

From my perspective, if I’ve attached the animation component, it would seem valid to query duration without getting an uncaught error… as it’s a getter, it should be easy to protect against that.

Returning 0 would make sense to me (perhaps not to everyone!)

Alternatively, undefined would be fine, too, as I could test for it and it would be meaningfully different to implying a zero-length clip.

IIRC, the Unity approach was that the first animation in the array was automatically set as current, even if not activated. This too would be fine :slight_smile:

Yeah, selecting the first as the current, regardless of whether it’s playing, would seem acceptable. But also, if there’s an animation component but no animation assets set, and you query duration, I guess then it should return 0.

1 Like