0 votes
by (420 points)

So in my game I have a lot of values that are on a sliding scale from 0-100, with different breakpoints on these scales that denote different tiers in that attribute.

For example...

<<set $dex = 22>>
<<set $dexTier = 0>>
<<set $dexDesc = "">>

<<if $dex > 99>>
 <<set $dexTier to 5>>
 <<set $dexTier to "Lightspeed">>
<<elseif $dex > 75>>
 <<set $dexTier to 4>>
 <<set $dexTier to "Super Sonic">>
<<elseif $dex > 50>>
 <<set $dexTier to 3>>
 <<set $dexTier to "Pretty Fast">>
<<elseif $dex > 25>>
 <<set $dexTier to 2>>
 <<set $dexTier to "Kinda Slow">>
<<elseif $dex > 1>>
 <<set $dexTier to 1>>
 <<set $dexTier to "Sloth">>
<<elseif $dex <= 0>>
 <<set $dexTier to 0>>
 <<set $dexTier to "Standing Still">>
<</if>>

I can't help but think there HAS to be a better way to do this! Prefereably, I'd have the descriptions in an array with a way to parse the marker from math, but my math skills are... questioneable.

Any insight here?

1 Answer

0 votes
by (44.7k points)

This should do the trick:

<<if $dex > 99>>
	<<set $dexTier = 5>>
	<<set $dexDesc = "Lightspeed">>
<<elseif $dex <= 0>>
	<<set $dexTier = 0>>
	<<set $dexDesc = "Standing Still">>
<<else>>
	<<set $dexTier = Math.floor(($dex - 1) / 25) + 1>>
	<<set $dexDesc = ["Sloth", "Kinda Slow", "Pretty Fast", "Supersonic"][$dexTier - 1]>>
<</if>>

The Math.floor() method removes anything after the decimal place, so that should turn any numbers from 1 to 99 into 1 through 4.  Then it uses that tier to pull the correct description string from the array.

That also fixes the problems in your code above where you weren't testing for $dex == 1, where you were setting $dexTier instead of $dexDesc, and corrects "Super Sonic" to "Supersonic".

Hope that helps!  :-)

...