What Time Is It?

This entry is part 10 of 13 in the series Game Development

Previously On IT Ninja . . .

So we’ve got a bunch of places you can visit. We’ve got the beginnings of a working quest system. But there’s another foundation upon which this game will stand. Time! Or rather, an in-game time system (and clock to go along with it). The “story” of the game is based on the passage of time, so this will be a system that is widely used.

And Now On IT Ninja . . .

The Beginning of Time

I figure if I’m going to be messing with time, I should probably broaden my knowledge on how time works in Unity. So that means checking out the manual and watching a simple tutorial. After brushing up a bit, it seems really simple to track the amount of time the game is played in seconds. The best part is the timer pauses itself already when I call the pause function. Using timeScale to pause allows me to stop the clock so time doesn’t pass in-game while you’re in menus.

Not only that but I’m hooking the in-game time into the singleton I have setup to control saving and loading, so it’s everywhere with a simple change. I’ve even got the game setup to save and load the timer. Really all I did was declare a variable in a few places and add a single line to the update function! There is only little problem though, the main menu screen. The timer is still running there. I can fix that by adding an empty game object with a script that sets timeScale to zero.

Oops, no that doesn’t work. It seems that just carries the scale of time into the game, which totally breaks everything because of the way I have my pause controls setup. In reality though, the problem is only a problem on new games. When loading a game, it will load the time, so that’s not a big deal. So I can just simply set the value of the variable back to zero just as a new game starts.

What Day Is It?

So, uh, we have a variable that tells us how many seconds it’s been since we started playing the game. How in the hell do I convert that to minutes, hours, days, months, and years? I guess the first thing I need to do is scale time. I have to figure out a multiplier I can use to scale time to my game. Basically, one in-game day takes 20 real life minutes. There’s 86,400 seconds in a day, but I want that to actually be 1200. . .

I have to think about this math. Maybe I should create a new class to deal with this too while I’m at it. Not only will I need to do that, I’ll also have to use something called a Modulo operation? Can’t say it’s a concept I’m too familiar with, but it seems like it might not be too hard to understand. The logic and math of trying to figure this out is confusing me.

Time’s Up!

And it’s done, I think. . . Originally, I planned on creating a new script to keep track of time stuff but I couldn’t get it to work right. So I incorporated the functions into the GameControl and I think it’s right. At the very least the Hour/Minute function is working as intended. Basically one IRL second equates to one in-game minute. That makes a day in the game last twenty-four minutes in real life.

I have yet to test the day/week/month/year functions fully. But I know I have more work to do in them anyway, so they’ll get tested (and fixed) when I’m back to working on those things. At least now I can have things happen depending on time of day. I’ve also incorporated this into an actual working clock in the game world. I think I will still have work on that though.

Next Time On IT Ninja . . .

With a working in-game clock and time system I can expand on the questing system using it. I have realized that my implementation of the questing system is a little messed up and could use an upgrade. So I think I’m going to go back to the quest functionality and make it better!

Series Navigation<< Questing and DialogQuesting Improved >>

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.