For your games, you could have to manage a score greater than 32768 (I like games were I can score millions), and often, you want the score displayed in a fixed length, prepended with zeros (00072635).
But managing this can consume lot of tokens.
I came accros a solution I'm quite proud (I'm still learning Lua...) and wanted to share.
In Lua, if you do : "the score is "..score , score is converted into a string and then concatenated to the first one.
So to convert a number into a string, you just have to write : ''"..score
I wrote a recursive function that takes a string and a number (the length) and returns a string padded with zeros at its beginning to reach the desired length :
function pad(string,length) if (#string==length) return string return "0"..pad(string, length-1) end score=723 print(pad(""..score,7)) -- will display 0000723 |
Elegant and thrifty, isn't it ?
Then, you can use two variables, score2 will store the 10 thousands, score1 anything under 10000
each time your player scores something, you add it to score1 and you test if it's above 9999 :
if score1>9999 then score1-=9999 score2+=1 end |
Be sure the max score won't never be above 327689999 ... or manage it with a score3 variable
Here is the concept in action - score2 is displayed in orange, score1 in red:


Dear... I didn't. And I'm confused. Would have been more productive if I had posted it there. Sorry
And now, I find my solution not as good as I thought:-(
[Please log in to post a comment]