Variable used either as number either as string

Hi!
My question is about the best practice.

In my project I have often variables that I want, depending on the case, use/read either as strings, either as numbers.

When I started using Gdevelop more than 2 years ago, I think I read somewhere that if asked a string when the variable is a number, gdevelop would automatically make it a string for me.
And vice versa, if asked a number, GD would try to make the string a number, if not possible it will be 0.

According to this beleif, I started to take this habit :

  • when the value of my variable x is a number, directly write VariableString(x) instead of ToString(Variable(x)), when i want to get the string of variable x.
  • when the value of my variable x is a string, directly write Variable(x) instead of ToNumber(VariableString(x)), when I want to get the number of variable x.

Is it a bad habit? What is the best practice?
Thank you.

(I ask especially because my previews take a very long time to open, before even loading the preview’s loading bar, and it might be because of formulas or code that are not optimal)

Edit: apologies if I misunderstood. If you are talking about the difference between you converting data instead of letting the app automatically convert from string to numbers. I prefer to set the conversion instead of hoping the app does it a certain way. That’s just me. I like structure and consistency that I can control. It’s easy enough to miss things like capitalization. It helps me follow things later.

I would keep numbers as number and strings as string. It’s more efficient and less prone for errors.

You can easily get “11” when adding “1”+“1” instead of 1+1=2. Keeping things separate just makes things easier to read.

1 Like

Hi! Thank you for your reply.

Yes! That’s what I meant.
Basically I enter in my program datas that are strings, and I then need to use some of these datas as numbers. So I have many of these string-to-number events.
So far I relied on the Gdevelop ability to directly convert the value to the type (number or string) asked.
But since I have long delay to start a preview, I wanted to know if there’s a real difference between relying on gdevelop, and use ToNumber and ToString functions.

If yes, I’ll have to rewrite many events.

IDK. But I believe you would need a significant amount of code, loops or large resources like images or sound to cause a significant noticeable delay. I would use the debugger to see what’s happening.

Even if one approach was quicker you would need to repeat it hundreds of times to notice a difference. Something is either looping, loading or waiting for something to happen.

1 Like

How? It’s just a collection of bytes that the engine interprets differently depending on the type.


You need to explicitly write VariableString(myNumber) + “1”. I can’t see how one can easily make a mistake with that.

@Bluzima, I’d use VariableString(…) and Variable(…). Because JavaScript is loosely typed, it’s perfectly fine to do it that way. This won’t be what’s causing the long loading time.

1 Like

Thanks a lot for your replies @Keith_1357 and @MrMen !

OK. Good to hear that, it reassures me about my project. I’ll look for the issue elsewhere then.

I deleted all sprites and music files from my project, and also old scenes that I don’t use anymore : it made a change, loading the preview fastest. But still 4 min loading…
About loops I don’t think they are the problem. Cause if I preview an empty scene, the delay is also long. So the delay is not about reading the code in frames, but before that, loading the preview.

If you have any idea or similar issue, another thread has been open about that long-loading preview issue :

1 Like