Hi!
How can I store javascript array in Gdevelop for using them in different iterations?
Thanks
1 Like
Hey! I am not sure what you exactly mean. If you mean just saving the array in a non temporary, there are two options:
- The “JS way”: Create a global and store the array here:
gdjs.myArray = gdjs.myArray | [];
gdjs.myArray.doStuff(); // Your manipulations
- The “GDevelop way”: store it as a GDevelop structure:
let variable = runtimeScene.getVariables().get("myArray");
let array = [];
array.doStuff() // Your manipulations
// For saving the array;
for (let i = 0; i < array.length; i++) {
variable.getChild(i).setString(array[i]); // Assuming you store strings.
// If you store an object you will have to make it a String using JSON.
// If you want to store a number use setNumber.
}
// To recover
let alllVars = variable.getAllChildren();
for(let item in allVars) {
let key = Number(item);
if(key !== NaN) {
array[key] = allVars[item].getAsString(); // Again modify that depending of your variable type.
}
}
Those snippets are not certified to work out of the box, I did them from memory!
4 Likes
Thank you very much!
1 Like