Crops, Grids, Scripts | Farming RPG Tutorial: GMS2 [P7]

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello there guys so we're finally starting the crop system today because there's so much to get through I've broken this up into multiple videos because otherwise this would be hours long but today we're going to import our crop sprites make all the crop objects set up a planting mode so that when we're in planting mode we create a crop where we click and we're going to use a data system called add es grid to make the different crop types and it's this last one that I want to take the most care with because they're actually going to be creating and using these grids a lot going forward so I want you to have a solid understanding of what they are and how they work and today we're just starting slow and creating just the one so let's go over what a grid data structure is actually going to look like so I'm sure you've seen a grid before it's a box with rows and columns and along here you can see I've numbered each row and column and take note that I haven't started at one as with a lot of things in programming we're actually starting at 0 so our first column is 0 now let's talk about grid coordinates so the grid has these two axes this is the x-axis going across and to the y-axis here coming down so let's say I want to refer to a specific cell in the grid so one of these little boxes I would give you a grid coordinate so so for example if I give you the coordinate 1 3 so remember coordinates always go X Y to the 1 here is referring to the x axis so we come to here and the 3 is referring to the y axis so we go here so this is the cell with the coordinate 1 3 we'll just do 1 law so for the coordinate 3 0 so the x coordinate is 3 over here and the y coordinate is 0 so this is our cell ok moving on so and this is where things can get a little bit confusing through the whole starting at 0 the-- but if we talk about the grids width and height we would count the rows and the columns here to get there within height so this grid it has a width of 1 2 3 4 and a height of 7 alright and now let's talk about our grid when we make our data structure we're going to call it D S 4 data structure and then crops and types we're going to have a couple of grids and we're just going to use this naming convention it might sound a little odd but it's going to help us later on and in this grid we're going to be storing two kinds of information for now we'll probably add more later but this is just what we're doing for now so the first one and it is going to go in the very first column so column 0 this is going to be the crops growth stage duration which just means the number of days until it goes to the next growth stage so if we have this plant right here and let's say it's in growth stage number 2 or something the growth stage duration would refer to the number of days it takes until it gets to the next growth stage and so this would just kind of be represented in the game by going to a new sprite so obviously a crop that takes longer to grow is going to have a longer growth stage duration and we can set this as different numbers for different crops and the second thing we're going to define in the crop type is its costs as in how much do we get from selling it so that means we have two types of data to put in a grid again I'm sure you can imagine many more we might say have a growth season to say you know that this crop core is during the summer or the spring but we'll just keep it as these two for now to keep it nice and simple ok so we've got two types of data in our grid and these are going to go in the first two columns so column 0 and column 1 and this means we're going to have a width of 2 now what about the height well the height is just going to be however many crop types we have and we're actually going to be writing a script that will add a new crop type whenever we want but more on that later so this is what a grid is going to look like as you can see here's our two columns and I have seven crops which is why I've got seven rows filled in right here but you could have more or less that's totally fine and I've basically just made up some numbers here for the growth stage duration and the cost again you can put different numbers the system will be able to account for it and then for our own sake I'm just going to add some headings so now you can see which crop type is which and actually this order is going to be really important later so the order here that you see with the tomato is first and in the potato carrot and so on this is going to come up again and again and in fact these numbers right here they're going to serve as a sort of index for the different crops so tomatoes will be at zero the potatoes will be at one and so on so for example when we draw the crops then we're going to be using a similar system for the one that we set up for our people we're going to have this one being sprite sheet and we're going to be drawing just part of the sprite so the crop of the tomato then we'll want it to be drawn things in this row and if the carrot it will be drawing stuff in this row so again we have bird zero bring the tomato four one two and so on and then the thing that determines which of these five sort of stages that it's drawing will be whatever growth stages in all right so with that signing out of the way I think we're ready to get coding let's go over to game maker okay so first off we're going to be importing our new sprites so I'm going to create a new group and call his crops and we'll import the first one and we'll click on this one and this is the kind of sprite Atlas for the crops so we'll call this one SP crops and next is sort of the crops when they've been picked so SP our crops picked so import something handy that you can do when you're importing sprites to game maker if you have this on the end of it this underscore strip and then a number when you import it it's going to treat your sprite as a strip image and it's going to cut it up and put it into individual frames for you right here you can see I call this strip seven and it's because there's seven different crops so if I just hit open it automatically imports it into the different frames so that's pretty handy and we'll do the same for the little sparkles and this is just going to be the thing that appears when the crop is fully grown to let the player know that it's finished growing and it's ready to be picked there we go alright and now let's add a couple new objects let's add the crops group again to keep our project nice and organized so the first object we'll make is just the crop itself so I'm going to call this obj crop and for the sprite I'm just going to give it this one for now just so that we get this in the project window but we're actually going to be drawing the crops from that sprite sheet up here so for now I just want to set up our obj crop and this is going to be all of the individual crops so we're going to have to give it all the variables we were talking about and get it drawing the correct sprite part okay and the first two variables I want is going to be framed with and frames height and these are going to equal the frame width and height of one of the individual frames here so if I just put on this grid and I can tell you that each frame is 32 by 64 here so you can see this gives us the different frames the crops so back here we can put in 32 and 64 but obviously if you've got a different sprite sheet then you're going to have to change these numbers accordingly all right the next thing we're going to do is the crop type so I'm just going to put 0 and if you remember from before from when we were setting up this 0 was going to be the tomato and this is just going to be the index of the different rows so here we've got the tomatoes I think this is the potatoes the carrots artichokes and so on alright so the default is just tomato but we'll be changing this later we'll also set up days old to just be 0 start the growth stage and just keep in mind that these two things are different we can be three days old and still be engrossed 8 1 which is going to depend on our growth stage duration I remember this is the thing that we'll be getting from our data structure so finally we're going to have max growth stage and so this is just going to get the very last growth stage to the crops and again I'm going to set this up so that it will work for whatever size crops bite that you've got so we're going to write write get with of STR crops so this is going to get the width and then we're going to go over frame wit -1 so for example if we've got a sprite that is a hundred pixels wide and a frame width that is 25 then our maximum growth stage is going to be 4 minus 1 and we have to put the minus 1 because of how our data structure and everything starts at 0 all right and next we're going to put fully-grown equals false because it's not fully grown yet and we'll have sparkles equals false and remember the sparkles going to be the little flourish effect that would tell the player when it was fully grown all right so the next thing we need to do is get it drawing right thing so if we come into the drawer event and just like before we're going to say drawers right heart to kind of just like with the curly braces you can actually do this with just normal brackets you can have things on multiple lines if you've got a very long line which is going to be so down here is the argument that we have to give it the sprite so SPR crops the sub image and remember there's just one frame so sub image zero because it starts at zero and now we have to give it the coordinates on the sprite to draw so a left top and then a width in height so this is going to be just like before with our people we want to be giving it this top corner here here here here here depending on whatever growth stages in so that it's drawing just this much every time so we're going to say in growth stage times the frame width so that we can get the number of pixels crop type times frame size because remember crop type is going to be equal to a number from zero to six for all of the different crop types remember tomatoes the zero potatoes for one carrots were three and so on so we're going to get zero times 64 one times 64 and so on and finally the width and height or we know that that's just 32 and 64 and finally a coordinate to draw all this and so now we'll just put X Y so that should do it for now let's come into our farm room and let's put in a crop make sure we're on the instances layer and bring in the crop and because of all of the variables that we set up we should actually just see a dirt pit because we set everything to zero so I might actually just change that so that we start at stage let's say for there we go so that's working perfectly okay so I'm actually just going to delete this because we're going to be creating crops with the mouse so we're going to create another object called crops and we're going to set up a planting row so that if we're in planting mode and we click with the mouse we're going to create a crop and this crops object this is sort of going to be one of the meta objects I might actually move it in there because this is going to be the manager to the entire crops system and actually we're going to go ahead and take persistent and bring it into our room zero because this object is going to persist throughout the game it's going to be where we put all of the data structures it's going to be taking care of the crops different growths and the creating of the crops let's jump into its create event and the first thing we're going to do is just initialize the variable that's going to be storing the data structure grid so when a call is remembered es crops types and for now I'm just going to set that to zero so it's not a grid yet it's just a variable that's equal to zero because we're actually going to be creating the grid on L mouse click the next thing I'm going to do is just like I was kind of saying before with the 0 1 2 3 4 5 6 being a kind of index for all our crops we're going to create an enumerator similar to what we did with the directions when we were doing the facing of the player to enum crop and we're going to have all the crops here so I could write tomato equals 0 potato equals 1 and so on but these are actually the default values for enumerators so if you just put tomato comma just like this it's gonna take care of that for us current hard to church chili good by the way I've called it a good but I actually am not 100% sure if that's what I do so if you've got a better idea what this plant is please leave that in the comments because I've asked a few people and we're not 100% okay so back to the crops and a last one was the corn now the next thing we're going to do is create a script and this script is going to create the different crop types so right here we can create a new script and we can call this bit of code from anywhere you want they're kind of like functions it's like setting up your own function am i going to call this create crop type basically what we're going to do is we can just call it from within here like this create crop type and then we can give it some arguments for example for our tomato like this and we're going to have it take care of that for us and set up the crop type for the tomato just by giving you these variables here so remember this first one was going to be a growth stage duration so for tomatoes I'm saying that it's going to take four days for it to move between growth stages and I'm saying that I'm going to sell them for forty negatives currency but you can see right here that gaming is kind of complaining and that's because we haven't set up the script yet so let's go do that and when you're making your script it's important to leave descriptions so that when you're writing it in code alpha that you can see what the script needs what kind of arguments in it and I'll show you what I mean so we're going to write at description create crop type just the name of the script and we're going to have two arguments here women say OGG growth stage duration the first one and the second one is the cost so now if I come back to crops and we type this function in it's going to give a description and the type of arguments that it wants the first thing I want to do is get the number of arguments that we've given the script because remember what I was saying before about it being important to try and generalize everything if we're introducing more arguments later the scripts will be able to adapt so we're going to make a variable are numb and this is going to be equal to the argument count okay so the next thing I want to do so if this is the very first time that we're calling the script the very first crop type of we're creating the grid data structure doesn't actually exist yet so we have to create it so first we check if the s doesn't exist ts crop types so it's checking if a data structure exists but we also have to tell it what type of data structure it is so we just type C S type grid okay and then we create the grid and we'll save it in our variable D s crops type so we go dears crops types is equal to D s grids create and now we have to tell it what width and height to create the grid so with the width variable this is actually going to be equal to the argument count all right so because if we give it two arguments then the number of columns we need the grid is going to be equal to two and if there's more then it'll be that so we can just put our genome and the height so now we'll just make it one because we're just creating the first row for the grid I'm also going to create a variable called var pipe and make it equal to one and this is going to be the height of the grid all right so what about the other case where we're calling the grid for any time that isn't the first so the greens already going to exist so this is going to be false so we're going to go else now for these cases because there's only one row in the grid now what we're going to have to do is increase we have to increase its height then that row will belong to a new crop type but to do that we have to get the current pipes of the grid so that we can resize it to be one more so we're going to go VAR v is equal to DX grid pipe and then we go dears crops pipes and now we can resize the grid so we go dis grid resize TS crops types and then again the wit should just be whatever the argument number is odds num and the height is going to be one more than the current height so we're going to put types plus one and now what I'm going to do is I'm going to increase the height variable by one because we've resize the grist I want to change it because we need this variable in a second right so this section has managed the size of the grid we haven't actually changed any of the grids variables the whole point of this script was to slot in characteristics of a new crop type and then set them in a grid so we have to access the grid and then change some of it cells so the way we access a grid is we type DEA's crops types and then this little hash is the access to get the cell and then we give it a certain XY coordinate then we can set it equal to some kind of variable so this is going to set this cell equal to whatever we put here now we're going to have to do this a couple times for each x coordinate in the row and again so the number of times that we're going to have to do this will be the number of columns that we have which again is just equal to the argument number so I'm going to write a repeat statement but we're going to want to write var I equals 0 and explain this in a minute and then we're going to repeat and a number of times we're going to repeat this because we're setting multiple cells you're going to equal to odd none to the right add nums like that and then we're going to increment I so what this does is we can set the x coordinate equal to I so that the first time that we do this loop I is going to equal 0 so we're going to be setting the first column and then on the second loop I is going to equal 1 so we're going to be setting the next column enter this way we can keep going across and this repeat loop is going to do all the work for us instead of us typing out this serve it over again and it also makes it variable to the number of arguments that we give it so now but what argument are we going to be setting it to and we can actually use this little eye here to do the same thing with argument so if we type argument and then I so again we're going to be setting when I is equal to zero and we're setting the first column we're going to be getting argument zero which is going to be this one and then when it's one and we're setting column one then we want to be setting argument one the cost and it'll keep going across like that it's a very nifty to do it this way now finally the only thing that we haven't really talked about is how do we know what row we're accessing what seduce variable going to be well it'll change every time we call this script it will be dependent on whatever the height of the grid is currently so if it's the very first time we're running this we want this to be zero right because it'll be Rho zero and if it's the second time we want it to be one so again it's kind of equal to the height the current height of the grid after we've done all this resizing minus one because we're starting at zero so what I'm going to do is I'm going to create a variable again called by Y and an equals height minus one and then we just set this to YY and we're finished now we can come into crops and define all of our crop types I'm just going to copy and paste this a few times and I'm just going to leave this up a little bit to put in all of the crops all right now the thing with data structures is they're a little bit different to other variables so generally when you run your game variables like this are stored in memory and it's deleted once the game is done or once an object is deleted but data structures they actually persist in memory and they're not deleted when an object is deleted or when that game and so you have to do your own cleanup so we're going to do is make the destroy event I'm going to say if the S exists so if our data structure exists and the type is yes grid - in the event the way destroyed we want to also destroy the grid yes destroy and we'll copy and paste this and also put that in the game end okay and now we're going to set up the code to make a crop going home into the step event and we're going to say if if we click then we're going to create a crop I'm actually going to create another script to do this but for now let's just put if the mouse check button pressed can be left so that's just mouse button left and then we're going to use a script that we create and I'm going to call this instance create crop and this is going to be quite similar to just the instance create layer function so we're going to give it a coordinate and then we're going to give it a type of crop and it's going to create the crop for us so instance create crop and we're going to say wherever the mouse X and mouse wise and from now I'm just going to say crop let's go corn it's complaining again just because we haven't actually set up a script yet so let's come into instance create crops let's give it a proper description instance create crop ah and this is going to be the X the Y and finally the crop type so now what we're going to do is create the instance give the crop this characteristics depending on whatever type of crop it is ok so let's create the instance so we're going to go instance create layers so we have to give it some arguments here so we're going to go argument 0 argument 1 we'll put this in the instances layer and the object we're creating is obj crop and remember this function here not only does it go and create an object for us it also returns its ID and we can store its ID in a variable so I'm going to store it in far s like that so now down here I can go with against and I'm going to change some of its variables so remember in Ruby J crop we had crop type and growth stage duration so now we can update this without script we're going to go crops type is equal to and what argument do we want argument number two which is actually the third one but again it starts at zero two arguments two and first stage duration and we're going to actually just pull this the growth stage duration for this specific crop from our crop type group so we're going to go dears crops types use the accessor to remember the growth stage duration is in column one and then the y-coordinate it's going to be whatever the crop type is so that's going to give us the row that we want to that crop now we can't actually just keep it like this because we're in obj crop we're not actually in our master object anymore so this variable here it doesn't belong to us we have to go get it so we have to write props like this because this variable is data structure it belongs to crops and it's not a global data structure you can actually make it global but we'll just keep it like this for now so the final thing we're going to do is very similar to this I come in here that returns the ID we're also going to return the ID so we're going to approach return inst so that if we wanted to do anything with our instance when we use this script now we have and I'm just going back to the crop so again that we can actually see what properties because right now it's just going to give us a dirt mound if we start at growth date zero so I'm going to change this to three okay and let's run the game and see if that works there we go so we're creating a crop but you can probably see the coordinates of the crops there were a little bit off and that's because it's kind of origin where we're drawing it from is at the top left hand corner so that's where it appears to be when we create the instance so let's go and change that's that's come into obj crop and we're going to modify where we're drawing it then we're going to set up a couple new variables for this so we're going to say X x equals and we're going to correct basically what we had before so we kind of have to get from to remember if we were clicking up here it created the crop kind of down there so we have to kind of subtract half of the frame width and then a big chunk of the frame height so again you might want to play around with these variables if you've got a different kind of sprite so but I've worked this out to be about X minus 2 frame with over 2 plus 3 and the y to be frame height plus 6 so then in a drawer event instead of drawing at the X&Y location we're going to draw a bit above it so xx and YY so now when we run the game again there we go all right that's a lot nicer so now we're going to do a couple more things we're going to add a planting mode that we can toggle in and out of and we'll also do it so that we can select what crop or planting we're not always just going to be planting corn so we're going to have a kind of visual indication of what we're planting and the way we're going to do this is we're going to draw one of these over the mouse cursor and whatever one it's on that's the crop that it's going to create so they'll come back to the crops and we'll add in two here we will add planting and we'll save it's a false by default so we'll be not implanting mode at the start and we're also going to create select crop equal to zero and I'm actually also going to create an MX and an my and these are just going to store the mouse variables because I'm going to be using these a few times in different events and this just means we can save it one time we're going to be updating it in the step event and then we can go ahead and use it in the other events instead of always just getting this variable so we're going to go so we can just set it to zero at the start because we're going to be updating it in here so first we'll do the toggle so I'm going to say if keyboard.check pressed and I'm going to say the P key for planting P for planting so if I hit P I'm going to toggle the planting variable so planting equals not planting so we've done a similar thing before so this is just going to make it so that if it's currently true then it will become false and if it's currently false it'll come true so there is our toggle and now we put so if we are planting then we're going to do all of this so if we're in planting mode we're going to update the MX to be Mouse X in my dream s5 and now I can just put MX and M like that now we're going to do our little visual indication of what we're planting and again I'm going to say if planting actually we can just say if we're not planting go ahead and exit but if we are planting then draw crops to be planted and so one note here make sure for you that this sprite is centered because we want it to appear sort of over the mouse right here in the center I'm going to go draw sprites SPR crops picked and now the sub image is going to be what determines what kind of crop is appearing and so we set up a variable called select crop I'm going to be drawing whatever this is currently set to so there could be anything between zero and seven for us because that's how many crops we have in here again if you're using a difference right make sure you have a similar system setup okay and again the X&Y location is just going to be aware of the masses so input M X and M like now we want to come back here and instead of just making the corn we're going to say again it's just going to be whatever the select crop is equal to so you can see how we're reusing this again and again this little index of zero to six now the next thing we want to do is set up a way to change this and I'm just going to have it so that if we scroll up and down we can increase or decrease the next crop so I'm going to put if mouse wheel up select crop plus equals one and the same for down I'll have it subtract but I don't want it going over the number of spikes that we have right because if slash crop gets to something like 20 well I don't have that many crop types and I don't have that mix price so we're going to start running into problems so we're going to want to cap it at however many crops we have there's a few ways we could do this we could get the height of the grid again or we could get the number of images in our sprite here so you can do it whatever way you want I'm just going to put the image number so we're going to come into props here I'm going to say so if select crop is larger than sprite get number and this will give us the number of frames in the sprite but again because select crop it starts at 0 we have to put a minus once so if it's the case that select crop has gone over the number of images that we actually have we want to put it back to the start so that it kind of loops so if this is the case then we're going to put select crop equal to 0 and now we need the case if we're going the other way so if we get to a negative number we also want it to loop back to the front so else if select crops is less than 0 then we're going to set it to the number of images so sprites get number FBR crops 6-1 again okay so that should be it so if we run the game now we should have all of that system in place so planting motors off so if I'm clicking around now nothing is going to happen if I hit P there we go so we've got our Tomatoes coming up and if I scroll up and down there we go so it's looping and if I scroll the other way okay so let's plant a few of these so there we go chilies the goods or whatever they are corn tomato and potato there we go so everything is working so I think this is a really good start for us we've got a bunch of scripts set up we'll play around with the data structure and there's still so much to do so we're going to have quite a few more videos on this right so we still need to set up the grid and we have to snap the crops to the grid we're going to be checking if there's other instances that already exist before planting going to be checking if there's soil beneath us before planting and of course we also have to set up the growing of our crops because right now they're just kind of stagnant so we're going to be doing some of that in the next few videos but for now I hope that gives you a nice little start to the crop system and an appreciation for just how much kind of setup and how organized you have to be to create something like this so I hope you were all well and I'll see you next time
Info
Channel: FriendlyCosmonaut
Views: 26,695
Rating: undefined out of 5
Keywords: farming rpg tutorial, farming rpg tutorial gamemaker, rpg tutorial gamemaker, crops gamemaker, crops gms2, crop system gamemaker, crop system gamemaker studio 2, gamemaker studio 2, friendly cosmonaut crops, friendly cosmonaut farming rpg, friendly cosmonaut, gamemaker studio 2 tutorial
Id: UC3G-lfnBvA
Channel Id: undefined
Length: 32min 28sec (1948 seconds)
Published: Wed Aug 09 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.