Element Text Set To 0 Equals Empty

Hi all,

This is a simple issue with a simple fix, but I’m confused as to why it’s happening.

If you set a text element’s text to 0 via:

this.entity.element.text = 0;

the text element sets its text to "" and prints nothing.

The simple fix would be to change it to

this.entity.element.text = "" + 0;

but it’s strange that this is occurring in the first place.

My guess, which might be completely off, is that internally there’s a check of the same kind as 0 == '' which evaluates to true thanks to js’s strange rules.

If you want it to print 0, you would put the quotes around the 0. 0 on its own is registered as null, and thus it won’t show anything (this happens because texts register strings only and not numbers, it’ll register 0 as null because 0 means nothing or null). If you want it to print 0, you need to have it in quotations for it to register the 0 as a string.


You can use toString method to print 0. You can change your line to

this.entity.element.text = (0).toString();
1 Like

Hi @TingleyWJ,

That happens because the engine internally will convert all values passed to the text property to string:

So yes, you should take care of passing only string values to this property to avoid getting unexpected behaviour.


I met the issue in my leaderboards at some point, when I noticed some players had no score. As you said, @TingleyWJ, it was easy to fix, so I started to explicitly convert my scores to string before assigning to the element property.

@Leonidas pointed to the sources of the engine, where the check happens. As you assumed, it is due to the way JS evaluates zeros:

var val = 0;
if (val) {} // false

However, I do agree, that this is not intuitive. If I am able to do:

this.entity.element.text = 7;

Then I should be able to assign a zero as well. You can consider creating an issue in the engine repo.