Log In  

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)

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

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:

Cart #22779 | 2016-06-12 | Code ▽ | Embed ▽ | License: CC4-BY-NC-SA

P#22780 2016-06-12 18:49 ( Edited 2016-06-13 00:11)

P#22785 2016-06-12 19:34 ( Edited 2016-06-12 23:34)

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:-(

P#22792 2016-06-12 19:49 ( Edited 2016-06-12 23:49)

If it works for you, it's more than good enough :)

But I think we can all learn from each other

P#22797 2016-06-12 20:11 ( Edited 2016-06-13 00:11)

[Please log in to post a comment]

Follow Lexaloffle:          
Generated 2023-09-26 22:36:04 | 0.016s | Q:13