Use expressions in behavior properties or variable initial values #2342
Replies: 3 comments
-
I was offered a JS solution by Erigatohishima: |
Beta Was this translation helpful? Give feedback.
-
Properties are primitive types - they are not expressions. This can be seen in the UI because there is no expression editor to edit them - they are just a value.
It's expected.
Never use eval! I don't think JavaScript is even useful for that. eval is dangerous and is a nightmare for performance. Don't use it. JavaScript has functions to get a random value. GDevelop has one too. It is more than enough. Back to the original problem:
Is the behavior supporting to customize the speed with an action? If yes, why not using an action to set the speed to a Random value? Second solution, not involving any extra events in the scene events sheet: you edit the behavior, add a "onCreated" behavior function and inside it you set the property to a random value.
Not necessarily. This requires very careful thinking. There is a high risk of "order of initialization fiasco". What if you try to read the value of a variable inside the behavior... but the variable itself has its value initialized from another variable... which itself has a value initalized from the previous variable? We have two issues:
This being said, I understand the idea and that's maybe something we could add but with severely limited options, like only "pure" expressions like mathematical expressions, random ones and a few others. But in the meantime, better be explicit and just set your property using an action - if the behavior allow you to do so. |
Beta Was this translation helpful? Give feedback.
-
At least basic math like random would be nice, yes. |
Beta Was this translation helpful? Give feedback.
-
I recommended my TimedBackAndForth extension to a user, and he said he wanted to use a random speed.
It's not possible to type in math in a number field, and it's not possible to change the property through events (because it's a lifecycle method?)
I edited the extension, converted the Speed property to a string, edited the events to convert the property to a number, and it works fine if I enter 200 as string value, but it doesn't work when I enter ToString(Random(200)).
I tried to convert and display the property, likeso:
And I got:
I tried moving the problem to a variable, likeso:
But it's not working either.
Shouldn't variables and math functions be working in these fields?
Beta Was this translation helpful? Give feedback.
All reactions