I am using javascript with my game and I want to know how to modify the value of a scene variable using javascript.
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
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.
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
Thank you
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