Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Time in Games

Discussion in 'General Discussion' started by BearweeStudios, Mar 1, 2015.

  1. BearweeStudios

    BearweeStudios

    Joined:
    May 22, 2013
    Posts:
    12
    Hi, I've been trying to figure this out for a while and can't find anything on it. I want to know how one makes Time based game objects like in Clash of Clans. Like for example it takes 2 hours to upgrade a certain item. My question is where is the time stored? When the player comes back to the game an Hour later how does the game know an hour went by?

    Another example Candy Crush when a timer counts down and like every 15min or so you get a heart. and if you comeback 30min later the player has 2 hearts. Where is the Time Stored?

    Thanks.
     
  2. carking1996

    carking1996

    Joined:
    Jun 15, 2010
    Posts:
    2,605
  3. RockoDyne

    RockoDyne

    Joined:
    Apr 10, 2014
    Posts:
    2,234
    Simply put, you schedule it. You say that this is going to be done at 4:30, instead of in three hours, so that it's easier to track using real time.
     
  4. BearweeStudios

    BearweeStudios

    Joined:
    May 22, 2013
    Posts:
    12
    Thanks guys, I thought about the saving of the time and then subtracting the current time, but was not sure. Scheduling the time seems good but what if days go by then I guess i'd have to get the date and subtract or add 24 hours for each day that had gone by..
     
  5. Samuel411

    Samuel411

    Joined:
    Dec 20, 2012
    Posts:
    646
    That will allow people to cheat by just changing their system time. The thread carking posted will help you.
     
    BearweeStudios likes this.
  6. RockoDyne

    RockoDyne

    Joined:
    Apr 10, 2014
    Posts:
    2,234
    So? If it's a single player game then whatever. If it's online, then the server should be doing most of the lifting anyway. Hell, it's just as easy this way to incorporate it into an in-game day/night cycle or other time mechanics. The important thing is to be thinking about time differently from the countdown that people normally see. If you start having a lot of things counting down, it's much more efficient to have a sorted list where you only need to check the first entry to see if it's time for it to be ready, then to have to iterate through the list and decrement each timer.

    Also, I'm pretty sure what carking posted will have the same issue. All it seems to do is grab system time and convert to UTC.
     
    Samuel411 likes this.