Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Time system - leading zero

I'm trying to make a good data/hour system to my game, but numbers don't show leading zeros, so the hour shows like 2:5h, when I need 02:05h. How can I solve it? An insane block of ifs to check if is 0, them set to 00 (and if is 1, set 01, etc), can do the job, but it doesn't seems the right way.

The timestamp if formed by vars: $second, $minute, $hour, so using live, each second sets second var to +1. One way maybe is something like:

(if: $second is in (a:0,1,2,3,4,5,6,7,8,9))[ (set: $second to "0"+$second) ]

Of course it doesn't work (plus sign tries to sum string 0 with the var. But that's the idea...

Comments

  • Ok, found a way. I was trying to modify the seconds when generated, but I can "cheat" the clock display:
    $hour:$minute:(if: $second is in (a:0,1,2,3,4,5,6,7,8,9))[0]$second
    
  • edited April 2016
    In your example you would need to use the (text: ) macro to convert the number to a String so you can append it to the "0".
    (set: $second to "0" + (text: $second))
    

    One common method used when building an in-game time tracking system is to only track the smallest unit you are interested in and to derive the larger units as you need them.
    The total number of hours that have passed in game time.
    eg. 2 days + 5 hours => (2 x 24) + 5 => 53
    (set: $gameHours to 53)
    
    ''Generate the values to be outputed''
    
    Calculate number of remaining hours in gameHours.
    (set: $hours to $gameHours % 24)
    
    Calculate the number of days in gameHours
    (set: $days to ($gameHours - $hours) / 24)
    
    Output the current days/hours in DD:HHh format.
    (print: ("0" + (text: $days)).slice(-2) + ":" + ("0" + (text: $hours)).slice(-2) + "h")
    
    note: the above uses a the Javascript String slice function instead of the Harlowe (substring: ) macro because the macro needs an end position parameter which requires us to determine the length of the temporary $days/$hours strings.

    Because you may want to display the DD:HHh output multiple times or in more than one place you should place the relevant code in a child passage and then (display:) that passage where you need it.

    1. Place the following in a new passage named game-time
    {
    (set: $hours to $gameHours % 24)
    (set: $days to ($gameHours - $hours) / 24)
    (print: ("0" + (text: $days)).slice(-2) + ":" + ("0" + (text: $hours)).slice(-2) + "h")
    }
    
    2. Examples of using the new game-time passage
    The total number of hours that have passed in game time.
    eg. 2 days + 5 hours => (2 x 24) + 5 => 53
    (set: $gameHours to 53)
    
    Output the current days/hours in DD:HHh format.
    (display: "game-time")
    
    Add 14 hours to game time
    {(set: $gameHours to it + 14)
    (display: "game-time")}
    
    Add 6 hours to game time, note the day change.
    {(set: $gameHours += 6)
    (display: "game-time")}
    
    Add 3 days to game time
    {(set: $gameHours += (3 * 24))
    (display: "game-time")}
    

    The above could be modified to track game seconds instead.
  • Wow, great answer. I have achieved what I need in another way, but this one looks better, I'll make tests later. Thanks!
  • As record, I'll let my own solution here, maybe can be useful for someone:
    (live: 1s) [
      (set: $csecond to it + 1)
    
      (if: $csecond > 59)[
        (set: $cminute to it + 1)(set: $csecond to 0)]
    
      (if: $cminute > 59)[
        (set: $chour to it + 1)(set: $cminute to 0)]
    
      (if: $chour > 23)[
        (set: $chour to 0)(set:$cday to it + 1)]
    
      $cday days, 
      (if: $chour is in (a:0,1,2,3,4,5,6,7,8,9))[0]$chour:
      (if: $cminute is in (a:0,1,2,3,4,5,6,7,8,9))[0]$cminute 
      (if: $chour is in (a:0,1,2,3,4,5,6,7,8,9,10,11))[AM](else:)[PM]
    ]
    

    So, I have the same clock (1 second to 1 second in-game), but I can control it anyway I need, for example taking the hour from the var $chour, or increasing time on each passage with (set: $cminute to it +X).

    I'm not sure if this method has any major negative impact.
Sign In or Register to comment.