I need to create a buttons layout.
since there are many of them and the layout&button props are received from a server through a JSON,
I want to Dynamically create them and assign their properties according to the info in the JSON.
the though i had is (like i was used to in NodeJS and such) to create their script with a constructor that initialize their properties.
but when i tried to expend the pc.createScript('SomeScript'); function to define these properties i got errors
In which case, I would do in the initialize function. Or if you are creating the entities dynamically, you can set the values after you add the scripts to the entities.
there was no adding property.
Scroll Anchor, Cancel Button,Error Message Panel are all entities chieldren to the entity with the script.
later on the script they are just used normally (_scrollAnchorEntity .function).
question is why did he use this._scrollAnchorEntity = this.entity.findByName('Scroll Anchor'); and not var _scrollAnchorEntity = this.entity.findByName('Scroll Anchor');
Using var only creates a variable in the current function scope. As you want to access it in other functions of the class, you have to add it to the this object. It’s effectively the same as creating a public member variable in C#.
Javascript is a dynamic language and allows you to add attributes (both functions and variables as functions are first class objects in JS) to existing objects at any point.
im talking about (for instance)
line 16 (the 1st line i posted) and line 87
you just use this._scrollAnchorEntity to create an a new public variable (where is the declaration?)
and assign it the child this.entity.findByName('Scroll Anchor');? and since its global you use it again in line 87?
thus you mean that in every script i can create function scope variables by using var newVariable = something
or script scope variable this.newVariable = something
or static/global variable using scriptName.attributes.add('newVariable', {type: 'type', default: 400, title: 'new Variable'});
I think you have misread the code. _scrollAnchorEntity is never used without the this object. It’s always this._scrollAnchorEntity.someFunctionName();
Line 16 is in the class’s initialise function that gets called when the script instance is created and is first active. Line 16 is creating a new property of the class instance (this) called _scrollAnchorEntity and later use it on line 87.
No. Attributes are declared on the class but the data is assigned in properties on a per script instance. So each script instance will have it’s own set of data. Eg with this attribute, the property can be accessed via this.newVariable in the script.
so the initilize function is not only the function that is called first like Start() in unity,
it serves also the constructor function and what in JS I would have placed in the constructor (and can’t in pc.createScript I can place there.
means: defining methods and script wide variables.
It is not the constructor function just like Start/Awake in Unity are not constructors. The are functions that called before the first Update call is made when first active in the scene. The distinction is important to make as constructors are called when the object is created.
In Unity, it’s not an uncommon problem for someone to try to call a function on an inactive object in the scene that has never been active only for it to go wrong. This is usually because Awake/Start have not been called yet as the GameObject has never been made active.
But as people tend to think of them as constructors, they think it gets called automatically when it’s first created.
Back to PlayCanvas, initialize will gives the developer the opportunity to add properties to the class instance before postInitialize and the update calls.