Timed Chest ( And a timer )- Unity 3D[Tutorial][C#]

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey what is it guys welcome back to one of these Friday Saturday video we don't really have dates on those anymore but we just do them when we want to and it just happened that I was testing out my live stream a few days ago just playing around the settings just live streaming a little bit to some of my friends actually and some of you guys just tuned in and came and asked question which I didn't answer to and one of them was really interesting the person wanted to know how exactly to create some kind of chess mechanic that you open it and then you gave a timer a real timer not liking that you know like a 5 seconds delay on an ability but like an actual timer that persists through game session so went ahead and I showed him out exactly I've done it for my game right here if you have a look we have this High Noon thing which is basically just a chess that you roll when I click on this it rolls this value it gives me some goal and now I just looted a good amount of goal that's pretty good it's saving to the player breath now if you have another way to save yeah and you can actually use that and we also have like the place in code where you can input what you're gonna be doing so are you going to roll for gold are you going to give the player some kind of special reward and guys I'm gonna stop talking now we're gonna jump right into it alright so we'll be looking at the chess example now I've already got one working my game I've got some exemple to look at during the video as well but let me just show you what I do have in my real game so it's basically just something you click on and then every 12 hour you receive a free roll you click on the button and then we do something like that so I'm basically just showing a timer I'm receiving your reward if I exit I come back I'm still on the same exact timer as you can tell so this is what we're gonna be doing now obviously we're not going to be looking at the example I have in this game because it's tied up in so many different ways so we're gonna go ahead and just start one from scratch and we're gonna try to put everything into the same object this way you guys can actually have your own standalone chest object so that's what we are going to do the first thing I did is actually create just in using so that's something really really fresh there's nothing in here there's only the main camera and yeah so let's start from that I will go ahead and just create a new UI and a button so it is a really simple button the button is going to be opening the chest or something of the sort now just rearrange your canvas the way you want it I'll just put a quick image on that I do not have an image waiting yeah I do have image sorry about that so we're going to be putting I don't know maybe the first one at the top okay and that's what is going to trigger my chest now let's rename this for something like chess chest button whatever and of course as always you just place it the way you want I'm just gonna be doing something really simple but in the center make it wide 300 by 500 no that's definitely not good looking okay that's my chest button it's big enough right so the button object is uh what we're going to be using as the base of our object so that's where our script is going to be located let's go ahead and just create that chest object and there is few things we need let's get started right away so there's a few things we're going to be needing for this and there's ah if I Jin if you can just remember correctly we're going to need a function that we call when we click on the button so private void sorry public void it has to be public we're calling it from a button and let's just call it something like just click so whenever we're clicking on the button on whatever we're opening the chest this doesn't have to be button by the way if you have something else triggering that chest um just go ahead and just do what we're going to be doing in that function so just click and when we click on that chest what we should be doing is save the timestamp actually now how we going to be saving the timestamp we're going to be using a date/time from system I believe so let's go and just try to type that in date time the date time and this is part of using system so just make sure that you're using system now date/time dot now and we're going to be saving the amount of tix and I think we'll be saving that as the you you long actually the other could be quite good now let me just show you what this gives us in terms of data so I'll just put a two string on that just to test it out and we're going to be linking our function to that very button okay I'll convert you long to a string okay so let's just put it on a normal take value right now so back on my button adding it on click event I just drag and drop this object actually the button object and we'll find the chest click let's put this scene and have a look at the console down here this is information you get now this is really hard to read if you're if you're like me and you're like a human and such what this is is actually the amount of thick's of your timer right now so if you have a look down here at the bottom right of my screen it's like 11 2 in the morning right now um and we do have like the current data as well this is what it represents this actually represent your time your real time your computer time basically so this is always going to be different it always has been different it's a really huge number but that's what it does so we just represent your system your system time so what we're going to be doing is we're going to be keeping track of that inside of our player breath or any other save structure you have so let me just go ahead and do a player pref we're gonna save a should we save it in let me just go ahead and just we're gonna save a a string so set string actually and we'll save this as a string just like that now remember that the first argument is always the key so I got to give it something here last chess open or last chess click so technically what should happen at this time is whenever we click on the chess we save that inside of our last just open key and this way the big number we saw a little bit earlier is going to be represented in our register okay so let's keep going now what we're going to do is we're going to disable that button whenever we um we're going to disable that button whenever we click on it so let's add the unity engine UI up here and create a private button this is going to be this button or chest button so whenever our chest button whatever we click on it then we disable because we don't want to be able to click on it again since we're gonna have like a timer going on and just to get a reference to that I'll do a private voice stop you could keep your your button up public and just link it manually in the inspector but I'd like to just have the least amount of public stuff going on so I'll just get it manually here in a start so chest button is equal to get component of type button now remember if you're doing it the exact same way as I am right now your script has to be on the same object as the button so these two component they have to be standing one next to each other well they don't really have to be like next to each other but they have to be on the same object so we're getting that and then once we click on the chest we're going to one line below hit and we'll do chest button interactable is equal to false this way if we have a look at what's going on we start the game we click here and it's disabled at the same time inside of the regedit if I can type it right let's go have a look at our game right now our game is this one already had it open so that's my flippy game last chest open and that's the value that's the amount of ticks on the very right hand side now the left side on the right side that is value we are looking at so we know that this is save now what we're going to do is actually make sure that we re enable it after clicking on it say we're going to put like a delay 3 seconds delay which is something quite small then we'll bump it up like 12 hours later if you want but we're going to start with like 3 seconds delay so we can actually test it out to do this we need to keep track of that in a update what I'd like to do is actually position my update right next to my start so I just cut that and put it here let me zoom out a little bit and here is what we are going to do let's just check is our current is our current button disabled so if chest button interactable we can get it this way or we can also do the is interactable i think this one works so if is interactable that means we can do something now what we're going to do is we're going to put the exclamation mark in front of it so if it's not interactable then we're going to look at the time and check ok is right now the good moment to be interactable and a little bit later on we'll also add a a timer text because because we like doing things right so we also add the timer to let the user know how long he has to wait now here's the way we do it so we are going to do we're going to get a long time stamp and we're going to do is equal to this string and to get this string we've got to do a player prep dot get string and we're getting the string last chess open which is the exact same thing we put down there so now that we have the amount of ticks we had and now this is obviously not going to work because we're trying to make a a laundry you long we're trying to make a you long out of a string so to have this work we're going to have to do a ulong dot bar so we have to parse this string and then this way we can actually get a ulong value out of this string so once that this is this is done we can we kept tracked off on the previous time at we say if you could actually be putting that in say a field up here so private you long last chess open and that's actually better if we do it this way so let's just open is equal to the thing we've got from the the player pref okay and let's also set it down here now I'm doing it in a weird order but that's just how it is so let's just open at this point is going to be daytime dot now the ticks and I'll move it up one line and let's actually just swap that okay and this one is long okay so now let's just cast this as a you long okay just to recap really quickly we have this last chest open which we check at the start so if we do have a value if we if we open the chest before one time before then we do have a value in that that's like the amount of ticks which we saw earlier something with a really really big number we know that from the very beginning and then chest click when we click on the chest when you reset that last chest open to that very value to the right now value and then we set it back on the player breath and now finally we said V chest to interactable false now what we're going to need to do at this point is take the last chest open and actually just get difference in between now and that last just open value the way we're going to do it is by creating a you long diff which is just 4 stands for difference and we're going to do right now so date time dot now the ticks - time stamp or not times 9 but last chest open and just like that we're going to be casting that as we're going to be guessing this as a you'll own the date and this way we have the difference so if we do a debug log diff we go in the game press play and we click on the button this is the difference in ticks that we can see so as we wait longer the text amount gets higher alright so now we do have a difference in between the last time we press and right now not what we need to do actually convert that into MSO milliseconds and there is something quite easy you can do using the date/time I think using a timespan actually from the C shop is you are going to create a you long that I just call em for now and we'll do diff divided by timespan dot fix per milli second and this is going to give you the actual amount in milliseconds so debug that log let's try that again M and it should now be a lot more readable so that's the difference in milliseconds so 8,000 is equal to one second right now we click on this button like 10 seconds ago 12 seconds ago and using that we can now know when to toggle it back on and now let's actually swap this around a little bit I'll create a float that I'll call seconds left and what's going to happen this time is we're going to be taking the number we're going to be looking at so say we're waiting three seconds then we're going to be taking three thousand because 3,000 M s is equal to three seconds we're going to be taking that number and then reduce it by M so the value we're going to add at the very beginning when we click on the button is going to be three thousand and then as time pass by that number is going to go down until it reaches zero at the moment in rich 0 it means that we're ready to just reopen the chest or you know do an action there so we're swapping this around let me just go ahead and do that so float second left is equal to three thousand minus M and then since we want this in seconds for formatting purpose when we have the timer will just do a divided by 8,000 this way we have the amount of seconds and like I said which is three debug log seconds left I like to just check out everything line by line so like I said right now when we press on the button it should start at three thousand and then just go down or sorry not 3,000 but three because we virtus in seconds now once we're below that we actually mess up our whole thing because we're dividing by something that is negative so so let's just keep going and let's do the simple one if seconds left is smaller than zero that means we're pretty much it's done here and if that's the case we're going to say our button what's our button name chest button dot interactable is equal to true and we'll also do a return here and you'll see why I do the return because we plan on I'm not keeping that function going afterward so we press on the button we click here let me just put that back up click here wait three seconds and then it should go back up to our triggers sense and for some reason it does not so what exactly is going on looks like second left never go below zero which is kind of impossible if you ask me okay for some reason I think that this equation right here is being cast as the end because those are two ends and then it divided by another end so for some reason it does something not cool here obviously so what I'll do is just put a float in front of that and hopefully we cast these two number as float or if you want to be sure you can just put a dot 0f here and I'll do the same thing there so having this completed I'll just go ahead and pull a this and then hopefully we do not get the same error so we wait one two three seconds and it's back up we can play that again three seconds and it comes back okay so we do get like the basic behavior going on not what we're going to do is actually start cleaning up things a little bit and making this more enjoyable to look at so this check over here this check is something we're going to be putting in another function just to help us out a little bit we'll go down here and we'll create a private private ball is chess ready or something like that and phone is just messing up sorry about that is chess ready now this is going to return a boolean and all we're going to be doing is we're going to take all of that put it in here and let's just make sure everything is fine so we do check the difference now if it's below if it's below zero instead of doing just button interactable we're simply going to return through else we return false and you'll see why we do this in a second because I plan on using this somewhere else to make this more readable and also more easy to interact with so let's go back in our update so if the chest is not interactable then we ask okay is the chest ready if it is let's go ahead and just do chest button dot interactable is equal to true so we just moved our function a little bit now the reason I wanted to do this is because I also want to test this out in the start in case we're carrying over from a previous session so if it's just ready actually if it's just is not ready so if it's not ready let's go ahead and do chest button that interactable is equal to false here now here is what is going to happen we are going to need to define somewhere what exactly what's the amount of time that we should wait now I know you guys are if you want to create chests like this you're not going to be only taking like three seconds okay so next thing to make this more pleasant to actually work with is we're going to be creating a public field that is going to allow you to edit the amount of time you want to wait for so public float let's do ms2 weight and by default we'll set it on that's five seconds that's not really let's just put it on like five seconds for now and then we'll move on like I said to bigger numbers like 12 hours indie is chess ready we're going to be replacing the 3000 by ms2 weight it's also make sure this is a float so we don't get the same problem and just like that we've created a chess that should actually work in between session let's test this out in between session like I said so if we quickly click on this we exit it has not been five second yet and as you can tell we are now back so we click on this one two three four five and it's back so it's really waiting the real amount of time and that's even working in between sessions so that's fairly cool right but when it's actually offline we'd like to have some kind of display we'd like to have some kind of timer on it that would be really cool let's go ahead and just create that right away on the same button that we've created at the very beginning we have this text component now let's I mean if you don't have it you can just right-click create a new one and um we're going to be turning this into a timer so I'll just call this chess timer and let's bump up the font size a little bit why not let's do it test here so twelve hour let's go for twenty minutes and thirty seconds something like that we can just bump this just for looking at the size of it that's why I'm inputting some stub values maybe change the fun so it fits with the rest of my game okay so definitely something like that that would be quite cool now what we're going to be doing is go back on our chests declare a new private we'll do it a private because like I said I just rather have a lot of things private and that is going to be the chest timer now like I said if you want to be putting it public then you can do that and just link it via the inspector if you decide on putting it private like I do just make sure it's on the same object and you know what it's not actually on the same object my button is up here my chest script is there but then the chest timer is one children below that so it's right here so if you want to be putting it public put it public you're gonna have a field you can drag and drop your your text into the field here but since I like keeping things private I'll just go fetch it manually in the way I do this is I'll do chest timer is equal to let's do a get component in children of type X and this way I should have your reference to that right away okay now back in our update and earlier I said we're going to be having like your return button here and that's I really meant it so let's just go put it put it back up here so if the chest is ready we put the chest on interactable that's true and we also call the return value and since we here the second it gets back to interactable let's do chest time or dot X and just put it on blank so we're not going to be seeing the text when it's it's ready or you can be saying something like ready like I said it's up to you and the really important thing here is that if it's not interactable then we make it interactive all we do return because the rest here is going to be setting the timer so set the timer now let's actually go and try to find how much time we have in between right now and before the the actual chess gets reopened and now some of the values that we calculated in just Reddy's are going to be reused here so we kind of wrapped it in the function but some of that element could be reused actually all of that I'll just copy it over for now I know that that's not really optimal but for readability purpose for reading my whole code I think this would be better but of course in term of performance I'd rather just keep the values that I've conquered up here okay so let's do a you longed if I need the difference that's cool the seconds left I also want that and now this is a fun part where we just go ahead and we create our timer our timer is going to be our right now the text in our timer we're just gonna put that on R so R is going to equal zero at the very beginning then what we're going to do is say we're going to tackle the hours first so R is plus equal week ass int on seconds left divided by three thousand and three thousand six hundred if we have 3600 seconds at least that amount that we're going to have at least one hour then let's do a two string and we can append that with a character like the H character that would be cool or let's do a string H and n it's spacebar and there we go we have the amount of hours just like that now for two minutes let's do minutes and then just below that I like to do seconds left - equal then we're going to be looking do we have at least one hour so we do the same equation we did up here if we do have a lot at least one hour let's get and remove that by doing eight times an hour okay so right now we're removing all the hours from our string and we can move on to minutes as far as minute goes it's very simple we do the R plus equal again cast this seconds left as int and we do divide by 60 this time we'll do a two string and what we can do here is if we all at least two digits we'll do a two string and we'll put two digits in here this way we can actually have a look at two numbers even though we're only on a one one digit second so I'll just spend that by EM and let's do seconds seconds is even more easier we do seconds left modulo 60 and we just take the remain of that so to string again two digits in that plus s now I know this is a little bit complicated but I have another tutorial on timer if this is something that butter is you and here we go we have our timer text just like that we can do our actually we can do timer tax or chess timer that X is equal to R and that is the text we need that's what we need to actually have a timer rolling we're going to go ahead and just step this out hopefully everything works in a single go that would be so cool so right now we're playing the game the text is not change and it's because the chess is not interactable when we start the game and what I feel like doing is taking this chess timer text and actually just putting it and the is just ready call so if chess is ready we're going to be saying chess time where the text is equal to ready then we're going to do a return true now this way we should actually change the text as soon as a game start so it says ready we click on it we wait five seconds and it's back okay that's fairly cool now if we're going to be doing something like 12-hour it's really easy to calculate all you're going to be doing since you're in a mess all you really have to do is just go ahead and just type in okay so you've got 60 seconds in a minute then you've got 60 minutes in an hour and since you're in ms then you're gonna have to do times a thousand at one point so that would be equal to one hour right here this big number is three million I don't I don't really speak English well that number right here is a single hour so if you want to do that like times twelve that's 12-hour right there so the number we've get is this and can we just base yep and as you can tell we're now on a 12-hour cooldown now obviously you're gonna have a different action depending on what you're exactly doing with your game so when you're clicking on the chest this is where you would put the action so down here is where you would give gold to your player or this is where you would roll a dice to know which reward he gets something like that this is actually where you do it I've also using this mechanic in the past for let me just show you really quickly in the real game I've use it to keep track of my timer for ads I do have a function in my game if I just show you down here you can watch an advertisement to gain five gold now I don't want my user to be able to spam that so what I do is we click here we play an ad and then the same exact mechanic goes on however I'm not displaying the amount of hours in my timer and you guys I hope you enjoyed this video I hope this helped you and if it did please leave me a like on the video I really appreciate that as always and please leave comments in the comment section below if you do have comments check out the patreon page if you wish that would be very very cool and other than that please subscribe I'll see you in the next video
Info
Channel: N3K EN
Views: 41,508
Rating: undefined out of 5
Keywords: Unity, Unity tutorial, How to make games, Unity game engine, Mobile game, Mobile, iOS, Android, tutorial, c#, coding lesson, unity tutorial, n3k en, n3ken, n3k unity, en, unity, mobile tutorial, chest tutorial, unity 3d chest tutorial, unity 3d timer, timer tutorial, time tutorial, unity timer tutorial, unity timer, unity timer c#, unity countdown timer, countdown, time lapse, unity3d time lapse, unity 2d, unity 3d, unity 3d tutorial, unity 5 tutorial, unity 5 time, unity 6 time
Id: Yoh6owRXCXA
Channel Id: undefined
Length: 29min 31sec (1771 seconds)
Published: Sat Aug 20 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.