Hello! I have here a function that formats scores from this:
3459
to this:
0003459
To use just add this code:
whateveriwanttocallthefunction = require("score")
The function has 2 parameters the total score and how many digits are in the final string.
EXAMPLE:
score(1456,6) would return the string "001456"
Edit:
I modified this so that it uses string.format. 3 params now! The first 2 are the same as before, the last one is optional and is maxscore.
2 return values the string of score, and the score itself.
Happy coding
-Sheepman
Arcade style score function
- sheepmanbuddy
- Prole
- Posts: 7
- Joined: Sun Sep 08, 2013 5:31 pm
Arcade style score function
Last edited by sheepmanbuddy on Sun Jan 26, 2014 8:43 pm, edited 1 time in total.
- DaedalusYoung
- Party member
- Posts: 413
- Joined: Sun Jul 14, 2013 8:04 pm
Re: Arcade style score function
You can do this with string.sub("00000" .. score, -6), although of course you do need to keep score under maxscore.
- bartbes
- Sex machine
- Posts: 4946
- Joined: Fri Aug 29, 2008 10:35 am
- Location: The Netherlands
- Contact:
Re: Arcade style score function
How about string.format("%07d", 1456.6)?
- sheepmanbuddy
- Prole
- Posts: 7
- Joined: Sun Sep 08, 2013 5:31 pm
Re: Arcade style score function
I didn't know you could do that with string.format! I feel stupid...
Edit: Next time I need to do something similar to this, I will just play around with string.format.
Edit: Next time I need to do something similar to this, I will just play around with string.format.
Last edited by sheepmanbuddy on Sat Jan 25, 2014 4:09 pm, edited 1 time in total.
Re: Arcade style score function
Don't feel stupid, you can do things in many ways and you might be able to use this method for something else .sheepmanbuddy wrote:I didn't know you could do that with string.format! I feel stupid...
- Jasoco
- Inner party member
- Posts: 3726
- Joined: Mon Jun 22, 2009 9:35 am
- Location: Pennsylvania, USA
- Contact:
Re: Arcade style score function
How about if you want to put commas in?
Returns a number formatted like 1,000,000.
I made one that formats a number to hh:mm:ss but I'm not sure if it's optimal enough:
Code: Select all
function formatNumber(number)
return (string.format("%d", number):reverse():gsub( "(%d%d%d)" , "%1," ):reverse():gsub("^(-?),","%1"))
end
I made one that formats a number to hh:mm:ss but I'm not sure if it's optimal enough:
Code: Select all
function formatTime(t, m)
local r = math.floor(math.mod(t/60/60,60)) .. ":" .. string.sub("0" .. math.floor(math.mod(t/60,60)),-2) .. ":" .. string.sub("0" .. math.floor(math.mod(t,60)),-2)
if m then r = r .. (t - math.floor(t)) end
return r
end
- DaedalusYoung
- Party member
- Posts: 413
- Joined: Sun Jul 14, 2013 8:04 pm
Re: Arcade style score function
I'm sure you can do that with os.date or os.time too
-
- Prole
- Posts: 4
- Joined: Sat Jan 25, 2014 9:02 pm
Re: Arcade style score function
Looks like a good find. I will download this to try.
Who is online
Users browsing this forum: Ahrefs [Bot], Semrush [Bot] and 3 guests