// app.js
require('./orbitCamera');
var camera = new pc.Entity();
camera.addComponent('camera');
camera.addComponent('script');
camera.script.create('orbitCamera');
However, I got undefined in the console… Does the script attributes only work in the editor?
It seems that I can only define this.speed = 1 in initialize phase to make the code work.
Attributes are initialized after entity has been initialized (enabled and in hierarchy).
So if you add the camera entity to hierarchy, then it should define all attributes and they will be accessible.
I just test it at runtime, I create camera and add it to app.root, the script attributes is still undefined.
// app.js
require('./orbitCamera');
var camera = new pc.Entity();
camera.addComponent('camera');
camera.addComponent('script');
camera.script.create('orbitCamera');
app.root.addChild(camera);
console.log(camera.script.orbitCamera.speed) // undefined
I’ve made some tests, and here are the observations:
If application is not started, then attributes won’t be initialized. - this is fine
If entity is not within hierarchy, then attributes won’t be initialized. - this is fine
If script is added to entity before it is added to hierarchy, then attributes will be initialized once entity is added to hierarchy and is enabled. - this is fine
If script is added to entity which is already within running app hierarchy and is enabled, then attributes won’t be initialized. - this is not fine
Attributes do get initialized once entity is about to become active within application. But scenario 4 shows some issue here.
Reason why attributes are not initialized instantly is due to some complexities in data flow. We could look into trying to initialize attributes at the moment of script being added to entity, this would solve most of problems, but will introduce some other complexity there.
It is called only once per entity, right before first time it is about to become active.
So disabled entities do not call initialize until they get enabled within hierarchy.
And initialize works as it should (just tested the codepen example), it is only attributes do not get initialized.