Jump to content
  • Advertisement

Recommended Posts

Posted (edited)

The development team (my cousin) is currently at the point where we want to implement an in-game time system which will also obviously act as a foundation for our day/night cycle.

We are  wondering if any of you guys has done something like this before and would be willing to share some code, or if you would know of something on github. We haven't been able to find anything online and decided to ask on here before we go hack it out ourselves.
 

Any suggestions on how to do this would also help.

For a better description of what we want, it's a time system similar to stardew valley or harvest moon with minute/hour/day cycle.

 

Thanks for the help 🙃

Edited by SIr Pep
More!

Share this post


Link to post
Share on other sites
Advertisement
Posted (edited)

A simple game-time system is written very easy! We had a way more complex one in our Medival City Building game that also controlled the sun-states. Start with a long integer (UInt64 in C#) at zero; this is your initial time (like the beginning of the year you want to start at, maybe 1761 for example) so your initial time is the time you choose to start at (1761) + the year value of your integer.

You then add a value to that integer in your update loop that determines how fast your time increases. We have had a day-night cycle of 24 hours in 20 minutes per day.

Then you could calculate your time by some simple formatting

secondsPerDay = (24 * 60 * 60)
days = seconds / secondsPerDay
seconds %= secondsPerDay

second = (seconds % 60)
minute = ((seconds % 3600) / 60)
hour = (seconds / 3600)

year = <your starting year>
month = 0;

while (days >= GetDaysOfYear(year)) //<-- if you want to respect leap years this is <yourDaysPerYear> + (!((year) % 4) && (((year) % 100) || !((year) % 400)));
do
    days -= daysOfCurrentYear
    year++
end
while (days >= GetDaysOfMonth(year, month)) //<-- grab the current month's day count from a table in your calendar; for example day[0] (January) 31 and maybe add a day if you are in a leap year
do
    days -= daysOfCurrentMonth
    month++
end
day = (days + 1) //<-- do this to start at 01.01 not 01.00

 

Edited by Shaarigan

Share this post


Link to post
Share on other sites

There is also no reason why game world times need to match Earth and human times.

If all you need is day/night you might decide that 50000 time steps equals one day in your game, then divide the current time to get the portion of the day. Maybe you decide that time % 50000 uses 0 as dawn, 12500 for noon, 25000 for dusk, and 37500 for midnight. 

Or you can stick with real world times.

Don't go with the system's clock and system time, since those have many difficult situations. Humans adjusting the time and date, automatic time corrections from time servers, adjustments for time zones, daylight saving, discontinuities due to system sleeping/hibernating, lots of situations happen when using real world time.

Share this post


Link to post
Share on other sites
1 hour ago, frob said:

Don't go with the system's clock and system time, since those have many difficult situations. Humans adjusting the time and date, automatic time corrections from time servers, adjustments for time zones, daylight saving, discontinuities due to system sleeping/hibernating, lots of situations happen when using real world time.

Yeah that's the first thing I read... We want a time that seperate from anything in real life, and that goes in 17 minute cycles... We haven't had time to utilize the system @Shaarigan shared with us yet, so I want to wait till we do that before I post more questions or comments :)

Share this post


Link to post
Share on other sites
Posted (edited)
On 3/29/2019 at 11:09 PM, SIr Pep said:

and that goes in 17 minute cycles

If you want 17 Minutes per day just add 1 to the seconds on every real-time second and use (17 * 60) for the secondsPerDay constant. This will divide your seconds through a 17 minute cycle as required. This will add a full day every 1020 seconds in real-time.

Just as a tipp: If you want to have nights go faster (as we wanted to in our game) just double the time you add to the seconds variable in every update so nights will run faster ;)

Edited by Shaarigan

Share this post


Link to post
Share on other sites

It's going to be an RPG. So the point is that you go to sleep when it gets dark 😏 Thanks for the help :) I wanted to ask you some more questions, but the programmer wants to figure the rest out himself so he can learn and know how to fool around with it in future. I'll post a clip here with his code if I think about it :)

 

Share this post


Link to post
Share on other sites

I highly recommend using a simple integer to keep track of the number of seconds since the epoch of your game, like the UNIX timestamp. All you need to do is to increment/decrement this number by however you want. Only do the conversion to actual year/month/day when you need to display them to players.
 

gameTime = 0; // the beginning of time in your game

gameTime += 10000; // fast forward 10000 seconds later

gameTime -= 10000; // travel backward 10000 seconds before

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!