Accessing functions from a referenced entity in another object

Hello all,

I’m still getting my bearings after moving from Unity. One issue I’m having that will probably be an easy fix(!) is accessing functions from other objects. I want a game manage which will have references to other management classes. Firstly, whats the best way to reference this? I currently create an empty object and attach the script to that, then I can add the other objects to the manager script. Secondly is this the best way to reference them:

GameManager.attributes.add('questionManager', {
    type:'entity',
});

GameManager.attributes.add('uiManager', {
    type:'entity',
}); 

I’ve looked through the docs and it says this is the best way to reference an entity. In the GameManager class I’m trying to access a function from uiManager but I cannot find the correct way to do this.

I’ve seen techniques like // this.uiManager.script.changeQuestions("some text") but I get errors such as uiManager not defined, even though the reference is there in the editor.

BTW this is the function I’m trying to call in the referenced class.

Uiupdater.prototype.changeQuestions = function(question) {
    this.questionText.text = question.question;
};

I’m trying to get my code organised with relevant managers for each functions but this seems pretty difficult at the moment and I don’t really want to start making god classes.

Any help would be much appreciated!

Mark

Can you share the project? It’s a bit difficult to give the exact fix with the code you have posted.

For reference, the Model Viewer does something similar: https://playcanvas.com/editor/code/446385?tabs=6079417 Line 19

I’ll post a link shortly, but interestingly I did a console log on this.uiManager and got this in the inspector:

Object { name: "UI Manager", tags: {…}, _labels: {}, localPosition: {…}, localRotation: {…}, localScale: {…}, localEulerAngles: {…}, position: {…}, rotation: {…}, eulerAngles: {…}, … }

So the object reference is there it’s just how to access the script element and the functions that eludes me at the moment.

Hey,

So to access a function in a script attached to an entity you have to do entity.script.[scriptname].function(), it looks like you’re missing the script name in your function call.

Thanks for that, I’ve tried that too but still no joy. I’ve even entered a bit of a scatter-gun approach and tried virtually function call I can.

The link to the project editor is :
https://playcanvas.com/editor/scene/580974

The game manager is attached to the camera with the entities added in the inspector. The code I’m trying to run is in the game manager class if anyone can help.

Thanks,
Mark

I did a fork here: https://playcanvas.com/editor/scene/581704

Ultimately, you needed to do this:
this.uiManager.script.uiupdater.changeQuestions("s");

I noted that you had this in a console log as well doing:
console.log(this.uiManager.script.uiupdater.changeQuestions("s"));

This will print undefined as the changeQuestions doesn’t return anything.

1 Like

You sir are a legend. So I need the use the createScript name as opposed to the actual file name, man that was frustrating.

Thanks again!

Mark

hello
i attempted to create the following:

var SomeScript = pc.createScript('SomeScript');

SomeScript.attributes.add('_model', {
    type:'entity',
});

var att = self._model;
// initialize code called once per entity
SomeScript.prototype.initialize = function() {
   var model=this.SomeScript;
    console.log(model);
    console.log(att);
    console.log(att.script);
    console.log(model.script);
    
};

// update code called every frame
SomeScript.prototype.update = function(dt) {
    
};

the goal is to reach to the scripts attribute of the model so I can eventually activate a function on another script.

as you can see the attempt is both on the same model and the variable model.
in both cases console.log(model); and console.log(att); the result was undefined.

in both cases console.log(att.script); and console.log(model.script);I receive the errorCannot read property ‘script’ of undefined`
could you help me figure what did i do wrong?

When you add an attribute to a script, it’s accessible by this.attributeName .

So in your script you have the attribute _model which is referencing an entity. You can access it in the script functions via this._model.

gatcha

but, on this script theree is another script - ChangeMaterial2

var SomeScript = pc.createScript('SomeScript');

SomeScript.attributes.add('_model', {
    type:'entity',
});

// initialize code called once per entity
SomeScript.prototype.initialize = function() {
   var model=this._model;
    console.log(model);
    console.log(model.script);
    console.log(model.script.ChangeMaterial2);
    
};

// update code called every frame
SomeScript.prototype.update = function(dt) {
    
};

I believe I have done as you described above, but still - looking for the script inside “Scripts” returns undefined.

as seen in the image, it can be confirmed that the ChangeMaterial2 is present in the scripts object

At a rough guess, you are are not using the name of the script as shown in a previously reply. Try changeMaterial2 instead of ChangeMaterial2.

If you are still having problems, please post a link to the project.

1 Like

facepalm
thanks, the whole process is clear now

your help was much appreciated