[SOLVED] Script communication

Hey All,

Just set up the account to explore PlayCanvas option. Trying to work out some of the basics and I am struggling with how to get classes to talk to each other. Below is s very simple example of what I am trying to do

I’ve created a Game.js script and attached it to the Root. I’m trying to call a function in the scriptB.js file.

Game.js file

Game.prototype.initialize = function() {

   ScriptB.alertMe();

}

scriptB.js

ScriptB.prototype.alertMe = function() {
    
    alert("Found the Script");
    
}

My questions are. where do I attach ScriptB to get it to be included in the game package?

What steps do I take in Game.js so I can call the function in, ScriptB.js?

I’m sure this has been asked and answered many time, and I have looked, but I can’t find anything that is working or I understand.

Thanks,
Ross
.

There’s a thread here that might help: How to call other script file functions?

TLDR, you need a reference to the entity that has Script B to be able to call functions/access properties.

If it’s on the same entity, you can do this:

this.entity.script.scriptB.alertMe();

Assuming the script name is scriptB.

You can also use events to communicate/broadcast messages across the app/entities/objects: Communication | Learn PlayCanvas

That worked perfectly, thank you sir.

Ross