Hi all,
GDevelop 5.4 rolled out an update to variables, which we can call “unified variables”, as well as support for local variables and variables restricted to extensions. We had seen a lot of different questions here and on the discord, so we wanted to put out a unified post rather than one-off responses.
tl;dr:
- Games are not breaking. They will work as before. Don’t panic!
- Still, you must declare variables, but can do this progressively as you work.
- This is not always convenient so we’ll make a UX improvement to address this/speed up declarations.
- The new system is safer, simpler, more flexible and not mistake prone and will save you minutes if not hours of debugging and yelling at your screen. We’ll update tutorials, the documentation and almost all examples soon (in a few days).
Long version:
As with all updates since years, the update in the variables is not breaking any existing game. The new system requires you to declare variables though, which is a change in most people workflow (despite recommending to declare variables since many months, I understand many creators have been avoiding this).
I’ve seen a few people complain because of this new requirement.
This is understandable as it requires a bit more work rather than being just able to write the name of the variable and go ahead (though this is not without risk. Did you ever mistyped a variable name? Keep reading!). Groups also require all their objects to have the same variables so that they can be used.
This can be “flow breaking” but I think this is totally fixable from a UX point of view by having, when you write a variable name in a field requiring a variable, something displayed like “Declare a new Xxx variable…”. When clicked, this will ask you where to put the variable (scene or global, scene by default) and the type (number by default).
This being said, the new system is here to stay as it provides many advantages:
-
This is simpler: there is now a single action to change a variable. The action adapts to the variable type and works for all variables: global, scene, local.
- This means you don’t have to choose between scene, global (or local) actions/conditions, and then between number, string or boolean.
- This means the search is now returning a unique action instead of 6 before (or 9 with local variables!) (or even more with extension variables).
- This is akin to most programming languages where you can just do
myVariable = 3
(after declaring it earlier). There is a single symbol to affect a variable (=
), whatever its type.
-
This avoids mistakes and long debugging times:
- This avoids beginner mistake to declare a variable somewhere, like a global variable, and then use an action to change or a condition to check a scene variable. Now, it will just works (and you can move it later if you did a wrong choice).
- This avoids you to use a wrong type with a variable. GDevelop will show you an expression editor that is adapted to the variable type. This is good for beginners to understand that a variable is not supposed to go from a string to a number (which is not performant) (but GDevelop will support this in case your force it to do so).
- This is safer as a mistake like putting a letter with a wrong case (
Health
vshealth
) is not possible anymore. GDevelop will tell you that the variable does not exist.
This was a mistake observed for every single new user and a very frustrating one.
This is akin to most programming languages that errors when using an undeclared variable (or even prevent you from running your script/compilation) or when affecting a wrong type to a variable (example: TypeScript and most typed languages).
-
This is more flexible:
- If you move a variable from a scene to a global variable, or to a local variable, or vice versa, your events will… just continue to work as before.
Previously, you had to rework all the actions and conditions and expressions using it. - This is even more flexible: extensions can now take “variable” parameters. Which variable though, global, scene or local? Any of them will work.
- Same for expressions. An expression taking a variable as a parameter will work with any of them. Tell good bye to
GlobalVarToJSON
, say hello toToJSON(anyVariable)
and it just works.
This is akin to most programming languages where a variable can be moved from one scope to another.
- If you move a variable from a scene to a global variable, or to a local variable, or vice versa, your events will… just continue to work as before.
-
This is more concise in expressions (this was introduced before GDevelop 5.4):
- You can now write
"Your score is" + Score
instead of"Your score is" + VariableString(Score)
. - Better, you can now write
MyVariable[index + 3]
instead ofGlobalVariable(MyVariable[Variable(index) + 3])
. I let you imagine the result if we had to support local variables.
This is also akin to most programming languages where variables can be be written and it just works.
- You can now write
All of this allowed us to roll out local variables, but also scene/global variables scoped to an extension (useful for extension builders). In the future, all extensions actions/conditions/expressions will work with any variable. No more duplication. We might even think of things like scene folders that contain variables (so, “semi global” variables ;)).
I understand declaring variables might seems like a pain (though this is required in every programming language and other no-code tools), but I think this is actually a “sounds worse, but actually is better” solution (as it teaches beginners that variables are really “something that exists” in memory, with an initial value you can choose). If it was like this since the beginning, nobody would question this Still, we’ll work on:
- How to declare in ~2 clicks a variable from the action/condition editor. Remember you can open scene and global variable editor from here already. Prototyping fast is important.
- See if we can we make it easier to declare a variable for all the objects of a group.
I’m leaving this thread opened but remember to stay civil/polite in your feedback.
I encourage you to give it a try for a few days to fell how “good” it feels to just declare your variable once and be using it anywhere without any risk. It’s very pleasant!