Godot 3.1 Grid-based Movement Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

https://fornclake.com/2019/03/grid

I posted this a little while ago but I've updated it with the help of someone on my Discord to remove this tiny bit of stutter when moving. The video was recorded and uploaded today, it took me a little while to get around to it but I'm glad I recorded after the fixes.

I'd like to have a loose series of RPG tutorials that all work together but aren't sequential. One on dialogue, one on inventory, turn-based battling, etc. Let me know if that's something you'd be interested in.

Hope you enjoy!

👍︎︎ 8 👤︎︎ u/fornclake 📅︎︎ May 06 2019 🗫︎ replies

Have a look at this thread: https://www.reddit.com/r/godot/comments/bcb3sg/grid_based_movement_solved/

My comments and suggestions for that code apply to yours as well. You are creating member variables for values that may only be changed in very specific moments and that don't have a meaning at all times. That means that it's now possible to create several different invalid states that the compiler (or in this case the linter) can't find, which is a source for bugs.

If you want to express that there is a process that the player can start but can't modify once it has started you should use animations, tweens or coroutines.

👍︎︎ 5 👤︎︎ u/warlaan 📅︎︎ May 06 2019 🗫︎ replies

Nice! I've followed your tutorials for the Zelda engine (up to the HUD episode, so far) and I've done decently well. Just wanted to give my thanks, mate! :D

