Simple questions on variables

How would I get a variable from a different script to work with a separate script.

For instance script A, may have a variable called cheeseTots.

and I want to use that variable to be added to the hunger variable that is found in script B.

then take that value and quantify how hungry the player may be. Now ofcourse I can put this all into one script, but was curious if there was a simple solution.


To directly answer your question, yes it is possible to access scripts from another entity or the same entity as another script.

An example would be in the Model Viewer starter kit on line 19 here: PlayCanvas | HTML5 Game Engine

The mouseInput script stores a reference to the orbitCamera that is attached to the same entity and then calls functions, attributes or even access variables from that script instance. (Example being on line 112 and 113).

The other way for scripts to interact with each other is the event system that can be found here:

And a project example here:

A practical example of events in a game can be found in Flappy Bird example here: PlayCanvas | HTML5 Game Engine

To answer the question in a roundabout way, I would say that you may want to think about where this data should owned and whether they need to be accessible. If it needs to be accessible across the whole application, perhaps it shouldn’t be owned in a script but in something more accessible to the project.

Again the Flappy Bird project is a decent example of a project layout as well as the Model Viewer Start Kit.