Convert milliseconds to...

Get help using Construct 2

Post » Tue Jul 24, 2012 5:01 pm

I have a timer set up which displays minutes:seconds:milliseconds. Each kind is its own variable. It's displaying it as a text box, and so I can not use it to compare best times, etc.

I have a formula which converts all the variables into milliseconds, but am wondering if there's a way to split them back up into minutes, seconds and milliseconds.

Or if there's a simple way to provide times and best times. It's turning into a real mind-bender.
@bearboxmedia
www.bearboxmedia.com

Nintendo Wii U Developer using Construct 2
B
68
S
8
G
6
Posts: 929
Reputation: 9,356

Post » Tue Jul 24, 2012 5:16 pm

If you have the time in milliseconds:

t: time in milliseconds

milliseconds: t % 1000
seconds: int(t / 1000) % 60
minutes: int(t / 60000) % 60

So your display would be:
(int(t / 60000) % 60) & ":" & (int(t / 1000) % 60) & ":" & (t % 1000)
B
79
S
24
G
52
Posts: 4,724
Reputation: 39,711

Post » Wed Jul 25, 2012 1:51 pm

Hi R0j0hound,

Thank you for your reply. I'm not able to get that working I'm afraid. I currently have the code:

Add dt to Timer
Set minutes to floor(timer/60)
Set seconds to floor(timer)%60
Set milliseconds to timer%1000

Set TimeScore text to:
right("00"&minutes,2)&" : "&right("00"&seconds,2)&" : "&left(right("000"&float(milliseconds),3),2)

I then convert these to milliseconds using:

CurrentTimeVar = Milliseconds+(seconds/1000)+(Minutes/60000)

However, this displays the milliseconds as 0.0000 and 1 second shows at 1.0000.

The code you have given me shows the timer very strangely. When it reaches 61 seconds, the other sections of the timer does not change, and it happily counts up to over 100 without the other seconds being added:

E.G: 0:0:101.56789

At the moment it's being used for a best time, so I'm having to store the min, sec, and mil data. It would be a lot better if I just saved the mil and converted it to 00:00:00.

Kind regards.
@bearboxmedia
www.bearboxmedia.com

Nintendo Wii U Developer using Construct 2
B
68
S
8
G
6
Posts: 929
Reputation: 9,356

Post » Wed Jul 25, 2012 3:10 pm

R0J0hound's example has the time t in milliseconds. Your timer is in seconds so the milliseconds would be:
floor(timer * 1000) % 1000

minutesSecondsMilliseconds.capx (r99)
B
54
S
28
G
18
Posts: 1,520
Reputation: 24,990

Post » Wed Jul 25, 2012 9:00 pm

:( That's one of the problems with beta... I can't open the example files from stable.

Thanks for your help! I've tried to recreate what you've described, but it doesn't seem to be working, so I've installed r99 on my laptop.

Never heard of zeropad, but this is giving exactly the same result that I currently have but with 3 milliseconds being shown rather than just 2. Judging from this, if I wanted to store the time as a best time, I would need to save all 3 things like I'm doing now (Mins/Secs/Mills)...

Say for example I just have a random number of milliseconds. 12345678. Is there a formula which will allow me to convert this into minutes, seconds and milliseconds. This way, I only need to save the milliseconds for each level.

My level works perfectly now, but it's untidy and I forsee it being hard to manage. It's around 24 events long, but I think I can get it lower than 10 with this other method.
@bearboxmedia
www.bearboxmedia.com

Nintendo Wii U Developer using Construct 2
B
68
S
8
G
6
Posts: 929
Reputation: 9,356

Post » Wed Jul 25, 2012 9:02 pm

[QUOTE=AnD4D]Say for example I just have a random number of milliseconds. 12345678. Is there a formula which will allow me to convert this into minutes, seconds and milliseconds. This way, I only need to save the milliseconds for each level.[/QUOTE]

[QUOTE=R0J0hound]If you have the time in milliseconds:

t: time in milliseconds

milliseconds: t % 1000
seconds: int(t / 1000) % 60
minutes: int(t / 60000) % 60
[/QUOTE]
B
27
S
8
G
8
Posts: 903
Reputation: 8,452

Post » Wed Jul 25, 2012 9:19 pm

[QUOTE=AnD4D] Hi R0j0hound,

Thank you for your reply. I'm not able to get that working I'm afraid.

The code you have given me shows the timer very strangely. When it reaches 61 seconds, the other sections of the timer does not change, and it happily counts up to over 100 without the other seconds being added:

E.G: 0:0:101.56789[/QUOTE]

Do you know what I'm missing? At the moment I have:

Add dt to timer
Set t to timer%1000
Set text to (int(t / 60000) % 60) & ":" & (int(t / 1000) % 60) & ":" & (t % 1000)

This gives me the strange series of numbers.
@bearboxmedia
www.bearboxmedia.com

Nintendo Wii U Developer using Construct 2
B
68
S
8
G
6
Posts: 929
Reputation: 9,356

Post » Wed Jul 25, 2012 9:36 pm

Milliseconds is seconds*1000, so your timer that counts seconds needs to be multiplied by 1000, not modulo 1000.

Set t to timer*1000 (this)

Set t to timer%1000 (not this)
B
27
S
8
G
8
Posts: 903
Reputation: 8,452

Post » Thu Jul 26, 2012 12:24 am

Ahh, that seems to be a lot closer. I'm trying to have it display as 00:00:00, and have tweaked the code enough to show the first two sections correctly, but I'm falling down at the millisecond section which is either showing the full range of numbers, which is daft looking, or the right length but not showing the first 3 digits of the milliseconds:

right("00"&(int(milliseconds / 60000) % 60),2) & ":" & right("00"&(int(milliseconds / 1000) % 60),2) & ":" & left(right("000"&(milliseconds % 1000),3),3)
@bearboxmedia
www.bearboxmedia.com

Nintendo Wii U Developer using Construct 2
B
68
S
8
G
6
Posts: 929
Reputation: 9,356

Post » Thu Jul 26, 2012 12:37 am

Did you check out ramones' example? To me it sounds like it does everything you want except store the total time elapsed in milliseconds, is that correct?

If it is correct, just make another variable "t" like you have in your case, and instead of the "Set text to timer*1000" action in ramones' example, do "Set t to timer*1000". Now the "t" variable will hold the total amount of time elapsed in milliseconds, and the text will show the time in "min:sec:mill" with 2, 2 and 3 numbers respectively.
B
27
S
8
G
8
Posts: 903
Reputation: 8,452

Next

Return to How do I....?

Who is online

Users browsing this forum: Draconinho, totoe and 9 guests