diff --git a/Core/GDCore/Project/Variable.cpp b/Core/GDCore/Project/Variable.cpp index 2f06d240be74..4b2b99af57b9 100644 --- a/Core/GDCore/Project/Variable.cpp +++ b/Core/GDCore/Project/Variable.cpp @@ -306,7 +306,7 @@ void Variable::UnserializeFrom(const SerializerElement& element) { if (IsPrimitive(type)) { if (type == Type::String) { - SetString(element.GetStringAttribute("value", "0", "Value")); + SetString(element.GetStringAttribute("value", "", "Value")); } else if (type == Type::Number) { SetValue(element.GetDoubleAttribute("value", 0.0, "Value")); } else if (type == Type::Boolean) { diff --git a/GDJS/Runtime/variable.ts b/GDJS/Runtime/variable.ts index 68d58e15bd82..ae1bb2597d41 100644 --- a/GDJS/Runtime/variable.ts +++ b/GDJS/Runtime/variable.ts @@ -54,7 +54,7 @@ namespace gdjs { // Protect against NaN. if (this._value !== this._value) this._value = 0; } else if (this._type === 'string') { - this._str = '' + varData.value || '0'; + this._str = varData.value !== undefined ? '' + varData.value : ''; } else if (this._type === 'boolean') { this._bool = !!varData.value; } else if (this._type === 'structure') { diff --git a/newIDE/app/src/VariablesList/VariablesList.js b/newIDE/app/src/VariablesList/VariablesList.js index 9de67b2d88fc..5de1bfb445a4 100644 --- a/newIDE/app/src/VariablesList/VariablesList.js +++ b/newIDE/app/src/VariablesList/VariablesList.js @@ -1885,7 +1885,16 @@ const VariablesList: React.ComponentType<{ props.variablesContainer ); if (!variable) return; + const oldType = variable.getType(); variable.castTo(newType); + // When changing type to String, reset to an empty string. + if (newType === 'string' && oldType === gd.Variable.Number) { + variable.setString(''); + } + // When changing type to Number, reset to 0. + if (newType === 'number' && oldType === gd.Variable.String) { + variable.setValue(0); + } _onChange(); forceUpdate(); },