Is there a way to call upon a "this." variable in a different script

See where the black arrows are pointing ‘this.timesLooped’

is there a way I can call this in another completely different script and change the value of it. I understand that the values found in the prototype function are unique to whatever is contextually attached, but was curious if there was a way to influence that value.

Also another question is is there a way to change the ‘attributes’ using scripts.
These are the attributes I’m referring too.

I’d like to also be able to dynamically change these in game if need be. For instance change the Frame rate to be 6 instead of 12 to create a ‘slow mo’ effect or take it up to 24 to create a ‘super fast’ effect.

Thanks :slight_smile: I’ve been absent for a bit, because I just launched my website! I was using playcanvas to do most of this stuff.

in the other script initialize you can use this.spriteSheet= app.root.findByName(‘ENTITYNAME’).script.animatedSpritesheet; to point to your script and in your code this.spriteSheet.Frame Rate= new value;
I supose this is what you was asking.

Looks like it, I’ll give it a shot!

f’n works!

Thanks @ayrin

This is going to be a more versatile sprite sheet that people could use for their 2d sprites. Thanks for contributing your epicness. :smiley:

I have a feeling I’ve missed the question here but isn’t this just this.attributeName = newValue?

Adding to ayrin’s answer, if you don’t want to use findByName on root (as it is technically quite expensive to call), you can reference an entity as part of a script attribute. Example here:

So what you are suggesting works within the same script. Which is fine if I want to do something like translate it or adjust it’s values, but the question was getting into the entities script itself.

So if EntityONE has a script called ScriptOne

And I make EntityONE an attribute to let’s say ScriptTwo, what I wanted to do was using ScriptTwo, to change the values found in ScriptOne.

So for instance this code makes an undefined variable


//this also comes back undefined.


//but this comes out with a value'Numbers').script.animatedSpritesheet; // this code works great

The ‘Numbers’ part which is just the name of the entity in the heirarchy side pane. It would be nice to get the ‘Numbers’ part to be automatic since I plug in that same entity as an attribute. So how do i get the NAME from the attributed entity.
Like where it says Animation Two.

In ScriptTwo, you can access the EntityONE’s ScriptOne attributes and variables as so:

var ScriptTwo = pc.createScript('scriptTwo');

ScriptTwo.attributes.add('entityONEReference', {type: 'entity'});

ScriptTwo.prototype.initialize = function() {
    this.entityONEReference.scriptOne.someAttribute = someNewValue;
    this.entityONEReference.scriptOne.someVariable = someOtherNewValue;

In short, attributes, variables, functions, etc in the PlayCanvas scripts are all properties on the script object.

If you are still having problems, can you share a link to the project?

Great let me try this I think I get what you are getting at lol :slight_smile:

Yea your code doesn’t work :frowning:
But here is the project

Press 1 and 2 to toggle between the two sprites.

the ROOT has the animationController script
and the Numbers/Coin entities hold the animatedSpritesheet scripts.

Can you share the project itself rather than the published build please?

Oops. Typo.

FYI: returns an entity object reference.
Whoops! Here it is! Also I did try the one you just corrected too. ;(

Here’s my fork of the project:

And direct link to the script:

1 Like

nailed it! thanks i must have wrote it wrong.

Sorry @yaustar i don’t quite get it instead of using app.root.findByName(‘ENTITY’).script.SCRIPTNAME

Instead of searching for the entity by name using findByName(), you can instead use the script attribute to store the reference to the entity directly as shown in the example I just gave RobotPencil.

var AnimationController = pc.createScript('animationController');

AnimationController.attributes.add('animOne',{type:'entity' , title:'Animation One'});
AnimationController.attributes.add('animTwo',{type:'entity' , title:'Animation Two'});

AnimationController.prototype.initialize = function() {
    //this is where you'll need to change the 'Strings' to the corresponding entities
    this.spriteSheetOne = this.animOne.script.animatedSpritesheet;
    this.spriteSheetTwo = this.animTwo.script.animatedSpritesheet;

Ok thanks got it, and this is less expensive ?

I’m not sure on the exact reason why it performs better, but perhaps instead of scanning throughout the whole engine for the specific code, you have it already ‘parented’ to the context of the object you are using, so in that sense it goes less far, if I had to guess.

I mean, my testing app is super small since I’m only using a few elements, so it performs identically. But I’m curious if you end up having 100’s of things if it would change dramatically. Another problem i can see happening with the’findByName’ is that if you have multiple objects that happened to be the same name, and you don’t want them to be affected, using attributes is ideal, since you can lets say have one code that only affects that one entity, and another code that effects ALL entity’s by that same name.

But I’m just guessing.

I will let you know when i replace all my findByName in Kallen

I have made a quick test yesterday but don’t seem to work is this correct? or is script 1.0 fault?
i have tried this pc.script.attribute(‘Root’, “entity”,“Root”); and also with just (‘Root’, ‘entity’) but no luck