Accessing RuntimeScene variables using JavaScript

I have created a game with a single scene called ‘Level 1’. I have also defined some scene variables as well. I have exported the game as HTML5.

I am working with the index.html file in the exported game. I am running a simple server in vs code to test the game. The index.html file has the default script to create a RuntimeGame object and start the game level. When I run the game everything works as expected.

My question is how do I access the scene variables in the script?
Below is a snip of the script section in the index.html file. I have added some code to this.

So far I have tried to

  1. access the SceneStack of the RuntimeGame object and then get the current scene and tried to get the variable but this does not work. This return a Null;
    var my_b = game.getSceneStack().getCurrentScene();

  2. var sceneVarNumber = game.getSceneData('Level 1').name;
    This does work and returns me the name of the level.

  3. game.hasScene('Level 1');
    This also works and returns true.

  4. I taught that the scene might not have loaded when querying for the variables so I registered a callback function to let the screen load first using
    This does trigger the callback function mentioned but when I check if the first scene was loaded inside the callback it returns false.

I have been scratching my head for 2 days with this now.
I have read through the entire documentation to find a solution, but no luck so far.

I would also like to get clarity on how the game is set up and run from the script in the index file.
This is what my understanding is

  1. A RuntimeGame object is created.
  2. Somewhere inside the RuntimeGame object creation a SceneStack is created
  3. startGameLoop() loads a scene from the SceneStack and runs it
  4. gdjs is a global variable

The way to get the scene variables looks like this:


The way to get the global variables looks like this:


And to set the variable (same for scene variable):


Also, as far as I know, you are correct on the other things. gdjs is a namespace though, not a variable.

1 Like

Here’s a detailed response on accessing variables in javascript from an earlier thread about it.

Thank you for the code snippet.

I have tried these method calls. These work well inside the JavaScript event block in the editor.

But I want to access them after the game is exported into a HTML5 format. So I am trying to access it inside the script tag of the HTML file as mentioned in the question.

Thank you for pointing out to earlier threads.

But I have already go through them. They mention how to access the scene variables from inside the JavaScript event block. I want to access them after the game is exported into HTML5.

Why do you want to do that?

And it may not be possible if the javascript is compiled; the variables would lose their names.

var varname = game.getSceneData().variables[0].name;
var varvalue = game.getSceneData().variables[0].value;

Hi j2I,
Thank you for the code snippet. It works and is able to fetch me the variable name. But the value returns undefined. I guess this is because the scene has not been loaded. But I have the code inside the callback function registered using


But for some reason when inside the callback I check for if the scene is loaded using the below code it still returns false.


Also can you please explain the code snippet provided a bit more? I didn’t understand how the variables are stored inside the array. What if I wanted to edit a scene variable?

Also, can you point me toward where is the documentation regarding the getSceneData? What all data is returned by this method?

Hi all,
I am just posting my findings for anyone interested in doing something similar.

var my_b = game.getSceneStack().getCurrentScene();

My initial approach of using the SceneStack to access the Scene Variables was actually correct.
The only issue was that I was trying to access the Scene Variables even before the scene was loaded.

If you would wait long enough to let the scene load and then use the usual approach as highlighted by cnem would work to access and modify the scene variables.

Now the question is how to know if the Scene has actually been loaded. To solve this I tested out the
The above callback function, us supposed to work if the scene has loaded. This call-back function is available under the gdjs namespace. But the only issue is that even after waiting for the callback the scene does not seem to have loaded. So we will have to implement some workaround to solve this issue.