Creating Global(Scene Wide) Variables(Solved)

Hello, I have a project I am working on in the threejs editor, I want to be able to set a variable in one script, and be able to access it in another in different objects. In essence, can I create a global variable, that can be edited, and read, and used in statements(If, for, etc.). I was unable to find this online, so I decided to ask. Thank you in advance to those who answer.

  1. There exist a few globally accessible variables (scene being probably what you’re looking for) - docs. You can append any arbitrary data to userData property of that scene object:
scene.userData.someValue = "test"; // NOTE scene.userData.someValue will be accessible from any script
  1. You can also take advantage of the global window for example. In your Scene (or any other initial script), just do:
// NOTE Scene.js script
function init() {
  window.appData = {
    someValue: "test"
  }; // NOTE window.appData will now be accessible anywhere within the app

// NOTE Ground.js script
function update() {; // NOTE "test"

Alternatively, you are also free to use JS events system, which is accessible globally.

Thank you, just to clarify, you can recall the value in userData by doing something like:

if (scene.userdata.someValue == true) {

This worked thank you