[SOLVED] Relatively new here, would love some clarification please!

Hi all,

Although the title says I’m new here, I have been programming for around 5 years now (C++, C#, Java mainly) however when it comes to Javascript I am pretty dumb. I have a clear understanding of all the fundamentals however there are a few things tripping me up. When looking at current existing projects and code samples, I noticed one main thing which I would really appreciate some clarification on.

I would basically like to know the differences (if any) between writing a script like:

pc.script.create('scriptName', function() {    
    var ScriptName = function(entity) {
        this.entity = entity;
    }; 
    
    ScriptName .prototype = {
        initialize: function() {
        },
    
        update: function(dt) {
        },
        
    return ScriptName ;
});

and writing a script like this:

var ScriptName= pc.createScript('scriptName');

ScriptName.attributes.add('someAttrib', {
    type: 'entity',
    description: 'blah blah'
});

ScriptName.prototype.initialize = function() {   
};

ScriptName.prototype.update = function(dt) {
};

The second option is the template used when creating a new script in PlayCanvas, however looking at examples gives me more examples of the first layout. I have also noticed the use of private variables and functions in the first example, I might be missing something here but when using this.variable name in example two across different functions it seems to go out of scope and the variable becomes undefined. I don’t want to have to rely on global variables just to access them within their own class so any clarification would be great!

Thanks,
Gary

1 Like

Hello,

The first example is using the legacy script system - the second example is using the new script system. When you create a new project it will use the new script system by default. Some older examples are using the old script system which is why you’re still seeing it. However all the examples and tutorials at http://developer.playcanvas.com/en/ should be updated to the new script system (let us know if you see any old ones there).

Regarding variables the new script system is relatively straightforward as you would write code like in a typical javascript class. To access a variable of a script instance from another script you would do something like this:

var ScriptName= pc.createScript('scriptName');

ScriptName.prototype.initialize = function() {   
    this.variable = 5;
};

ScriptName.prototype.update = function(dt) {
};

var OtherScript = pc.createScript('otherScript');

OtherScript.prototype.initialize = function() {   
     // assuming ScriptName is attached to the same Entity as this script...
     console.log(this.entity.script.scriptName.variable);
};
1 Like

Hi,

Thank you for the clarification between the two. I think it’d be best I jump straight into the new script system so I can keep on top of it.

I’ll look into standard Javascript classes a bit more and make sure I completely understand how it’s scopes work before continuing. I have managed to get some cool functionality in this current project, but I was just losing references to variables so I must have been going wrong somewhere.

Thanks again!

1 Like

If you have swap method defined, you need to inherit private fields from old instance to new. I assume you do have swap method, and once you save file, it replaces instance in your application and looses private fields, that is why you getting undefined.
Let me know if my assumption is correct.

Hi Max,

Thank you for the reply. The undefined variables were actually within the scope of a callback and I didn’t realise that you had to declare a reference to this outside the scope and then “bind” it to the callback. Vaios pointed it out to me so that solved my problems.

Thanks for the information about the swap method though, I will bare that in mind for the future!

1 Like

JavaScript has “scoping” thing going on indeed, you will get used to :slight_smile:

Sometimes when I’m writing Javascript I want to throw up my hands and say “this is bullshit!” but I can never remember what “this” refers to

2 Likes

Haha, 10/10!

I do that all the time when it comes to pointers in C++ so I’m definitely expecting to have some of those moments with Javascript!

Thanks!