Global function in PlayCanvas

Hi all,

Is it possible to declare a global function in PlayCanvas to be used in all your projects? Where I can call it via function(x,y,z) from any other script in other projects?

I have found several resources regarding declaring variables, but not for functions:

How do you make a global constants file? - General - PlayCanvas Discussion

My apologies for the noob question.

Thanks

Hi @spike_bump,

A common approach in JavaScript application, not only PlayCanvas, is to declare a method on the window object.

That makes it globally available to any other script.

window.myMethod = function(message){
   console.log(message);
};

Now you can call it like this:

window.myMethod('Hey!');

Or even this:

myMethod('Hey!');
2 Likes

Thank you, Leonidas.