👍︎︎ 2 👤︎︎ u/kosthasequel 📅︎︎ May 06 2019 🗫︎ replies
Captions
everyone welcome to my first Godot Engine tutorial since the release of 3.1 stable today we're going to be covering grid-based movement or RPG style movement the player will smoothly move if you're holding a key but when you let go you'll snap to the size of our tiles we're also going to be covering collisions using a ray cast 2d and we'll briefly look at the new tileset editor from three point one there's no sprites involved we're just using the icon PNG that comes with every Godot project if you want to see the source code that'll be in the description below first let's start with a regular node as our route under it will create a new sprites and use icon PNG as the texture rename sprite into layer will be handling our collisions manually so we're gonna use a sprite note over the regular kinematic body 2d nodes that you'd usually use for 2d projects in the inspector go to offset and turn centered off save the scene as whatever you want I'm gonna call it test dot T SCN next we'll add a script to our player node make the template empty and have the path just player Gd so let's start with a couple variables we'll have speed equals 256 and tile size equals four last position is going to be an empty vector to target position will be an empty vector two and moved or or move direction will be another empty vector 2 so speed and tile size are pretty self-explanatory we need speed to be a larger number because we're gonna multiply it by Delta which I'll explain in a second tile size is just gonna be any whatever you the size the length and width of your player sprite is gonna be the icon dot PNG is 64 by 64 so our tile size will be 64 by 64 now these next to last position so when we move less position is going to be where we were moving from we update this every time the player isn't moving and then target position is where he'll move to then move direction is a vector - based off of the inputs we press and it let it's what sets our target position so now let's create a ready function we're just going to have three lines the first one is going to make sure that the player is snapped to the the grid that way if we move him right here it'll make sure that he gets snapped to 64 by 64 good size then last position will set to his current position and target position will set to its current position now we need a function that will set our move direction based on our currently held keys create a function called get moved earth and add the following our left equals input is action crest make sure you do pressed and not just press do I left and right is action pressed UI right okay if you've seen my Zelda like tutorials 43.0 all of this should look pretty familiar to you we're gonna have moved or X equal negative int left plus int right and mover Y is negative int up plus in down so basically these that is action pressed are all boolean x' so they're true or false what we're doing here is this line handles left and right movement this line handles up and down movement we take if it's if left is true if the left key is being pressed we turn that into an integer so that this makes it one and then make that negative and same thing with right so that means if left is being pressed then move direction dot X will be negative one if right is also being pressed it'll be negative one plus one and equals zero and if only right is being pressed move direction X will be 1 so that means if we're pressing left and right we won't have movement but if we're only pressing one or the other it'll move that way or the other and it's the same deal with up-and-down so that's all we really need but I'm just going to put a check to make sure that mover move direction dot it so that we're not moving left and up or right and down or left and down and up and right this is just making sure we're not pressing diagonal keys because in a grid-based movement we don't want the we don't want the player to be able to move diagonally it's not gonna work that well with this format anyway and vector 2.0 this was adding 3.1 this is the same as doing this there's also right bla bla bla but we'll just use euro so cool now we need our process function which I'm going to put right here so we're going to be separated into one section for movement users comments and one section for not moving idle so let's start with our idle States we need the movement to be above it but I'll start with it here for the sake of easier explanation so if target is our target position this just means target position is always where the player is trying to go so if the position is already where he wants to go then this is our idols point we don't need to move so we'll only check the movement keys if we're already at the target position we'll set our last position to position and our target position will be plus equal move Direction times tile size so when we're idle we'll check the move direction so let's say we're checking the move direction the players pressing the up key and then our up our target position will turn into vector vector to 0 negative 1 times tile size so we'll add the tile 64 pixels upwards to our target position that will mean our idle position our position is not art our target position which means our idle State is through and were down to our movement state now so our position will be plus equals speed times move Direction kindness Delta adding this to the position just actually moves the player and our speed move direction and Delta that just makes sure that it's all locked to the current frame rate if we didn't have Delta then if the players framerate is only 20 FPS then the player would move a lot slower and if it's like 120 FPS then it'll move faster multiplying it by Delta just make sure everything Delta is the time since the last frame if you want to read up more on this you could just look up Wikipedia Delta timing and there's there's a whole lot of information on this so yeah this will just move the player if we're not when we're not the target position then we'll be getting the new move Direction blah blah blah and the player will move here now if position dot distance to last position is greater than equal tile size minus speed times Delta so in a position equals participation so this line we check if our current position the distance from our current position to our last position is greater than ignore this part for right now is greater than our tile size so if we've moved more than 64 pixels then we'll set our position to our target position so if we if something happens and we move far away well now the game will check wait that's not right and then just snap them right to the target position this also makes sure that we keep moving if we only press the key once we'll keep moving until we finish that tile now this minus speed times Delta what happens with this is it'll check if it's greater than the tile size and then you can't actually see it happen but it'll snap it'll move forward and then snap back to our target position which will make it very clear that we're moving in a grid it won't be smooth movement if we're holding the key this just subtracts the amount of distance we would have went so that between frames or from the frame that were not at the target position between the frame that we reach it it'll just snap there instantly that just makes sure that everything is smooth so if we run the game now let's set the main scene to test we have grid movement and as you could see there's no there you might see a little stutter because I'm recording but there's no there's no breaks and movement there's no when we're moving left you can't tell that it's a grid but if I just tap writes it'll move until it's in the next grid so that's everything for the movements now we need to do the collisions first let's make a little map for the player to walk around in so add a new child node to the node and it's going to be tile map create a new tile set and down here we'll set the image to the icon B&G again let's make this the whole screen a new single tile snapping options BAM and select a tile set the modulate to zero we can't see it here but this will just make it black so now let's go back to our tile map see it's black set the size make sure it's 64 by 64 or whatever your tile size is and let's just put some walls around our scene [Music] Oh one thing I forgot to do go back to the title set and we're gonna add some collisions to this collision Square BAM okay so now we have a wall around our scene with some collisions however because the player is not a kinematic value to D it is just a sprite we don't need it to be a body instead what we could do is just use a ray cast the raycast is going to face wherever the move direction is going we're going to set them ray cast to look that direction and if the raycast is ever colliding with a wall we just won't let the player move there so set its position to 32 by 32 or whatever just make it the center whatever half your tile sizes or your player your sprite size and make sure you set it to you check the box for enabled now let's go back to the player script on ready variable array equals ray cast 2d this will just let us say Ray instead of dollar sign recast 2d when we're referencing in the script at the bottom of the get move directions script we're going to nope not at the bottom no yes at the bomb sorry if move Direction does not equal vector to zero so this is any time the players movement remember this is just for if the player is moving diagonally this is if the player is moving at all all so if the player is moving we're going to cast the Ray to move direction x tile size divided by two this will just set it right to the boundaries of the player so it would end up being 64 divided by 2 it'll just go right up here so then we need to add some stuff to our movement code so if Rey is okay we actually need to reorganize this a little bit if Rey is colliding position equals last position target position equals last position so if the player hits a wall we'll just snap him back to where he was before he had a wall and it'll look like he never hit the wall so if it's colliding do that if it's not colliding let's highlight all this and press tab if we're not colliding then we'll move so let's play the game now we walk up to the walls you'll stop when he gets there and if you want you could go to debug visible collision shapes and you could see exactly what's happening when we move right the raycast moves to the right up moves up bla bla bla and yeah we can't collide into the walls this is just a little bit less expensive than using the can mag value 2d nodes and it works pretty nicely so yeah that's it for this tutorial I might come back to this and do animations and stuff in the future but for now this is enough if you want to see more feel free to message me on my discord or Twitter and if you want to support me I have a patreon which keeps these tutorials going all of these links can be found in the description and yeah thanks for watching
Info
Channel: fornclake
Views: 21,321
Rating: undefined out of 5
Keywords: godot, game dev, tutorial, rpg, grid, programming, beginners
Id: JtnnKVxoH5k
Channel Id: undefined
Length: 16min 57sec (1017 seconds)
Published: Sun May 05 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.