Storing variable locally to only that instance of the script

so I have a script that takes a number as a string in the attributes and then I want to store that variable locally, accessible only to that only instance of the script and not be a global variable, as far as I have seen the var collides with other instances of the same script, so what is the correct way to create local variables? if its let then where should the let be first declared?

Something like this?

YourScript.attributes.add('someAttribute', {
    type: 'string'
});
YourScript.prototype.initialize = function() {
    this.localVariable = this.someAttribute;
};
YourScript.prototype.randomFunction = function() {
    console.log('my local var:', this.localVariable);
};

But, you could just use the attribute directly with this.someAttribute too.

2 Likes

ok chris, lets say I have 2 of this script assigned to 2 different game objects

one has hello as the attribute and another one has world as attribute, would that mean both the scripts will hold their own hello and world? or will this script create one localVariable that is one and only and shared between multiple instances of the same script?

execution order:
YourScript("hello")
YourScript("world") // not really accurate representation but you get the idea

will the result be this?
option A:

result:
world
world

or will the scripts hold their own variables and the result will be like option 2:

result:
hello
world

I would expect every time a script is used in the PlayCanvas scene, a new instance of the script is created. So, option B should happen. this.someAttribute and this.localVariable would both be unique for each script.

1 Like

Hi @sir_akc,

The two different scripts would maintain separate attributes even if the the attributes had the same name. The reason for this is because when you apply the script to the object, you are applying an instance of it. To illustrate:

If you had two Scripts:

var Script1 = pc.createScript('script1');

Script1.attributes.add('number', {
    type: 'number',
    title: 'Number',
    default: 5
});

// initialize code called once per entity
Script1.prototype.initialize = function() {
    
    console.log(this.number);
};

// swap method called for script hot-reloading
// inherit your script state here
// Script1.prototype.swap = function(old) { };

// to learn more about script anatomy, please read:
// http://developer.playcanvas.com/en/user-manual/scripting/

and

var Script2 = pc.createScript('script2');

Script2.attributes.add('number', {
    type: 'number',
    title: 'Number',
    default: 10
});

// initialize code called once per entity
Script2.prototype.initialize = function() {
    
    console.log(this.number);
};

// swap method called for script hot-reloading
// inherit your script state here
// Script2.prototype.swap = function(old) { };

// to learn more about script anatomy, please read:
// http://developer.playcanvas.com/en/user-manual/scripting/

Script1 would log 5 on initialize, and Script2 would log 10.

I hope this is helpful.

1 Like

You can see the different values of any attribute for every instance easly in the editor :slight_smile: if you add the same script to two entity, parse the script, you can enter a different value for each…

anyone tried using var with same variable name? I tired it, that var gets copied or something over all instances

That makes sense as when you use var it is not instanced. It is part of the blueprint that the instance is created from. That is why you would want to use either a script attribute or this. before your other variables.

This reply, of course, is assuming you’re only declaring your variables within the scope of the script and not globally. If you are declaring them globally, then the script to get loaded is what the final iteration of that variable will be.