☑ Call a function in a script on another entity

Hi guys,

before the new scripting system was out, we used to call functions on scripts on other entities like this:

var someEntity = app.root.findByName("SomeEntity");
someEntity.script.myScriptName.myFunctionName(someParameter);

but it seems that that is not how it works anymore since the console doesn’t show the functions defined in the scripts if we print out ‘someEntity.script.myScript’, it shows all variables defined like this:

this.myVariable = someValue;

but it doesn’t show the functions defined on the prototype like:

MyScript.prototype.myFunction = function(pParameter){//doStuff};

so in the new scripting system what is the best way to call a function of another script?

1 Like

Hi,
What you are proposing should work (I use script communication a lot)

Please note you should be using this.app.root rather than app.root

Thanks, but we are using "this.app"
I was just making an example from how it used to be coded and before the new script system it used to be app.root.find

Hi, Christian.

I would like to using this.app.fire() and this.app.on(). By using this combination, you can also pass some parameters from one script to another script.

Something like that:

In A script :
var paras = "Hello, PlayCanvas !"
this.app.fire('game:start', paras);

In B script:
this.app.on('game:start', foo, this);
var foo = function(paras) {
alert(paras);
};

For more details see url :
http://developer.playcanvas.com/en/user-manual/scripting/communication/

1 Like

Hi Wing,

we know about the event listeners, but they are not always convenient since you have to set up a listener and an event which can blow up your code. Too many event listeners can also have negative impact on the performance of your applications,
Therefore we are looking for the more direct way to call functions on another entity, but thanks for your reply.

The way you call functions on another script instance hasn’t changed. You can still do

otherEntity.script.scriptName.myFunction()

2 Likes

Actually, I am a novice in PlayCanvas.:grin: And I also met this problem a few days ago. I have no idea about that. So I just used the event listeners.:grin: Thanks for your remind about the event listeners’ nagative impacts. I am looking forward to the answer, too.

In that case it should work fine, perhaps you could share a small example where it’s not working for you.

[SOLVED]
Hi @vaios,

the problem might be another in the case that we’re using it then,
I tried in a blank project and it works just as you said, but in our use case I’m still getting an error:

ent.script[pScript][pFunction].apply(undefined, this.targetParametersArray[functionIndex]);
ent is a variable filled with a target entity, pScript is a string equal to the name of a script on that entity, pFunction is a string which equals a function in the script and finally this.targetParametersArray[functionIndex] is a random string.

The error I see in the console sais: Uncaught TypeError: CreateListFromArrayLike called on non-object and references the line of code I mentioned above.

Are we missing some conversion or something like that, or do we have to address the functions of the script differently? I can print all of the variables into the console so they seem to be working.

EDIT:

Sorry for the ruckus - we just found the problem, which simply was that we gave apply a value out of the array instead of the whole array

MEA CULPA