How do i edit the Create function to add params to constructor?

var SomeScript = pc.createScript('SomeScript');

An attempt like this yielded error (missing semicolon)

var SomeScript = pc.createScript('SomeScript', arg1,arg2,..){
    Name1= arg1;
    Name2= arg2;
SomeScript .attributes.add('Name1', {type: 'SomeType1'});
SomeScript .attributes.add('Name2', {type: 'SomeType2'});

The constructor is not exposed in PlayCanvas scripts. What are you trying to do/solve?

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.

I do something similar in my Isometric Builder project where I dynamically create the UI (I do do a bit more and use pooling and events to populate but it’s the same principle):

Edit: The docs have a pretty good example of procedurally creating entities as well:

1 Like
    this._scrollAnchorEntity = this.entity.findByName('Scroll Anchor');
    this._cancelButtonEntity = this.entity.findByName('Cancel Button');
    this._errorMessageEntity = this.entity.findByName('Error Message Panel');

is this a way to define private params? no var?

Yes, that looks right at a glance. No car needed as you are adding a property to an existing object (the this object).

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.

Err… that doesn’t sound right. Can you show the whole file please?

Edit: Just to check, you know what the this reference in C# is right?

hi Yauster

its the same code you shared here

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'});

am i correct?

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.

I would read about objects and properties, it will explain it a lot better than I can:



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.

1 Like

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.


More like Awake() in the strictest sense.

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.

understood, thank you for the clarification
that was quite educating