GDevelop icon indicating copy to clipboard operation
GDevelop copied to clipboard

GlobalVariableString() is returning '0' when a String variable is empty

Open danired opened this issue 2 years ago • 2 comments

Hi all,

I think I found a bug when using GlobalVariableString(variableName) function.

image

image

When the value of the String Variable is empty, the Preview renders a '0'.

This is because this line in Runtime/variable.ts:

} else if (this._type === 'string') {
  this._str = '' + varData.value || '0';
}

If varData.value is empty, then this results as '0':

this._str = '' + '' || '0'  -->
this._str = '0'

image

In the same Runtime/variable.ts, you can see that the method setString(newValue: string): void doesn't use these || '0' at the end, so I think it should be equals both the constructor and the setString().

setString(newValue: string): void {
  this._type = 'string';
  this._str = '' + newValue;
}

I attach 2 videos Before and After the fix.

Please, check if I have missed something important. Thanks in advance!

Before the fix

https://user-images.githubusercontent.com/21989259/213698045-604a55ad-7d68-42b3-8d52-d3ee80bf6c79.mp4

After the fix

https://user-images.githubusercontent.com/21989259/213698074-ddf61a8e-7d83-4dcb-924f-c99a06235d83.mp4

danired avatar Jan 20 '23 12:01 danired

🤔 We need to be very careful with this, since "0" has been GDevelop's default value for string variables for years, and many events sheets rely on this assumption to know whether or not a string variable is defined. Here, this may be fine since this only applies to per-declared strings, and we may want a string defined as "" to be indeed empty... Although, that still remains a breaking change of a core feature of GDevelop. Let's handle this with the utmost care.

arthuro555 avatar Jan 25 '23 05:01 arthuro555

This came back up again from a few users (and myself) as unexpected behavior. https://github.com/4ian/GDevelop/issues/7267

@arthuro555 with the variable feclaration changes, and this behavior being different from how dynamically created structure/array children strings are handled, is this something that can be updated more easily now?

Silver-Streak avatar Jan 10 '25 06:01 Silver-Streak