r/GameMaker : Episode #01 - Fixed Grid Movement

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone welcome to the very first video of our slash game maker a video series where we take a look at your questions on the subreddit and answer them in this video so the first post is from a user called Digital Ronan and they say I was wondering if there were any examples or resources covering how one would set up grid-based movement like a roguelike where you can only move one space at a time now it's kind of hard to leave answers to leave very detailed code answers in reddit threads or any sort of text form so I'll read you what I have the best that I can but then we'll jump into a project and go over how I would implement this so the first thing I said is I would create a variable called input and an alarm that trigger is every you know X number of seconds right so this alarm would just keep reach rigging and whatever value is stored in input that is going to be what triggers our movement so I say this will create snappy movement that can only be activated every X number of seconds since the alarm is the event triggering the movement so then I give an example of that and I say once this is set up you can just have your alarm reset itself so that it continuously fires and reads whenever functions whatever function is stored in input and moves that way so hopefully that makes sense let's go ahead and jump into a project and see how we can do this right now so I'm gonna go ahead and create a new project alright so here I have a project set up it has a simple sprite player sprite a simple player object and a basic room with our player object in it I have not done anything fancy at this point I've just set up everything just to show you the sprite player is a size of 64 by 64 and an origin place in the top left corner add a create event for our player and we're going to define a couple properties in here in our variable called input as I mentioned in the answer so I'm gonna say input is equal to undefined now this will just start as a null value which we will assign in just a second now the other thing we want to do is we want to add a variable called alarm interval and I'm gonna set this equal to 30 frames so this is going to be the interval in which our alarm triggers so now what we've done is we've created the initial variables here and we want to go ahead and define how our grid is going to look now usually I would put this in some sort of grid control or game controller but for the simplicity of this project I'm gonna just define it inside the player itself so I'm gonna say grid width or I'm gonna say grid unit width is equal to 64 now I've done 64 because our player happens to be 64 by 64 so it makes sense that our player is going to be the same size as the unit width that we would like to move so now we've set up an input variable we set an alarm variable and we've set our grid unit width let's go ahead and start our alarm and have it retrigger every X seconds just to make sure we get a nice cyclical alarm system going so I'm gonna say alarm 0 equals alarm interval and inside the alarm I'm gonna go ahead and create an alarm event and I'm gonna say alarm 0 equals alarm interval so now what happens is the alarm will once the alarm finishes it will reset itself and constantly read trigger so if I go ahead and just do some sort of tests on this a show debug message alarm alright and if we go ahead and run this project what we should see is a series of debug messages that constantly print all right alarm and so every 1 second or so or 30 frames in our game our alarm keeps read triggering over and over now we don't have any logic in there so nothing's gonna happen yet but what we want to do is we want to create a series of scripts that are going to dictate how our player will move and then we can just call that script every interval of our alarm so let's go ahead and create a couple scripts I'm gonna call this one move left and I'm just gonna say X minus equals to grid unit width perfect and that is all that we're gonna do for that script now I'm going to demonstrate it with just two directions and this will give you the idea that you need however when you go and implement this you're gonna probably want one for every direction so I'm gonna say move left and move up I'm gonna say Y - equal to grid unit with now the width and the height of the units are going to be the same because it is a square grid so I probably should name this variable something a little more universal but besides width which width indicates simply a left and right movement however like I said they are the same width and height because it is a square grid so I can use this width the variable for up and down movement so now we've created two variables call or two scripts called move left and move up so now what we want to do is we want to say well first thing we need to do is we need to store these movement scripts into our variable so that it can then be read and interpreted by GameMaker so first thing I want to do is we have our input variable created but what we want to do is we want to store these scripts so I'm gonna create a step event I'm gonna say if keyboard check pressed BK left now normally we would do all of our player movement inside of this keyboard event however we don't want the keyboard itself to trigger the event we want the keyboard to try to the keyboard to try to trigger the event but we want the game to restrict the movement to only a specific amount in a specific time so instead of moving our player when we press left we're gonna say input is equal to left now it's important that we do not include our parentheses here because we don't want to call our scripts when we press this we want to smear least or the script as a resource reference into our input so this would be the same thing as saying input equals SBR player this is simply an index reference to our resource in our resource tree so by not including the parentheses we are not calling the script but we are storing the script itself into the variable so what we're gonna do is run a copy and paste this and I'm going to say if VK up and instead of calling you instead of storing move left we're gonna store move up so now what we've essentially done is every time you press left and up we're gonna override whatever value is stored in inputs so let's go ahead and add some visual debugging for us so that we can see I'm gonna go ahead and draw add a drop GUI event I'm gonna say draw text at 10:10 and I'm going to say scripts gits get name inputs so now we know that input is just holding a reference to a script so we can call this script getname method on input and it will tell us what script is currently in input so right now we have input define set as undefined so this is freaking out we have to have some value set in here so instead of doing that I'm just gonna say if input not equal undefined then we will draw it this way game maker doesn't freak out and say I'm looking for a script here but you've given me null or undefined and so now it won't try to do anything but if I press left now you can see in our top left corner we're drawing move left and if I press up move up and so as I it between left and up we are changing the value of inputs to hold a different script there so now all we have to do is this is basically 99% of it in our alarm event instead of doing nothing here what we want to say is we're gonna say script execute and we're gonna say inputs so now what this script execute method will do is it will look for the script that is stored inside of input and then it will execute that script so let's go ahead and move our player since we're only going to be moving up and left let's go ahead and move our player somewhere over here and let's run the project and now as I press up nothing happens until our alarm triggers now what's happening is left is up is getting stored in our input variable and it keeps retrigger in so we're never wiping it clean at any point so by not touching anything that the alarm keeps retrigger in itself and keeps moving in that input direction that we've specified so what we need to do is we need to after each alarm iteration we need to wipe the input variable clean so that it forces the user to put another input into the game well I'm first thing I'm going to do is I'm going to change these events to check because we don't we want the player to be able to hold down left or up to move so let's make sure that these are set to check instead of check pressed and what we're gonna do is after we execute the script we're gonna set input equals to undefined and we need to make sure though before we run our scripts we want to say if input is not equal to undefined so now what we're gonna do is we're going to check to make sure that input is holding some value then we're going to execute input and then we're gonna wipe it clean and then when the player presses the keyboard again it will store a new value at input and then it will get triggered in that moment again all right so nothing's happened I press up and now we moved up one but input has now been erased and nothing happens if I press left left I move left one now if I hold left I keep moving left if I hold left and up I move up because it takes whatever is the most recent keyboard press and read that so this allows us to kind of this allows the player to hold down multiple keys and get very precise movement let's go ahead and test this theory by changing our alarm interval interval and let's set this to 10 frames so now every 10 frames we are going to be moving I hold left and now you can see we're starting to move a little bit faster I hold up and now we have some faster movement in our game now you'll notice I don't know if you'll notice this small detail let's see if I can try to recreate it it's a very subtle detail that we have in this that you might not want in your game so if I press left and I let go if I time it right I can keep the interval and move faster beared see there we've got some weird weird things happening where sometimes I have to wait the full 10 frames and sometimes I don't because we're catching it right as the alarm is triggering the reason this is happening is because the alarm is constantly running in the background and as soon as it gets a value and input it executes that so if we press a button when alarm is equal to 2 because it's counting down from 10 to zero let's say it counts down from 10 9 8 7 6 5 4 3 2 then we input well we only have 2 more frames left before our script executes so we aren't getting that full alarm interval weights from the player so there you can actually cheat the speed and move faster if you're able to time these inputs right before the alarm triggers so what we want to do is instead of constantly resetting the alarm every single time we press a key or every single time the alarm finishes we actually want to only trigger the alarm once a keyboard press has been put so let's go ahead and wipe these alarm sets that we have here and inside of our input move left we're gonna also say alarm 0 equals interval time alarm interval and now I can copy this here and now I can copy this here now one thing we have to keep in mind though is if we don't do any sort of alarm value checking in this moment holding it down is going to keep resetting the alarm to our interval which is 10 so we're gonna hold it down it's gonna say 10 10 10 10 10 10 10 10 10 and it's never gonna count down so we only want to do this if alarm 0 equals negative 1 and this means the alarm is not currently counting so now what we're gonna say is when I say okay if our alarm isn't counting down let's assign a value to it and this means that we can only start that countdown timer once now what we could do is we could also do one more check and we could say if not keyboard check DK up and not keyboard.check BK down we want to set our alarm equal to negative one so now we're saying if at any point the player is not pressing any keys we're gonna turn off the alarm so this way the player has to be engaged with the keyboard in order to get movement from the player so let's go ahead and run this project and now we should have two things number one if we press the key and we let go the player will not move and if we press the key we can not cheat that timer by getting it right at that last second all right so I missed one thing real quick let me go ahead and change this this is not supposed to be V Cave up or I'm sorry not down but this needs to be left because we've only implemented left and up movement now again if you were to implement this in your project you would want to do this for every direction so you would change this to say and not keyboard check up and not keyboard down and right but for this example we are keeping it relatively simple okay so let's run it again this should fix the problem that we just saw all right so now I press left and I let go nothing happens so I press up but I let go still no movement now if I press up and hold perfect if I press left and hold great so let's say you wanted to have that first movement be initial as soon as you press a keyboard you move that direction immediately but then if you're still holding it down it resets at a very specific time err so this will unlock an immediate first movement but then if you hold it down you get delayed set movement well we could get we would get rid of this here as soon as we press a button we don't want to set our alarm to our alarm interval because that will say oh hey you know you have to wait 10 seconds for 10 frames before you can move instead what we want to do is we want to set our alarm to 1 this will say we're gonna move immediately we're only gonna wait 1 frame before we start moving and then in our alarm events instead of resetting input to undefined we're gonna first check we're gonna do our keyboard check that we did here in our step event we're gonna do this now in our alarm events so we're gonna say we this up here line two this is going to stay unchanged we want to make sure that input has it holds a value before we execute it but what we want to do is we want to check if our keyboard is still being pressed down once the alarm executes then we reset the alarm so I'm gonna say if keyboard check VK up or so we want to check if either of these is pressed so if keyboard up or keyboard left we're gonna say alarm 0 equals alarm interval and what we can do and then what we want to do is we want to say we only want to reset input to undefined if we aren't pressing any keys by the time we get here so now let's go ahead and run it and see what we get so now I press left and I move immediately but you'll notice we have a bug here so if I press up I move immediately and then there is that alarm that is kind of delayed beforehand so what's happening is what we're doing is we're setting this alarm equal to one and that one frame is happening immediately and then we jump into hearing we check for our keyboard where our keyboard is still being held down because we cannot we're not pressing it frame perfect' so we press it we let go but this alarm has already executed so we're putting in one extra alarm interval what we want to do is we want to check again if we want to bring our key what we're going to bring our keyboard check back here and we're gonna say if we're not pressing anything now we want to turn off our alarm so we took it out for a second just to demonstrate something and now we're bringing it back to get our final features implemented so let's go ahead and check I press up I get an immediate movement but nothing else I press left immediate movement now if I hold left I move left and if I hold up I move up and it doesn't matter at what time I press it it is impossible for me to cheat the timer and to get a faster movement alright guys that is everything for this video thank you so much for stopping by and watching if you enjoyed the video please consider giving it a like and proud seeing that subscribe button if you would like your question featured in the next video that I do in this series please post it to Reddit and see what people respond with there's a lot of great answers on reddit and people are fantastic at giving advice but if for some reason you still don't feel like you have a good understanding of it or you just didn't quite get the answer that you were looking for please send me a message on twitter or on discord and i will try to include your question in the next video thank you so much guys again I appreciate it let me know if you like this series idea because I'm trying to come up with new ways to create GameMaker content that's more than just traditional tutorials I think this is a great way to engage the community a little bit and still do tutorials and education at the same time so thanks so much and I will see you in the next video [Music]
Info
Channel: GentooGames
Views: 6,441
Rating: undefined out of 5
Keywords: gamemaker, gamedev, indiedev, game, maker, studio, gamemakerstudio, how, to, make, create, using, design, shaunspalding, shaun, spalding, heartbeast, pixelatedpope, pixelated, pope, gravityshitgames, gravityshift, games, gentoogames, gentoo, tutorial, teaching, teach, lesson, howto, development, indie, grid, movement, rogue, like, lite, roguelike, roguelite, rogue-lite, rogue-like, move, top, down, topdown, top-down, snap, snapped, fixed, fix, rpg, reddit, r/gamemaker, episode
Id: RZNFnhA57WQ
Channel Id: undefined
Length: 18min 14sec (1094 seconds)
Published: Sat Jul 06 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.