Hey yall,

So I've been playing with Construct 2. I have had a game idea in mind for a while, but have ZERO coding experience. Luckily, it seems that I've found the perfect engine here!

One of the issues I had was trying to figure out how to display exponential units for my game, i.e. to have it read "5 million" instead of "5,000,000", etc. It turned out to be pretty easy, but since I haven't found this explicitly shared I figured I'd make my first contribution to the Tutorials section here.

First of all I used Rex's CSV tool to build the array for the unit names, but you can build it yourself using a standard array table. I wanted it to be extremely pedantic so I grabbed all the official number units from Wikipedia going up to 10^303 (centillion), but obviously that may not be necessary for your purposes.

I used simple text boxes to display the unit (one for using the unit name, and the other to show the 10^x format), an entry box to test out how different numbers look, and I used a global value "Number" which will become your score, HP, damage, what have you.

Here is the event sheet, the algorithms being the only real thing I did:

So what's going on here? First and foremost, we're using the CSV plugin to build a table array called "Unit" with all the unit names, and using the At(Column,Row) function from the plugin.

If you open the example .capx and type "512345678" into it, you'll see:

The Longform text box displays **512.35 million**

The Shortform text shows **5.12 10^8**

At this point you should be good to go, but to delve a little into why this works:

**For the Longform:**

- So first let's talk about pulling from the table. I had set the row names to the log multiple of 3 associated with the unit. So i.e. thousand is 3 (10^3), million is 6 (10^6), etc. To calculate which unit we need, I took the Log10() of the Number (a global variable set to the entry box remember), rounded down using floor(), divided that by 3, then used floor() again. Then I look up the unit name on the table using the Unit.At() expression, referring to the Column named "Unit" and putting the above calculation as a string using str() into the Row.

- The associated number is easier to figure... sort of. We want to show up to 999.99 before we roll over to the next unit. We're going to divide the Number by the appropriate power of 10 to get that result. What power should that be? Well it's the log10() of the number, then the floor() of that, divided by 3, floor() again, then times 3. And then use that as an exponent of 10. In other words: **(10^(3*floor(floor(log10(Number))/3))** Finally to get the 2 decimal places we're going to multiply our result by 100, round() it, then divide by 100 again.

**For the Shortform:**

- I also wanted a more compact look that shows the 10-power instead of the unit name. This one doesn't require the CSV table at all! The only complication is I don't want it showing 10^0 for single numbers, so I added a ? condition to check if it's a single digit, cutting out the " 10^x" section if it is.

- The calculation is similar to the above, except that we only want to go up to 9.99 before rolling to the next power of 10, rather than going in sets of 3 powers. So the exponent number is **"10^"&floor(log10(Number))** and the displayed number is **round(100*Number/(10^(floor(floor(log10(Number))))))/100**

## 0 Comments