How to Manage In-Game Time [Unity Tutorial]

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi guys mike here from comp3 interactive welcome back to the channel last week i did a video on actions and events and at the end of that video i promised that i was going to show you some actual real life uses for that kind of system now we'll gloss over a few things again in this tutorial but if you haven't already watched it i suggest going watching that video and i'll pop a link up right about there so it'll give you more of an understanding of what we're doing here but in this video what we're going to do we're going to clone an in-game time system from a game like stage valley or animal crossing now if you've played those games before you'll know that they're not in real time it's usually about two or three seconds equates to an in-game minute for example and that's easy enough to do on its own but what we want to do we want to make this a little bit more scalable and what we want to do we want to make events that can be tapped into so we can perform actions based on the time it is in the game now that may sound a little bit confusing but when we get into it you'll see exactly what i mean so just before we get into it i just want to thank gigatank3000 for sponsoring this video i've got his links down in the description go give him a follow on twitter go check out his website keep up to date with what he's up to really great fella and i also want to thank everyone supporting me over on patreon so that's steve uk brandon zillan rath you guys are fantastic so let's take a quick look at the scene that i've got set up here it's just a very basic scene just a few sprites and we have a car and we also have a ui element to display in game time and if you're interested in the sprites that i've used for this they're made by mutual pixels and i'll link his itch page down in the description so let's have a think about what we want we want a back end system that's going to run automatically and update the in-game time not only that we want to be able to tap into that and trigger certain events at certain times so the example that i'm going to use is we're going to set an arbitrary time say half past 10 10 30 and every time it hits 10 30 we want this car to just drive on past so nothing too complicated you can scale this however you need it in your game this is just to show that we can trigger events based on this time so let's start by creating the time manager script so that's going to be time manager and we'll open this up in visual studio all right so we're going to need a few things in here first of all we're going to set up actions and like i said i went over actions in the previous video so make sure you go and check that out if you're not familiar with them so these are going to be public static actions and remember we need to be using a system namespace to gain access to the action type and we want two different actions in this we want to be able to tap into whether or not the minute or the hour has changed so i'm going to call one on minute changed and then i'm just going to copy this and i'm going to change it to on our changed so depending on what time we want our event to happen or how often we want our event to happen we can tap into either of these actions and we also want to keep a reference to what time it actually is and i'm going to use properties for this because i want to make sure that you can't edit the time outside of this time manager script so again these are going to be public static this time they're going to be int and we'll set the minute um to make it a property we'll give it the get accessor and we'll make it a private set so we can only ever change this inside of this class and again we want to copy that but this time we've got a reference to what error it is next we want to decide on how often we want the minute to change in game based on real time so the way that i'm going to do it in my game i'm going to make it so every half a second real time increments the minute by one so let's set that up so that'll be a private float minute to real time and we'll set that to half a second 0.5 and finally we need another private floor which is just going to be our localized timer now in our start method we can set minute and hour to whatever we want i'm just going to start mine at 10 o'clock so we don't have to wait that long before we can actually trigger that event at 10 30. so minute is going to equal zero hour is going to equal 10 and then to start off we'll set our timer equal to our minute to real time next we'll just go into our update now many of you have probably done timers before and this isn't really any different so we want to minus time dot delta time from a timer if the timer is less than or equal to 0 then that means our real time has elapsed and we want to increment the minute so in here we'll do minute plus plus we'll add one to the minute but then we need to check whether or not that's hit 60 if it's hit 60 we want to increment the hour and reset the minutes back to zero so now if minute is greater than or equal to 60 we'll increment the hour instead or as well set minute back to zero and then outside of this we want to make sure that we set our timer back to our minute to real time so now we have the timer running that's going to add one to the minute every half a second as soon as the minute hits 60 it's gonna reset that to zero and increment one onto the hour now that's all well and good that's going to work but we want to make sure that we trigger these actions in the relevant places so we have one minute changed and it's going to come to no surprise to you that we're going to do that when we increment the minute so all we need to do we need to use that sort of like a method but what we'll do we'll do on minute changed and we want to make sure that there's something listening to that action if it's not and we try and call it it's going to be null and we're going to get a null reference exception so we could wrap that in an if statement we could check whether or not if one minute changed is not equal to null and then we can call it but there's a shorthand where you're doing this and the way that we do that we can do one minute change question mark dot invoke so what that's going to do the question mark is basically the null check so if on minute changed is not equal to no so something is listening to that event we'll then invoke it and we can do exactly the same in the hour so that'll be on our changed and that should be a time manager class completed extremely simple but now this is extremely versatile and i'll show you exactly how we can utilize this so if i come back over here create a new c sharp script and i'm just going to call this time ui now if we open that up we want to tap in to our on minute and then on our changed actions and when that happens we want to update that on screen text to tell us the time so obviously the first thing we're going to need is a public text mess pro ugui and that's going to be our time text and we're going to need to import text mesh pro here and then we want to subscribe to those time manager actions in our on enable method but if you remember from the last video we also want to unsubscribe if we ever disable this object so we'll just put in an on disable for now and then finally we need the method that we want to fire whenever the minute or the hour changes so that's going to be update time now really simple all we're going to do is we're going to set time text.text equal to and then we'll just do a bit of string interpolation here so the dollar sign and then speech marks and we want that text to read our time manager dot minute call on time manager that's the wrong way around time manager.hour and then time manager.minute and just to make this look a little bit nicer we want to make sure that we keep that lead in zero when we're less than 10 on the hour and the minutes so to do that you could actually do it in two different ways this is a little handy tip for you so usually you'd do dot two string and then you'd pass in the mask which would be that now that would work perfectly fine every time it was say five seconds it would read zero five but again there's a quicker way of doing this and a nicer way of doing it in my opinion if we just remove that two string we put a call on after minute and then put double zero that's basically the same we wanna mask this string with this format and we can do that as well on the error all right so now we're updating the time let's subscribe to those actions so we want to set time manager dot on minute changed and we'll add with the plus equals operator our update time method and we also want to do that for uh time manager dot on hour changed as well so we'll update the time when the hour changes and then really simply on disable we'll change that plus equals to a minus equals so we'll unsubscribe this method from this action if a time ui game object is ever disabled so let's go and hook that time ui script up we'll select our time block over here which is just the uh you can't really see it because it's pretty much the same color as the ambient editor color but i have a gray box with the time underneath it so we'll attach our time ui script in and drag in our time text over here and that should be if i was to play this game it's going to subscribe to that event it's going to automatically set it to 10 o'clock and then every half a second we're going to tick over one minute at a time so let's try that and it didn't work why didn't it work it didn't work because i don't have the time manager in the scene it's a mono behavior this needs to be in the scene as well so we're just going to create an empty object call it time manager and we'll drag that on that's all we need to do no it should work and it does every half a second we go up by one minute of in-game time so that's all well and good but now let's work on the car so we're going to need a script for this car call it car surprisingly enough and open it up in visual studio now we aren't going to need the start method in this but we do need our on enable and on disable methods and then we want a private void time check method in here so what we're going to do we're going to subscribe like we did before this time check method to on minute changed and on our changed actually we don't need on our changed because we're doing it as 10 30 so if the hour changed it can never actually be 10 30. so we can leave that one out so we'll set our time manager dot on minute changed and we'll subscribe time check to that method and then again remember to unsubscribe it and now in our time check what we're going to do we're going to check if time manager dot hour is equal to 10 and time manager dot minute is equal to 30 and some of you may have noticed a little bit of a cut there as i was actually recording this i realized a better way of doing it so currently i haven't changed anything apart from i've removed the update method from this car because we're going to use a core routine to move the car instead so let's go ahead and create that so that's going to be a private ie numerator move car and we want to start that core routine during the time check so we'll do start call routine move cut and then i'm just going to really quickly run through a rudimentary movement script that has nothing to do with this tutorial so there we go i've done a really quick movement script for my car it's probably not the best but that's not what this tutorial is all about so basically what's going to happen is every time a on minute change gets triggered we're going to run time check now if during that check our hour is 10 and our minute is 10 i've changed it from 30 to 10 just so we don't have to wait as long then we're going to start this movement routine to move the car from the left side of the screen to the right side now if we jump back over we see i've already attached the car script to our car game object and now if we hit play we should see the car is going to move across the screen at 10 minutes past 10 so right about no and it does brilliant and that's going to loop every time it hits 10 10. but what about if we want to change that what if we want to run this car past every hour well what we can do we can change this on minute change to on our changed and again remember to change that and this time we'll do it every time the hour changes so we can just get rid of that time check and now every time the hour changes we're gonna get a car drive past so let's run this and we'll see what happens so we don't get it on the first call the 10 o'clock call because we're just setting it to that then whenever it hits 11 o'clock we're gonna hit the car moving along now what i may do i may speed this bit up because it's going to be quite boring and i imagine i'm going to run out of things to say before we at the 12 o'clock just to prove that it does it every hour so we're coming up to this 11 o'clock now let's have a look we should be moving across and there he goes and there it is so you can use this for any kind of events that you like you could do a day and night cycle with this so as soon as it hits like 8 p.m start the sun setting the moon rising change everything to the nighttime sprites and everything works well the way that sturgeon valley uses this is each of the npcs are actually on a timer so on a monday they'll always do the same things for example monday 10 o'clock one of the npcs will go to the shop they'll wait there until 12 o'clock and then after that it'll trigger a new event where they'll go and do some fishing for example and this is the perfect way that you can implement that kind of system into your games you can tailor this however you like subscribe any scripts to the on minute or on our change depending on what you need and then you can do a validation check if it's the right time if it is fire off that event so i hope that's been useful for you if you liked it remember to like comment subscribe all that standard youtube stuff you know what to do but with that i'll see you again next week thanks for watching guys if you like the content remember to subscribe to the channel for weekly unity tutorials
Info
Channel: Comp-3 Interactive
Views: 5,080
Rating: undefined out of 5
Keywords: comp3, comp3interactive, unity, unity5, unity3d, unity2d, tutorial, game, development, dev, introduction, program, programming, code, coding, csharp, c sharp, c#, develop, 3d, 2d, artist, programmer, editor, extension, multiplayer, how to make a game, how to make games, game development, how to use unity, brackeys, comp-3 interactive, unity 2d, unity 3d, how to make, game developer, game dev, make money with games, in game time, stardew valley, time, time manager, events, delegates, actions, event listener
Id: Y_AOfPupWhU
Channel Id: undefined
Length: 16min 11sec (971 seconds)
Published: Fri Mar 26 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.