Day-Night Cycle | Farming RPG Tutorial: GMS2 [P12]

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys so today we're going to start setting up our day and night cycle and once again although this is part of the farming RPG series everything we do for this system will work on its own we're going to be in creating an entirely new object so you should be able to use this for different projects of course we'll be adding in some functionality regarding the farming side of things like getting our crops to become a day older everyday and grow when appropriate and we're going to be doing this day cycle in two parts today we're basically just going to set up the time and a few visual cues to help us track the time and we'll also get our crops to grow at the end of the day and in the next video we'll be implementing a kind of basic lighting system that will go with the day/night cycle so we can have the light cycling through different colors and different opacities depending on the time so let's get started and the very first thing we're going to do is set up a new object cold day cycle and this is going to be one of our special system objects that's going to be persistent and it's going to be handling everything to do with the day/night cycle so let's just come into our initializing room before we forget and add the day cycle to it okay now let's come into its create event and set up our system so I'm gonna have seconds minutes and hours and we don't really need minutes and hours but it's just gonna make tracking the time easier on ourselves so that we don't have to keep converting back and forward between seconds and hours and I'm gonna set up a day and we're gonna just start with the taping one and I'm gonna have seasons as well you might want to put months and I'm also gonna have you so we're starting on day 1 season 1 and year 1 and basically I'm gonna have a system similar to stardew valley basically because it's just very simple so I'm gonna have 30 days in each season and there's gonna be four seasons in each year so depending on how you have set up your game you might change these around a little bit once we get to coding so next I'm gonna have what I'm gonna call the time increment and this is gonna be the number of game seconds per step or frame and what that means is we could make our clock exactly like real life and to do that we would have to be saying with every frame of the game and remember because there's 60 frames in a second we want it to be incrementing a fraction of a second each frame and that exact fraction would be 1 divided by 60 so that 60 frames times 1 over 60 will get us our one second so we would put 1 over 60 if we wanted to do that but obviously that would make our clock incredibly slow and especially while we're just testing we want a much higher number than that so I'm actually gonna put a thousand and we'll tweak this later for getting it right for the game field but we'll just keep it as this for now next what I'm gonna have is darkness and basically what we're gonna be doing to indicate how dark is is we're basically just gonna be drawing a rectangle over the screen and that rectangle is gonna have an opacity between 0 & 1 and that just corresponds to its alpha value which is of course it's between 0 & 1 so if we are at 0 alpha that means we are completely transparent and invisible and if our alpha is 1 we are fully opaque so our darkness is going to be rotating between 0 & 1 as the time goes on for this video just so that we can have a visual cue of the time I'm just going to make the darkness 4 from zero to one starting at the very start of the day at 12 a.m. and then going on to again 12 a.m. at night the next day next I'm gonna have the light color and I'm gonna set that to black for now and it's gonna be the color of the rectangle and we'll be playing around with this in the next video but we'll just set it up like that for now finally there's a couple ways we could be drawing our rectangle so we could draw it in the drawer event and we could just draw it over the view itself we don't need to be drawing it over the entire room because that's kind of a waste of drawing and resources all we need to do is draw it over the view so the location of that rectangle would have to be relative to the view another way we could do it is just use the draw a GUI event because the graphical user interface will cover the entire screen anyway so if we just draw the rectangle over that that will fill up the entire screen as well so to do that when you need to be getting just how big GUI is so I'm gonna have GUI width and we're gonna get it so display get GUI width and we'll do the same for the height okay and we'll come into the step event and actually get everything incrementing so first off we're gonna increment the time and so we just have seconds going up in whatever time increment right so it's incrementing whatever we set here every single step or frame of the game and now let's just make our minutes and hours dependent on whatever the seconds is and we have to think how many seconds are in a minute 60 right so let's say we're currently on 120 seconds how do we get the number of minutes from that we just divide by 60 right so we just go seconds over 60 and the same for hours right because there is 60 minutes in an hour all right so that should take care of our variables now what we're gonna do here is change the value of the darkness so I'm gonna say darkness and remember how I said I wanted it to be changing between zero and one and basically just getting darker and darker as the day goes on so I'm just gonna have our darkness equal the number of hours over 24 so that way at the very start let's say we're at 1 a.m. it's gonna be 1 over 24 so that's gonna be quite a low opacity so it's not gonna be very dark and as we get closer and closer to 24 we will get closer and closer to 1 so it's gonna make a darker okay the next thing we do I'm just gonna call this a cycle check and this what this is gonna do is basically check once we get to 24 hours we have to reset the clock back to zero and then increment the number of days so this is how we're gonna get our calendar moving up all the time so we're gonna go if ours is larger than or equal to 24 because I want it to reset as soon as it hits 24 hours right as soon as it hits 12 o'clock at night so once that happens I want to reset seconds to 0 and I don't have to reset minutes or hours because they're already dependent on seconds here so once that kids 0 these will also be hitting sir but what I do want to do is increment the day and just so you know I'm using greater than or equal to because our time increment might not be a nice thing number that lets us hit exactly 24 it might shoot over 24 and be 24 point 1 or something so this will just make sure that once it is over 24:00 it will reset the clock but of course that is not enough because we also want to be checking if our number days hits 30 so that we can have a new season and then again once we've hit four seasons we want to increment the year so let's go if day is larger than 30 and I'm saying larger than 30 because I want it to have a day 30 right I want it to start at one day one and go and have the day 30 and only when it hits 31 do I want it to reset so once it hits a day that's greater than 30 we're gonna reset day 2 1 and increment the season and exactly the same logic here if season it's greater than 4 then reset the season to be 1 and increment the year and we're not gonna have a cycle check for the year because we just want to have that as infinite obviously you might want to alter this to suit your game but I just want to keep it as simple as possible all right so that is all of our setup the next thing we're going to do is just draw some things so that we can visualize all of these changes so firstly let's just draw our light well actually it's going to be the darkness more like so like I said it will be a rectangle and I'm gonna have this as a certain color and that color is going to be whatever this variable here is so for its position in the room we're going to start at the top left corner of the draw GUI so that will be 0 0 and then X 2 and Y 2 that will be the bottom right of the rectangle that's going to be the GUI width and GUI height and for the color so that is going to be whatever our light color is and just so I don't have to type it out four times I'm just gonna have a variable C 4 color equal to out light color and all right si si si si here and finally so for the outline this is asking us do you want me to draw the rectangle as an outline or do you want it completely filled in so we do not want it to be an outline we want to fill in so we put false now we can't change the Alpha with this function but we can set the Alpha of the entire drawing so we're gonna draw sets alpha and I'm gonna set this to our darkness value that is going to be determining how transparent or opaque out darkness Larry's and of course whenever you alter anything about the door event you have to remember to change it back because I don't want to change the drawing of anything that comes after this so draw set alpha back to 1 and you don't have to do this but I'm going to do this just so that you can see the what all of our variables are doing so I'm gonna draw text color all of the variables and I'm just going to draw them all yellow let's say and if you're wondering why I put string here that's because when we draw a text I'm gonna use the function like this it is asking for a string to draw I remember a string is something like this which has these quotations here that is going to be a string but our second variable is a number so we have to convert that to a string and I'm just going to do this for all of the other variables as well and of course just changing where we're drawing it the y-value here and all of our variables okay and let's run the game and have a look so there we go we're getting the seconds incrementing the minutes and the hours and the number of days is going up so obviously this isn't how light actually works but this is just a very simple visual cue for us we'll change this in the next video so that's good enough for now so what we're gonna do next is make our crops grow once we hit 24 hours so right here we're also going to be telling the crops objects to go ahead and make all of our crops older and the way we're gonna do this is I'm gonna tell the crops to run an event so if we come here and that event is gonna we could use an alarm but just for something different I'm going to come in two other and then user events and we can set up our own event here and I'm just gonna hit user event one and we'll do all that in here so remember in our step event when we're tapping G we were making the crops grow older we can use this exact code right here obviously without the G press to make our crops grow so we'll just get rid of this but that all should be fine but this is only gonna make sense if we're in the farmer but what if we were for example in the house and we have the clock hit 24 hours then then there won't be any crops for it to increment but I do want the crops in the world to be getting older even if we're not in the farm room and to do that we're gonna have to be altering our crops data grid that we set up so remember whenever a room ends we were saving all of our crops to our crops data grid here and we're saving the number of days old in column number 4 which of course is column 3 because we start at 0 so what we're gonna do is basically so in the case that if the room that we're currently in is equal to the farm room then we will do it this way so I'm just going to tab the server but otherwise if we're not in the farm room we will be looping through our grid and incrementing the number of days old of all the crops saved in there so remember in this grid every single Row is a new crop so we do need to check though that there is actually something in the grid there might be no crop set so we first have to check if the grid is empty and if you recall what we're doing if the number of crops was zero is we're just clearing all the spaces in the grid to be minus one so we just need to check if that cell of the grid which is just cell zero zero if that is not equal to negative one which it will be if there are no crops there so if it's not minus one we know there is something in there we're not sure exactly how many yet so we will need to get how many crops are in the grid and we can do that just by getting the height of the grid right the number of verse because however many rows there are we know each entry is going to be a new crop so we just need to get the heights so H for height equals to the dears grid height of a degree and now we're going to loop through it and increase the days-old of all the crops remember again that is this slot for all of our crops column number three so I hope you're getting used to looping through of the grid so we're going to use the same kind of format as we have before so starting at the first row which is going to be our YY we're gonna repeat and loop through all of our rows and that's gonna equal height because that's the number of entries every time we loop you're going to increment the row that we're on and we're gonna go dears crops data and x and y value so the x value is gonna be three that's the column that we're in the days old column and the y value is going to be whatever YY currently it's and we're just going to increment that value so there we go so we have set up this event but the day cycle has to actually tell the crops when to run it so it's head back to the day cycle and right here we're gonna go with the crops objects and we're gonna have it run its user event one so event perform and we have to give it a type and a number so every time I use this I actually have to open up the documentation and have a look at what everything is cold and you can see that our user event it actually comes under evey other and then we have to tell us which user event so the type is like one of these and then the number is whatever user event so we put evey other and then evey user and we had one okay so that should be it let's run the game and let's quickly plant a few crops so they're growing properly as you can see but let's leave the room and wait for a few days and see what happens and we might see one of the bugs that I mentioned earlier that we're going to have to fix if we come back so you can see that it has worked they have grown if you caught the middle crop being invisible for a second if I just leave and come back in you can see sometimes our crops are disappearing and that's actually because of a bug from earlier and that is located here in the room start so remember when we leave the room we save all the data and then when we re-enter we actually respawn all of the crops so once we've actually respawned them they're just a dirt pit so we needed a way to get them to grow to their current days old so and what I did was I just copied and pasted this basically into the start I taxed that on a kind of the end of the video and didn't think much of it but this code actually contains an incrementing for the number of days old which is not what we want to do when we're entering the room again right so if we go into the house and then come back out and it's still the same day we don't want it to be growing older that makes no sense and it's actually that code that was causing the problem for us because for the crop that was at the very end that had hit its maximum growth stage we were still incrementing its days old further and if you remember how we were drawing the crops we're kind of going along our spike here what actually has happened is it has incremented past its maximum growth stage and it is drawing nothing so it's appearing invisible so that is our problem so if we come back to crops we need to alter this a little bit so we don't need this and we actually don't need this if check here either because basically what we want to do when we enter the room we want it to update its growth stage which is what this code here was doing so we want it to do that every time so we can get rid of that and hit shift tab to make that come back and now for this bit of the code I don't want an L statement I'm going to change this to an if statement because what this is doing is saying so say we've reentered the room and we have it at its proper growth stage now I want to say if our growth stage is at the maximum growth stage then we want to do all of this stuff we want to set the growth stage to its max we want to say that it's fully grown and we want it to do a little sparkle so we put in here if a birth stage is larger than or equal to max growth stage so again just using this lighter than or equal to just for some reason if it's going over its maximum growth stage then we are doing all this and now we're not incrementing it anymore so that should have fixed our problem let's try that again so plant a few crops and leave and we'll wait here for a few days okay and we'll come back out and there we go so that has fixed our problem we're not getting any disappearing crops okay good so there's a few more things I'm gonna set up so if you notice when were inside the house we were still getting our light which is not what I want I don't want the daylight getting into any of the regions when we're inside so what I'm gonna do is disable the drawing of this stuff here if we are inside so I'm gonna set up a variable cold draw daylight and I'm gonna set this false by default and then at the start of every room I'm gonna have it check if we are in an outside room then we're gonna set it to true and if we're in an inside room we're gonna set it to false so we could use an if statement for this but I'm actually going to use a switch statement and what this is gonna do is basically we're gonna say whatever this value is equal to and then we're gonna set up a bunch of cases so in the case that room is equal to the farm room or in the case that is equal to the forest room I'm going to set draw daylight to be true and then I'm gonna break and breaking basically we'll quit this switch statement so in the case that it is the farm room and we haven't put anything here we haven't put a break or and we haven't told it to do anything it's just gonna come right down to the next case and do this until it finds a break statement so what this allows us to do is just put for all of the times where we want it to be true we can just keep listing them like this so if you've got other rooms that are outside just list them all here like that and then I'm gonna have what's called a default and this is if it doesn't find any of these to be true so anything that isn't outside its going to be an inside room then we set draw daylight to be false okay and now we actually have to prevent this drawing so what I'm gonna say is here I'm gonna have if draw daylight and I can write just if draw daylights like this because this will be either true or false so if it finds it to be true it will go ahead and do this but otherwise it's not going to do that so if I run the game again and I come into here we're not getting any change in daylight but it should still be changing it if we come outside sir good see there we go okay one final thing I'm gonna do just so we can manage the day cycle is I'm gonna set up a way for us to pause the time and we might want to do this in game for example when we open the inventory or something but especially for us just while we're creating the game we might want to just turn off this day cycle so I am going to have a variable called time pause and by default I actually might want to make that true so that when we run the game time pause is automatically true which means it is paused which means nothing is going to be changing until we play the time so in the step event what I'm gonna have is a toggle for the time for so if keyboard check pressed so if I hit the tee button I'm going to toggle our time pause value so whatever it is equal to I'm gonna set it equal to whatever it's not and we've used this logic before but just to remind you what this is gonna do is if time pause is currently equal to true this is going to set it to not true which is false and vice versa so it's just toggling it so now if time pause is true so if the pause is true for paused then we don't want to do any of this so I'm going to say we could wrap all of this in a condition over here to say if time pause so if that's true just exit the code block right so we're not going to get any of this incrementing okay so again let's just test that out right so by default nothing is happening with the time but if I press T I can pause and unpause okay so that is just about it for the day cycle but I do want to fix one last bug if we have planting mode open and then we exit the room we get this error and that's because the way we've set up everything with the some of our data structures and drawer events and everything we are dependent on us being in the farm room and we don't want to be planting inside anyway we only want to be planting if we're in the farmer so we need a way to turn off planting mode if we're not in the farm so let's come into the crops and basically I'm going to so similar to what we just did I'm gonna say if the room is not equal to room farm I'm going to turn our planting toggle right here and this is what was allowing us to plant so when we hit the P button we turn planting to true so I'm going to turn this off whenever we are not in the farmer planting equals false and I'm also after that I'm just going to exit the script so that will turn it off every single time basically so none of this code it's gonna run we can't toggle it because we've exited here and if it was true when we entered the room it's going to turn it off immediately so we're never going to run any of this we're never going to draw it because the step event runs before the draw event so that should get rid of any errors that we're having so let's run that and see so turn on planting and exit and there we go and then when we re-enter we are no longer planting alright so in the next video we'll be continuing what we have done in this episode and we'll be doing more of the lighting so that is the end of today's video thanks to everyone on patreon who are supporting me to make these tutorials and a special shout out to 3d Monkey beers and Italian thanks guys for watching I hope you're all well and I'll see you next time
Info
Channel: FriendlyCosmonaut
Views: 19,125
Rating: undefined out of 5
Keywords: day cycle gamemaker, day night cycle gamemaker, light cycle gamemaker, lighting gamemaker, day cycle gms2, time gamemaker, time gms2, time gamemaker studio 2, farming rpg lighting, farming rpg day cycle, farming rpg friendly cosmonaut, friendly cosmonaut, farming rpg gamemaker, rpg gamemaker, gamemaker tutorial, gamemaker
Id: tlEJt5e62Q4
Channel Id: undefined
Length: 24min 22sec (1462 seconds)
Published: Tue Sep 26 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.