How To Create Checkpoints - Godot 4 2D Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's up guys today we're going to make a simple checkpoint system for a 2d platformer in Gau 4 all right so to start with we need a way to kill our player so we've got this pit over here and we need to add a death zone so that when he falls in the pit he dies so to do that we'll go ahead and add an area 2D and we will call this death zone and then underneath there we need a Collis shape 2D and for this we will come over to our inspector under shape we'll grab rectangle and then we can just sort of stretch it out like that and drop it down there now we need a little script here for our death zone um so we'll just click this little icon right here to add a script we'll make sure this goes into our scripts folder and create now we need a a function to call when our player gets killed so head over here to node we want to make sure that we have Death Zone selected we can do body entered connecting from Death Zone and connect that's going to give us this onbody entered function now we want to make sure that our player is in a group so if we come back over to our player and go to groups you'll see I've got player in a Global Group if you don't have this just click the plus sign you can call your group Player click off global then okay and you're good to go so back to our death zone script here we will say if body is in group Player it's going to tell us that it was our player that triggered it uh we'll call the function Kill player and then we need to set up this function Kill player and we will leave it blank for right now so the Next Step here is to pop over to our scene I think we want to put a checkpoint right here so let's go ahead and create a new scene we go other node we'll call this an area 2D we'll name it checkpoint add a child node to that of a collision shaped 2D and again we will go to our inspector select our rectangle and let's just make this you know kind of like that so the player can pass through it last thing we need to do is to add a marker 2D and this is really similar to the portal tutorial that I did so if you haven't seen that check it out uh and we will call this respawn point and we'll just go ahead and drag this over here so that our player will respawn a little bit past the checkpoint you can go ahead and save this and we'll say save it as checkpoint in our scenes folder now we need to add a script to this as well and we'll make sure that we put that in our scripts folder and call it checkpoint so again we want to register the Collision when the player collides with this so we'll come over to node make sure we click on checkpoint and body entered so if body is in group Player and what do we want to do we'll hit pass for right now so we don't get an error and so here we're going to want to set this to the player's last known location and for this we want to hop back over to our scene and we're going to create a new node and this can just be a regular node and we'll call this checkpoint manager now check Point manager is going to need a script so we will add a script to our scripts folder open and create and this is going to need a couple things this is going to need a variable for last location and a variable for player and then for our ready function here we want to set actually we need to grab the player first and because this is the same level as our player we need to go up one we'll say get parent get node player and then we need to set last location equal to player. Global position so this should give us the world space position of our player and set our last location when the scene loads so now our checkpoint manager knows if the player falls into a pit and dies before they've hit a checkpoint just take them back to the origin point where they spawned so now in our checkpoint script we're going to need to get our checkpoint manager and we can do that in our ready function here where we can say checkpoint manager equals and we know this is level right above our checkpoint in our map so we're going to put the checkpoints in the checkpoint manager so we want to go up one up to so that's going to be get parent get parent get node and then we're going to say checkpoint manager so if our body enters our checkpoint we want to set our checkpoint point manager. last location equal to the respawn points Global position so now when our player goes through our checkpoint we're going to set the last location to whatever the respawn point to that checkpoint is so let's go ahead and drop our checkpoint into our scene here and we can move it right here and what this will do is when our player goes through the checkpoint you can kind of see the respawn point right here it'll set his last location to that so that when he dies he'll go back there go ahead and save and then in our death zone script for Kill player right we're going to need to again we're going to need to grab our checkpoint manager code so we can just copy and paste from over here in a checkpoint because we need the same thing so we'll grab this and we'll paste this in our ready function so now we have access to our checkpoint manager right we're also going to need access to our player which was right here so we're just copying this over into our death zone so we have access to everything we need and then when you kill the player we're going to set player. position equals checkpoint manager do last location now one thing to realize here is that our death zone and our checkpoint are not at the same level so we should be able to get rid of this.get parent and only have the one there and that should work all right let's test it out and see if we get any errors and no errors awesome I love it so there you go so when we fall into the death zone it's going to respawn us right here because we passed through the checkpoint and you could easily add a Sprite to your checkpoint if you wanted it to be visible maybe trigger an animation when the player runs through it but that's the basics of how you set up a checkpoint in good 4 thanks for watching guys
Info
Channel: swydev
Views: 304
Rating: undefined out of 5
Keywords: gamedev, game dev, godot, godot 4, godot tutorial, make video games, game dev tutorial
Id: DfXPWmoHiLs
Channel Id: undefined
Length: 8min 31sec (511 seconds)
Published: Thu Jun 27 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.