Number formatting
Hey yall,
So the game I'm working on is all about hyperscaling. So for instance in Level 1, you'd do 1 damage. In Level 10, you'd do 1Million. In level 20, it's be 1Trillion, etc.
If anyone has ever played Tap Titans, I really like how they dealt with crazy numbers, going from 999B -> 1T, 999T -> 1aa, 999aa -> 1bb, etc.
I'm relying on tables to store all of my data. Numerically, the data will behave normally, so 1 million is 1,000,000 etc. But I want it to display the numbers within their units, so 1 million would display as 1M.
How would I go about formatting the text and defining units in GameSalad? I'd rather not rely on using images, because I want it to be infinitely scalable.
Best Answers
-
tatiang Posts: 11,949
The TextSubStr() function will do exactly that. So TextSubStr(game.score,1,1) will return 5 from 5,000,000. Fortunately, text functions accept integer attributes and expression values.
For the table, I wasn't thinking of using any rules... just a constrain behavior. Something like Constrain game.units [text] to tableCellValue(tableName,ceil(log10(game.score)),1). And the table would look like:
[blank for ones]
[blank for tens]
[blank for hundreds]
[blank for thousands]
[blank for ten thousands]
[blank for hundred thousands]
M
[blank for tens of millions]
[blank for hundreds of millions]
B
[etc.]Maybe it could be simplified by dividing the log10() result by 3 since you're looking for multiples of a thousand. I haven't tried this all, I'm just thinking off the top of my head.
New to GameSalad? (FAQs) | Tutorials | Templates | Greenleaf Games | Educator & Certified GameSalad User
-
tatiang Posts: 11,949
Here's a demo. Click to roughly double the score. I didn't do any rounding so 1,385,000 would appear as 1.3 M.
New to GameSalad? (FAQs) | Tutorials | Templates | Greenleaf Games | Educator & Certified GameSalad User
Answers
It would be easy if your damage follows a simple equation because you could map the values to table rows but if it's arbitrary, I'm not sure. If it's level based, why not have the units in table rows and use the level # as the row number?
New to GameSalad? (FAQs) | Tutorials | Templates | Greenleaf Games | Educator & Certified GameSalad User
Well I could build a unit table that pulls something like (if x > 1,000,000,"M", if x > 1,000,000,000, "T") etc. But I'm still a little perplexed at how I can have the raw data use the actual full digit value but only display the significant digits, so i.e. store 5,000,000 but only pull the 5, and scalable to 5,000,000,000,000 also only pulling the 5.
In excel there is the Left() function which would solve it, I don't suppose there's a way to emulate that in GameSalad? It doesn't look like it's in the glossary. Or any other advanced number formatting, again in excel it would be "0,0K" or what have you
One correction: I tried using an integer attribute within textSubStr() and it gave me an invalid expression error. So I guess you'd have to constrain (or change) a text attribute to game.score and then use that text attribute inside of the text function.
New to GameSalad? (FAQs) | Tutorials | Templates | Greenleaf Games | Educator & Certified GameSalad User
Absolutely fantastic, thanks! I'll try the demo tonight but this already looks like a bullseye to me!