Cannot read an attribute value of a script from another entity script

Hello, I am doing some tryings with this playcanvas editor and it is very good, but I do not know if I am doing it wrong (sure) but i am reading docs and forum and tryings things since 3 days and I do not get how to read the value of an atribute from a script of another entity.
I want this in order to check some value and call one function or another depending of this value.

Here are links to my project and also here I write some code.
https://playcanvas.com/editor/scene/524666

I know that are some other questions related but I read them and I dod not get it yet…

I have an entity with an script with some values of a game interface:

var PropertiesUI = pc.createScript('propertiesUI');
PropertiesUI.attributes.add('player', { type: 'number', default: 0, min: 1, max: 2});
PropertiesUI.prototype.initialize = function()
{
    PropertiesUI.player = 2;
     console.log(PropertiesUI.player);
    //in console log appears "2"
};

var MainJs = pc.createScript('mainJs');
MainJs.attributes.add('propiedades', {type: 'entity'});
//and parse the entity that has PropertiesUI script

MainJs.prototype.initialize = function()
{
  console.log(PropertiesUI.entity);
console.log(PropertiesUI.attributes);
console.log(propiedades);
console.log(propiedades.attributes.get('player'));
//this returns an object but without any value
};

The only result I obtain is an object, or undefined, or something like “ScriptAttributes {index: Object, scriptType: function}” or “Func {name: “propertiesUI”, tags: Tags, _labels: Object, localPosition: Vec3, localRotation: Quat…}”

How do I read the variable player from MainJs, which is in another entity?

Thank you for your help :slight_smile:

With this attribute, propiesdades is a reference to an entity:

MainJs.attributes.add('propiedades', {type: 'entity'});

So therefore to get to the scripts of that entity, you just have to do the following for example:

MainJs.prototype.initialize = function()
{
  console.log(this.propiedades.script.propertiesUI.player);
};

I also see that you have made an error with the PropertiesUI script.

PropertiesUI.attributes.add('player', { type: 'number', default: 0, min: 1, max: 2});
PropertiesUI.prototype.initialize = function()
{
  PropertiesUI.player = 2;
  console.log(PropertiesUI.player);
};

Here you are creating a player variable to the global PropertiesUI script object ‘template’ (for lack of a better word) rather than the script instance. Also, as player variable is already an attribute to the script, you don’t need to redefine it. ie:

PropertiesUI.attributes.add('player', { type: 'number', default: 0, min: 1, max: 2});
PropertiesUI.prototype.initialize = function()
{
  console.log(this.player); 
  // This will output whatever value you assigned to it in the editor
};

I can explain the different bits and pieces in more depth if you need it, but you should get the general idea on how the structure works from this.

1 Like

Thank you for your reply, I am going to update my code :slight_smile: