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


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


#2

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


#3

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


#4

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): https://playcanvas.com/editor/code/560025?tabs=12879373

Edit: The docs have a pretty good example of procedurally creating entities as well: https://developer.playcanvas.com/en/tutorials/programmatically-creating/


#5
    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?


#6

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


#7

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


#8

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.


#9

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?


#10

hi Yauster

its the same code you shared here
https://playcanvas.com/editor/code/560025?tabs=12879373

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?


#11

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: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects#Objects_and_properties

Yes

Yes

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.


Button private exposed variable
#12

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.

correct?


#13

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.


#14

understood, thank you for the clarification
that was quite educating