Storing the AnimationBuilder when created from KeysPath

It seems animate.createAnimationBuilderFromKeysPath is the only createAnimationBuilder that does not return an AnimationBuilder object. I would like to store the builder somewhere so I can instantiate it multiple times, but I can’t seem to get that sucker out of the function. Here’s the best approach I can figure looking at sample code:

var _this=this;
jibo.animate.createAnimationBuilderFromKeysPath(animPath, this.basePath, function(builder) {
  builder.setDOFs(jibo.animate.dofs.ALL);
  builder.on(jibo.animate.AnimationEventType.STOPPED, function() {
  });
  _this.builder=builder; //_this.builder contains the builder object here
})
// this.builder and _this.builder are now undefined

The problem is, once I come out of createAnimationBuilderFromKeysPath the variable is undefined.

Is there a way to do this properly so I don’t have to rebuild the AnimationBuilder from the same keys file every time I want to play it back?

Ok, I managed to answer my own question, but I’ll post what I did here in case it helps someone else out.

I wrote a function for my class called “registerAnimationBuilder”, then called this from in the builder callback. So the code looks something like this (where _this has been defined out of the scope to point to refer to ‘this’, and ‘reaction’ and ‘keysFile’ were defined in the function before the call to createAnimationBuilderFromKeys).

    jibo.animate.createAnimationBuilderFromKeysPath(animPath, _this.basePath, function(builder) {
      builder.setDOFs(jibo.animate.dofs.ALL);
      _this.registerAnimationBuilder(reaction, keysFile, builder);
    });

In my class I define

  registerAnimationBuilder(reaction, keysFile, builder) {
    let builder_tag='builder_'+reaction+'_'+keysFile;
    this[builder_tag]=builder;
  }

I actually send some other variables that are in scope to the registration function as well. The registration function, which does know about ‘this’, attaches the builder to the object so it can be called later.

I’m still getting the trick of this asynchronous programming.

1 Like

Yes that’s a fine way to do it!

Something to keep in mind is that “registerAnimationBuilder” will not be called during “createAnimationBuilderFromKeysPath”, but rather after a short delay for loading the file; so even with this setup, you must be careful not to rely on “this[builder_tag]” being set immediately after “createAnimationBuilderFromKeysPath”, but rather only after “registerAnimationBuilder” has been called!

1 Like