Time Remaining: String Formatting

Get help using Construct 2

Post » Wed Jan 18, 2012 1:29 am

I made a little counter for time remaining to level ends in my game. I can't upload it now, but the example consists only in:

1. Add a Text on layout;
2. Add a global variable like "secondsToPlay" and set a number of seconds, for example 150 for 2 minutes and a half, or 120 to 2 minutes on Event Sheet;
3. Add Event: System > Every 1 second(s);
- 3.1. Add Action > Text > "Time Remaining: " & Floor(secondsToPlay/60) & ":" & Floor(secondsToPlay%60)
- 3.2. Add Action > System > Subtract 1 from secondsToPlay;

The counter will start: 2:0 - 1:59 - 1:58 - ... - 1:9 - n

The problem is: As you can see, I don't know how I can put a zero left on numbers to have this visual representation: 02:00 - 01:59 - 01:58 - ... - 01:09 - n.

In programming languages, for example, we can format the outputed string like: print "Missiles: %02" % numberOfMissiles and obtain a plain text like: "Missiles: 003".

From now on, the question is obvious: "How can I do this with Construct 2?", how can I format my string? Or... if anyone has a better way to do this...

Thanks in advance.hexodin2012-01-18 01:39:48
B
9
S
3
G
5
Posts: 2
Reputation: 3,206

Post » Wed Jan 18, 2012 1:43 am

time<10 ?"0"&time :time
Probably a few other ways as well.
Image ImageImage
B
167
S
50
G
159
Posts: 8,168
Reputation: 102,932

Post » Wed Jan 18, 2012 2:02 am

C2 has the ZeroPad function? If so, use ZeroPad(secondsToPlay,2)
B
83
S
22
G
15
Posts: 1,039
Reputation: 15,219

Post » Wed Jan 18, 2012 3:16 am

well
[code]right("00"&seconds,2)[/code]
works fine (:Yann2012-01-18 03:18:26
B
64
S
22
G
14
Posts: 1,482
Reputation: 16,457

Post » Wed Jan 18, 2012 10:50 am

Thank you, about the ZeroPad function, returns a error on r75: "Unknow Expression 'ZeroPad'", but the "right" function works well.hexodin2012-01-18 11:00:40
B
9
S
3
G
5
Posts: 2
Reputation: 3,206

Post » Fri May 05, 2017 5:45 pm

Very good information for the formatting of time. You just do not know this automatically. I used it in an educational game where students have to set an analog clock and it syncs and displays in a digital clock and it works perfectly.
B
7
S
1
Posts: 86
Reputation: 603


Return to How do I....?

Who is online

Users browsing this forum: db3344, Exabot [Bot], grigrizljac, Hex27, istero and 3 guests