[SOLVED] Can't get reference to entity (set in Attribute) in initialize function

I was trying to extend this example (PlayCanvas Examples) but in the Play Canvas Editor instead of in one massive JS file.

I created a template for the “player” and I am able to create new instances of the player template, however I can’t seem to be able to pass references to the canvas/screen/font for the complete instantiation of the UI components… so you can see the 3d models get drawn on the world, but the UI parts throw an exception when I try to add them to the screen.

I seem to have “solved” the problem by storing references globally to the screen and camera entities… not ideal, but it works!

References to entities outside of the template are not kept/stored in the template asset

You can get around this by assigning the attributes before adding the instantiated entity to the scene.

eg:

    var asset = this.app.assets.find('Golem', 'template');
    /** @type {pc.Template} */
    var template = asset.resource;
    var entity = template.instantiate();
    entity.script.a.foobar = this.entity;
    entity.reparent(this.entity);

Where script a is

var A = pc.createScript('a');
A.attributes.add('foobar', { type: 'entity'});

// initialize code called once per entity
A.prototype.initialize = function() {
    console.log(this.foobar.name);
};