Formatting game time

I have a simple question on how to format the time into minutes:seconds. And I cannot find any solutions how I did it.

This is how I created the room and added Room Properties

        Hashtable roomOptions = new Hashtable();
        roomOptions[PropertiesKey.TimeRoomKey] = gameTime;


        string[] roomProperties = new string[propsCount];
        roomProperties[0] = PropertiesKey.TimeRoomKey;


        PhotonNetwork.CreateRoom(null, new RoomOptions()
        {
            MaxPlayers = selectedGameMode.MaxPlayers,
            CustomRoomProperties = roomOptions,
            CleanupCacheOnLeave = false,
            CustomRoomPropertiesForLobby = roomProperties
        }, null);

And this is how I tried to reach the game properties from my game manager script which is in another scene

GameTime.text = PhotonNetwork.CurrentRoom.CustomProperties[PropertiesKey.TimeRoomKey].ToString();

The thing is I'm just unsure how I format the time, so far the time looks like 300, but I want it to look like 5:00. Any suggestions? Thanks in advance.

Best Answer

  • tleylan
    tleylan ✭✭
    Answer ✓

    Hi Johan. Assuming the value 300 is minutes the following should work.

          	Int32 gameTime = PhotonNetwork.CurrentRoom.CustomProperties[PropertiesKey.TimeRoomKey];
            GameTime.text = TimeSpan.FromMinutes(gameTime).ToString(@"hh\:mm");
    

Answers

  • tleylan
    tleylan ✭✭
    Answer ✓

    Hi Johan. Assuming the value 300 is minutes the following should work.

          	Int32 gameTime = PhotonNetwork.CurrentRoom.CustomProperties[PropertiesKey.TimeRoomKey];
            GameTime.text = TimeSpan.FromMinutes(gameTime).ToString(@"hh\:mm");
    
  • I noticed as well that you could be using the .Net Hashtable rather than the Photon library. I have no idea if that matters but I thought I would point it out. And in the spirit of less is more you can compress the code a bit (and not have to declare the size of roomProperties. The following should work.

    var roomOptions = new ExitGames.Client.Photon.Hashtable() { { PropertiesKey.TimeRoomKey, gameTime } };
    
    string[] roomProperties = { PropertiesKey.TimeRoomKey };
    
  • Hey, thanks for your reply. It is actually 300 seconds, 300 minutes would be a bit to long for a round. but i'll try that out. Thanks for helping out

  • I'm using the photon library by the way, just clarified. And I actually have a lot more room props, which I didn't show above, just pasted the necessary snippet of code

  • Thanks a lot tleylan!! This did just great

    Int32 gameTime = (int)PhotonNetwork.CurrentRoom.CustomProperties[PropertiesKey.TimeRoomKey];
            GameTime.text = TimeSpan.FromSeconds(gameTime).ToString(@"mm\:ss");
    

    If you don't mind, I have another question, how would you go about making a round timer, like I know how to make an offline one, but I'm not sure how to make an online one, I do have some idea about it but any clarification would help, cheers!

  • At least I didn't guess 300 hours :-) I use the .Net datatype naming rather than the C# ones and missed changing Int32 back. You might give it a try to see if you like it better they show up nicely in the editor with syntax highlighting.

    Personally (not that you asked) I'd wrap the logic into a method of a class, possibly named CurrentRoom. It makes it easy to reference in other places (like a tests suite) and easy to find/edit if the logic should ever change. GameTime.text = CurrentRoom.TimeRoomKeyFormatted;

    By "online" I think you mean a timer that is shared by all the players? I don't honestly know but implementing shared "stuff" correctly is important and a timer even more so. I saw something in the docs

    Synchronized Timestamp

    Whenever a client connects to a Photon Server, it will synchronize a lag-rectified timestamp. This can be used in a room (as all players are connected to the same Game Server then) to synchronize the timing of events.

    I don't know if that is helpful.

  • At least I didn't guess 300 hours 

    :)

    I use the .Net datatype naming rather than the C# ones and missed changing Int32 back. You might give it a try to see if you like it better they show up nicely in the editor with syntax highlighting.

    Could you abbreviate on that, I'm a bit unsure what you mean.

    Personally (not that you asked) I'd wrap the logic into a method of a class, possibly named CurrentRoom. It makes it easy to reference in other places (like a tests suite) and easy to find/edit if the logic should ever change. GameTime.text = CurrentRoom.TimeRoomKeyFormatted;

    Thanks for the suggestion, but I am only really calling it twice so it doesn't really matter I guess, but I'll try something out. Im just unsure why something like this doesnt work?

        private void Start()
        {
            gameTime = (int)PhotonNetwork.CurrentRoom.CustomProperties[PropertiesKey.TimeRoomKey];
            GameTime.text = TimeSpan.FromSeconds(gameTime).ToString(@"mm\:ss");
        }
    
    
        public void Update()
        {
            gameTime -= (int)Time.deltaTime;
            GameTime.text = TimeSpan.FromSeconds(gameTime).ToString(@"mm\:ss");
        }
    
  • Happy to... I don't know whether it is common or not (I will guess it isn't) but I always use the .Net datatypes in my C# code.

    Int32 gameTime
    
    // where typically one would see
    
    int gameTime
    

    String, Int32, Int64, Boolean, etc.

    Visual Studio and Visual Studio Code do a great job of syntax highlighting and I prefer it this way.

    I will suggest that wrapping "less than obvious" code is a good habit to adopt whether you use it twice or 10 times. You can unit test functionality by calling a method. You can't easily test when the code is part of some longer piece of code.

    As for why "this doesn't work" you need to define "doesn't work". Note that Time.deltaTime is a float so your cast may be creating an illusion. I would assign (and log) the deltaTime do the math on gameTime and log that. Always verify your assumptions, sometimes values are not what you believe them to be.