Improve variable usability

I am often frustrated when using variables in GDevelop, and miss some JS/TS constructs that are really nice. Here are some things that I wish for variables:

Allow freezing variables

Allowing to make frozen variables. This would allow to prevent errors like overwriting a variable on accident. GDevelop would also be able to warn you if you use for example an non existing child because of a typo, as it would know that this variable is not meant to be have new children added to it.

Inline collection variable creation

Allowing a syntax like this:
Variable({ a: "structure", withNumber: Object.X(), myVar: AStructureVariable, inlineInInline: { a: 0 }) or Variable([1, "e", P2P::GetID()]) to create an inlined variable as a parameter. The output code would look like this:

// For inline structures
myActionThatUsesAVariable((function() { 
  const inlineVar1 = new gdjs.Variable({ type: "structure" });
  inlineVar1.getChild("a").setString("structure");
  inlineVar1.getChild("withNumber").setNumber(objectsList[0].getX());
  gdjs.Variable.copy(
    runtimeScene.getVariables().get("AStructureVariable"), 
    inlineVar1.getChild("myVar")
  );
  gdjs.Variable.copy((function() {
    const inlineVar2 = new gdjs.Variable({ type: "structure" });
    inlineVar2.getChild("a").setNumber(0);
    return inlineVar2;
  }), inlineVar1.getChild("inlineInInline"));
  return inlineVar1; 
})());

// For inline arrays
myActionThatUsesAVariable((function() {
  const inlineVar3 = new gdjs.Variable({ type: "array"}); 
  inlineVar3.pushValue(1);
  inlineVar3.pushValue("e");
  inlineVar3.pushValue(gdjs.evtTools.p2p.getID());
  return inlineVar3; 
})());

This would be especially useful with for example firebase, where you currently have to do something like this: image

Custom variable types

I often want to work with arrays, but they have a small problem: unless i predefine all indexes and don’t use expressions in the variable path, I cannot get autocomplete on children. This is annoying, as it takes more time and can allow me to write an invalid path on accident. If we could make a custom type with an interface like the variable editor, I could for example if I want to do a To do items, I would make a Todo type that is a structure that looks like { heading: string, message: string, dueDate?: string }, and make a TodoList that is an array of Todos, and set my list variable as of type TodoList.

Then, GDevelop would now that whatever index of list i am accessing, it will be a structure with the children heading and message which are strings, and dueDate that is either a string or not there (= number 0) and provide autocomplete.

It would also tell me if i forgot a property when adding an item with the inline variable (for example if i try to add {message: "", headng: ""} to the list).

Variable parameters would also be able to ask for a specific type, so that if you pass in a variable that isn’t either untyped or typed with the asked type it would warn you too.

Extensions would be able to define variable types too.

tldr; TypeScript for GDevelop

1 Like