[Solved] How do I access scene variables using javascript?

I am using javascript with my game and I want to know how to modify the value of a scene variable using javascript.

I’m not sure but I think you might find something useful in the game engine documentation.

I want to know this too, I am surprised there isn’t a clear simple way to do this given how basic a need it might be to access/alter a variables in script. This post is old and still no definitive method given :frowning:

Set value of scene variable:

runtimeScene.getVariables().get("sceneVarName").setNumber(100);
runtimeScene.getVariables().get("sceneVarName").setString("new text");

Get value of scene variable:

var sceneVarNumber = runtimeScene.getVariables().get("sceneVarName").getAsNumber();
var sceneVarString = runtimeScene.getVariables().get("sceneVarName").getAsString();

Set value of global variable:

runtimeScene.getGame().getVariables().get("globalVarName").setNumber(100);
runtimeScene.getGame().getVariables().get("globalVarName").setString("new text");

Get value of global variable:

var globalVarNumber = runtimeScene.getGame().getVariables().get("globalVarName").getAsNumber();
var globalVarString = runtimeScene.getGame().getVariables().get("globalVarName").getAsString();

Set value of object variable:

var objects = runtimeScene.getObjects("objectName"); //return an array of all instances
objects[0].getVariables().get("objectVariableName").setNumber(100); //set number value  of first instance
objects[0].getVariables().get("objectVariableName").setString("new text"); //set text value of first instance

Get value of object variable

var objects = runtimeScene.getObjects("objectName"); //return an array of all instances
var objectVarNumber = objects[0].getVariables().get("objectVariableName").getAsNumber();
var objectVarString = objects[0].getVariables().get("objectVariableName").getAsString();

Set value of a child in a structure variable

runtimeScene.getVariables().get("structureVarName").getChild("childVarName").setNumber(100);
runtimeScene.getVariables().get("structureVarName").getChild("childVarName").setString("new text");

OR

var structureVar = runtimeScene.getVariables("structureVarName").getAllChild();
structureVar["childName"].setAsNumber(100);
structureVar["childName"].setAsString("new text");

Get value of child from structure variable

var structureVar = runtimeScene.getVariables("structureVarName").getAllChild();
var childVarNumber = structureVar["childName"].getAsNumber();
var childVarString = structureVar["childName"].getAsString();

That’s a beautiful reference! I’m going to keep that handy thank you!

I wish the GD JS script documentation was as clear with real samples rather than just method references to objects without showing any real way to access them.

Thank you @ddabrahim for your help

We can get value of child, but what about key name ?

| structureVar
–| Child_one : Hello
–| Child_two : world

How get “Child_two” from Sentence.
Nothing like this exist i guess ?
ReturnKeyNameOfChildInStructure(Sentence, 1)

You can feed the keys in to an array using Object.keys() method

So you get the structure variable with all children
var structureVar = runtimeScene.getVariables().get(“structVariable”).getAllChildren();

It is going to return an object with all the childs as properties.
Then, you can feed all property names in to an array like so.
var structArray = Object.keys(structureVar);

Then you can get the name of the first element using structArray[0] for example.

Wonderful :sun_with_face:

Thank you :smiley:

Hi,

how can we access to structure used as dynamic arrays like in this topic http://wiki.compilgames.net/doku.php/gdevelop/tutorials/simulatingdynamicarrayswithstructurenotation into javascript

thank you

Someone on the forum answered to set a global variable value in javascript it looks like this:

runtimeScene.getGame().getVariables().get(“globalVarName”).setString(“new text”);

I don’t know what to say it just doesn’t work.

i tried like
runtimeScene.getGame().getVariables().get(glossary).setString(myLex);
and
runtimeScene.getGame().getVariables().get(“glossary”).setString(“ScrapShot”);

It flashes me a critical error in the debug log that “glossary” is not defined.

I open global variables in the editor and i do have a variable named glossary which is a Text variable.

EDIT: I think you are using the wrong marks.
Instead of using “ you should be using "

So try this code instead:

runtimeScene.getGame().getVariables().get("glossary").setString("ScrapShot");

EDIT: Since the original post the forum engine was changed and it seems the new forum engine was using the wrong marks. Putting the code in to a code block seems to fixed the problem, so try to copy the code from the code block.

It should work now.

In case it still does not work for you and you get an error regarding “glossary” is not defined. Are you certain “glossary” is defined as a global variable and it is not a scene variable? In the latest version of GDevelop you need to define global variables using the global variable editor before you can use them.