C# Godot 2D Platformer | Creating a Scalable and Reusable Door Key System

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys this is mitch with fine point cgi and today we're going to talk about how to create a scalable door key system inside of godot so we are going to talk about how to create a key that follows the player and has a specific unique identifier we're going to talk about how to create a door that handles that specific unique identifier and we're going to go ahead and add in some code inside of the player so that way they can house multiple keys so that's what i have in store for you guys today so let's go ahead and get started okay so the first thing we're going to do is we're going to do a little bit of planning generally i don't do this on recording i do usually do this by myself right off off screen but i thought it would be cool to kind of show you guys especially with um some people asking about uh planning your code beforehand i thought this would be useful so what we're going to do is we are going to create a door that's scalable right so we are going to need a a door class here so we'll go ahead and drag this in and we'll just say door right and the door is going to have to be opened it's going to have to check keys right and it's probably going to need to have some animation and the key itself right is probably going to need to follow the player i imagine and it's probably going to need to be able to house some kind of unique identifier and it's probably going to need to handle player collision so we'll probably need that as well so how is this going to work well first let's take a look at our key section here right so we'll say hey what does a key need to do so let's say player collides with key right and it's going to need to come down here and follow a player right so it's probably going to do that by getting player position minus my position right so player collides with the key it's going to follow the player right it's going to get the player's position minus my position and then it's going to move right and it's probably gonna bounce between these two states here right it's probably just gonna go like this and it's probably gonna bounce go here and then it's probably going to bounce between these two positions like this so that's probably what's going to go on with the key now with the door what the door is going to have is the player is going to collide with it and what's going to happen is the door is going to check does the player have a key and if the player does does it match up with what i'm looking for so we're gonna say hey does player have the key i'm looking for right and then it's going to go ahead and remove the key the player has oops and then it's going to go ahead and open up right it's going to probably play that animation so we can drag this down drag this down and drag this down right and if the key doesn't have what the player is looking for then it's going to probably do nothing right so actually i'll just go ahead and grab an end state or let me see end there we are we'll go ahead and grab an end state we'll just put it right here and we'll say okay now the door has been open it's going to end and if the player doesn't have the key that we're looking for or it doesn't have keys we'll go ahead and just end right we're not going to do anything we're also going to need a way to delete the key so what we can do is remove the key this is probably going to delete the key right so we can just drag this in and just say delete key right so delete key object from tree we'll just drag this down and we'll drag this up and we'll go ahead and get rid of this open door animation there we go so that's pretty much all we're gonna need i think oh and i think we'll need to modify the player but we don't really have a player section here but we're gonna need to modify the player so they can have a list of keys which makes sense so i think yeah i think that's pretty much what we need so let's go ahead and get started coding okay so the first thing that we need to do is we need to go ahead and make our key so what we can do is we can go ahead and go up to our game manager right click add in a child to node and let's add in a node to d and now that we have our little node 2d here let's go ahead and rename it and call it key simple enough and let's go ahead and right click add in a child node add in a sprite and let's go ahead and for our sprite let's go into our platform metrovania asset pack or whatever asset you want it to be miscellaneous sprites we're going to scroll down and go to orb here so we'll go ahead and fetch this drop it in as our texture and if we focus in on that by hitting f we can see there's our little orb and you can see that our orb is a little compressed here so if we go into import and change this to uncompressed and then re-import it that didn't work so we're going to go ahead and do 2d pixels and import it and there we go i think there was something is it lossless oh it's lossless that i was thinking of not not not uncompressed so there we are and now it's got a nice 2d aesthetic that we're looking for and then we need to have some kind of collision right so we need to tell the key that the player has collided with it so they can pick up the key right so what we can do is we can right click add in a child node and put in an area 2d and then inside of that area 2d we can we need to add collision so we'll add our collision shape here 2d and every collision shape 2d needs a shape so if we come over here we can click on empty and change it to a new circle shape and that will give us a collision we can make this as big as we want but i think the default size is a pretty good size now that we have all of this set up we can right click our key and we can go ahead and make it into its own scenes let's save branch as seen click on that and call it key tscn and hit enter perfect so we basically have everything that we need to create this key so now what we can do is we can go ahead and open up that key scene so if i minimize those two come down here and let's go ahead and grab our key.tscn now this project is getting a little bit cluttered so we might have to do a little cleanup but let's go ahead and double click on that key let's right-click on our key and attach a script and let's go ahead and attach a key.cs script so go ahead and hit create and it's going to go ahead and open up visual studio code providing that visual studio code is not feeling unhappy today which in my case it seems like it is so i'm going to manually open visual studio code and let's navigate to my project here so open folder okay so now that we're inside a visual studio code what we need to do is we need to figure out what exactly you want to do with this key so the first thing is the key needs to follow the player right we don't want a key that doesn't follow the player because then it's basically useless right uh we need a key that when the player enters it right enters its collision area it will start following the player because we need to notify it right and we need to have the key have a unique identifier that we can use to keep track of what door that key opens because not all keys will open all doors right now i know you could use grouping to do this but i prefer to do it the code way to me it's a little faster and we're not interacting with the godot system as much we're more interacting with c sharp which generally is faster so what i'll do is first we'll come up here and we'll say hey uh we need to move right so private vector two velocity is equal to a new vector two and we'll go ahead and initially say that that vector two is zero zero and then we need to determine if we're following the players so we'll say private bull following player and currently we're not following the player and bulls default to false so we're good there and then we need to determine if we're picked up right because we don't want to allow the player to pick up multiple versions of this key off the same key right so we have to say and we'll say private bool picked up right so basically we're saying hey are we following the player are we picked up and um the last one that we need is what door we're going to open so we'll make that public so we'll say public string door to open and we'll go ahead and set that to nothing and we're going to need to export that and the reason why we want to export this is so that way when we instance a key into our scene or if we place a key into our scene we can go ahead and just set whatever door we want to be open we can just type in whatever we want and it'll go ahead and open that specific door now what we need to do is we need to go ahead and say hey when the player enters here let's go ahead and follow the player right so we'll come down here and we will need to set up kind of like a collision system right so if we come into godot we go to our area 2d we go to our node and we make sure we're on our signals here and we say hey on body enter let's go ahead and connect that let's copy this little receiver method and you can see that we are linking it to our key so that's perfect go ahead and hit connect and now if we go back into godot we can type private void on body enter and we can pass in an object here we'll say body now from here we can say hey first are we picked up right so if we're not picked up then we're gonna want to do some additional checking here okay so we can say okay we're not picked up if body player controller then we want to go ahead and do the rest right so we want to say hey following player is equal to true right because we want to follow the player now from here what else can we do well we want to say that we're picked up for one and now from here we want to follow the player right so let's go and create some following code now i don't want to do process i want to do physics process i want everything to be handled on the physics engine instead i prefer it when it's used more on that versus process which is kind of set to your frame rate if that makes sense whereas physics process is a very set time frame if that makes sense so we'll say is public override because we're overriding the physics process function void underscore physics process then we want to say all right if we are following player then we want to go to the player right so we'll say velocity is equal to game manager dot player dot global position minus my own global position now the reason why we do this open it's mad at me oops equals not equal equal now the reason why we want to do this is what we're doing is we're saying hey game manager dot glo player dot global position so get the position of the player get the position of my key and then subtract the two so that way we can say how far away are we from the player and then we can move towards the player right so what we can do now is we can say okay now move to the player so we can say move and move and slide velocity multiplied by 5 f comma vector 2 dot up and what that's going to do is it's going to say all right so we need to move our distance away times five so because the distance away can be very great or the distance away can be very small and if it's very small we don't want it to take forever to follow the player right i just want it to follow the player quickly but not too quickly if that makes sense now we do have a quick little move-in slide does not exist in current context and my guess is oh it's because this is a no 2d our key needs to be a kinematic 2d so let's go ahead and change that so right click our key come to change type and type kinematic 2d that's my fault so go ahead and change that and you'll see that it gets mad at us because we have no shape to collide i mean really we don't need one necessarily so i'm not going to worry about that for right now but we'll go ahead and hit ctrl s we'll go back to our key and change this to kinematic body 2d and that should give us the move and slide there we go so now what we can do is we can test this and see if this works it should in theory so let's go ahead and find out so if we go back to our test scene let's grab our key and kind of move it down here somewhere so that way the player can jump and grab it so if we run over there providing that it actually compiles sweet we jump we jump boost and you can see that nothing seems to work now the question is why are we getting our collision is what i'm wondering is gonna be my first guess let's see i wonder if it's not detecting if body is player controller let's try this first so what i'm gonna do is throw a break point here and i'm going to do a run and debug and we're going to play that in editor let's take a look at what's going on under the hood a little bit all right so we run over here we jump teleport and it's just straight up not detecting our collision let me see what did i miss i must have missed something oh it's not connected could have sworn we connected it all right so let's go ahead and connect it and let's try that again this time it should in theory work so we'll whoops and there you go now the key is following now you can see that it's following the player and it's getting directly on top of the player i don't know if i like that right and you can see the farther the player gets away the faster that key moves that's part of the reason why i wanted it to be this way was so that that key would follow the player when they get really far away it would speed right up and catch up to them which is perfect that's exactly what we wanted so awesome now i don't like how this is directly on top of the player so let's do a little bit of finagling and make it look pretty good so we're going to go to our player so players test scene and then let's go ahead and move over to our player and then what i'm going to do is i'm going to add in a follow location for our player so we'll go ahead and right click add in a child node and put in a position 2d and we'll just kind of drag we'll just kind of drag that out it's not going to let me so go here let me go to transform and click on position 2d transform pull this back some we'll say about here i think here would be a good spot for a key to be floating right and we will call this key follow location i'm going to go ahead and copy that so i have that and then i'm going to go ahead and save my player we're going to go back into our key here and we are going to say instead of looking for the player global position we'll say dot get node and we will go ahead and grab a position 2d and we're going to go ahead and grab that dot global position and let's see if that is better so we're going to come over here we'll jump we'll grab the key you can see it's now following the player where we expect but if we hit to the right you can see that it's still following the player but in the complete wrong direction which isn't great so what we can do is we can either move this through code right to go to this side or we can just go ahead and create a second node and i think i'm going to go ahead and create a second node so just ctrl d will go ahead and grab this providing it lets me it looks like it's not going to so go ahead and grab this we will go ahead and make this positive 10 instead of negative 10 and that's just going to throw it to the exact opposite and we will call this key follow location right and we're going to do the same thing here rename this but to left and we need to know if the player is either facing one direction or the other and the easiest way to know that is via their flip value right via their sprite animated sprite flip h value right so they can turn left or right so what we can do is we can come over here and go into our key and we can say hey if following player and if gamemanager.player.getnode right because we're going to grab a note we're going to grab that animated sprite okay and we are going to go ahead and say animated sprite dot flip h right because we want to see if it's flipped or not and if it's not then we're just going to go ahead and grab that velocity right here but if it is then we're going to go ahead and grab the exact same velocity but from the right side so go here key follow location i'm going to guess left and i'm going to guess right see if i'm correct so let's go ahead and save that and we'll go ahead and run it and let's see what happens all right so we run over here we grab our key we tab that follows correctly we rotate and there you go perfect that looks really good actually that works really well so the nice thing is if you were to have let's say an item or something like that that you want to follow the player you could definitely put that here and honestly we could put it so that this would be like an inventory or something like that that'd be kind of cool the player could just have a little thing floating maybe um but that might be something for the future for us to kind of think about right maybe we could we could uh make that happen right so now that we have that working now what we need to do is we need to go ahead and set up our so once the player picks up the key we need to tell the player that they have a key right so what we can do is we can go into our player controller here and we need to create a list of keys because what if they have multiple keys what if they have 20 keys right we don't know so we'll come down here and we'll say public list and i'm not getting auto completion which leads me to believe that i am missing yep so if we go to using system.collections.generic which is a generic systems collection library right we can come down here and continue our little list here so list there we are of keys so key keys is equal to a new list of keys there we are all right so basically once the player gets initialized they're going to go ahead and have those keys and it's going to be an empty list now once the player picks up a key we need to tell the player that they now have this key so what we can do is we can go back to our key and say hey when the player enters and actually we could probably keep that we can say hey when the player picks up the key we need to add it so we'll say gamemanager.player.keys.add this and then we will semicolon and what that's going to do is that's going to go ahead and add to the game manager the player's keys so now the player has a reference to that key if that makes sense so now that the player has a reference to that key we need to go ahead and make our door so what we can do is we can come back into godot we can go to our player test scene we can come over here and go ahead and click on our tile map here and let's just go ahead and add in a little um angle here come over here draw this out let's say and then let's flip this i believe that's s and then we'll pull this out as well and then we need to go ahead and make a door so i don't know if i have a door here so let's see do we have a door door oh door.png is that a cool door oh that's not quite the door i'm looking for uh let's see do we have a strange oh strange door closed that looks like a good one see look at that that's a cool one so what's this oh fancy fancy so what we can do is we can come over here we can right click add any child node a node 2d and we can go ahead and rename it so if we just click on it door we can right click add in another child node here and we can add in a static body because it's an object that's not going to be moving well i mean it's going to be moving but it's not really good it's a door so it's not going to be moving a lot so we can say static body 2d and then we can go ahead and right click on that it's going to need a collision shape so we're going to go ahead and add in a collision shape 2d if we focus in on that door you can see it's a beautiful door of emptiness and we can add in a cube rectangular shape and now we need a sprite for this door to look you know for to look like a door so we're going to go ahead and right click add in a child node and add in a sprite then we can go ahead and drag in our strange door closed as a texture here now this is probably a bit too big so if we grab this entire segment here and let's turn on the ghost thing so it doesn't do that you drag this over here and you can see that it is massively too large if we kind of put it in the center of that area it's definitely too big so let's go ahead and grab that sprite uh let's rotate the sprite 90 degrees so we'll rotate it and i want it to be negative 90 and we will go ahead and scale this door down some so we're gonna see yeah that seems like that'll work so let's grab this sprite let's unturn on this grab the sprite move it up so it now covers this little door here and we are going to go ahead and change our collision shape to be slightly larger than the actual door itself like that so now the player can't run past the door simple enough now what we can do is we need an area 2d to tell the door if the players walked up to the door right so we can right click the door add in a child node and add in area 2d so we'll go ahead and add that in then we're going to go ahead and right click that and add in another child node and add in a collision shape like we did last time go in here and add in a rectangular shape and we'll go ahead and make that fairly large so the player can just walk up to it from pretty much anywhere and that should work pretty well so we'll put that like that perfect so now the player can walk up to this door okay so we're going to go ahead and right click the door and we are going to go ahead and save branch as scene we're going to call that door.tscn we're going to go ahead and hit enter and then we will go and open up our door project now this should probably be zeroed out there we go so we're gonna go ahead and zero that out there we are and we will go ahead and right click our door and attach a script we're gonna go ahead and attach a door script we're just gonna call it door so go ahead and hit create and again visual studio is just not happy with me i'm gonna have to figure this out after this tutorial so we'll go ahead and open up that door script right here and we need to solve a few things so we need to determine if the player walks up to us we need to determine if that string is or if that key is valid right because they might have a key but it might be the wrong key so what we can do is we can just come up here and say hey public string door key and we'll say that that's going to be the the name of the key that goes to this door so if we go ahead and hit enter and we export that so when we put doors in our scene we can actually just name them and then it just makes sense right so from here we need to determine when the player collides with this door it needs to check if the player has that key right so what we're going to do is we'll go into our door here we'll go to our area 2d and we'll go ahead and connect that signal so we'll say hey on body enter go ahead and connect to our door and i'm going to grab this and connect it and then if we go back into our project we're going to say hey when the player enters right so public void on area 2d body enter object whoops not that object body we're going to check hey is the body a player because we don't necessarily want to run this check if the body's not a player because what if a slime runs into the door right and the player is halfway across the map suddenly it unlocks the door because the player picked up the key well that wouldn't be great right we don't want that we're going to say hey does the player have the key that we're looking for now this little bit of code is going to be pretty complicated so i'm not going to lie guys just you know we'll walk through it and i'll explain it as best as i can but we're going to be doing a bunch of link like coding so it's going to be kind of funky so we're going to say hey if gamemanager.player right so we have our player dot keys dot find all so we're gonna find that key in our player's key list and we're going to say find all and what happens with find all is it creates a variable in our case i'm going to say p right so we created a variable p for each key that comes in so what's going to happen is is it's going to grab a key and that key is going to be p or we could call it k it doesn't really matter i guess k would probably be better so we'll say k right and then we're going to do an arrow operator saying with that k do this right so k dot door to open is equal equal to door key dot count does not equal zero so what that's gonna do is it's gonna say okay i need you to run through here find all the keys that have the same name as our door key if you find one right if it's greater than zero then i need you to grab that key right so gamemanager.player.keys dot find x equals pointer right x dot door to open dot contains door key and we're going to pull back that key so what we're saying is hey if we have any amount of keys that match the thing we're looking for then let's go ahead and pull that door key and then we need to remove that door key so game manager dot player dot keys dot remove our key right it's okay then we need to delete our key k dot you free okay so we're saying effectively find a list of keys grab that one key out of that list and then remove that key and then delete the key from the scene and then we're going to say gd dot print open door all right i know that was a lot so from here we're going to say else we want to say gd dot print need key all right so if we come over here and we save our door scene and we hit play and we go ahead and we run over to that door so let's run over to that door oh i'm off my own game okay oh our door is not here so our door isn't here so i'm guessing yeah it got zeroed out because i zeroed it out so let's go ahead and move it so let's go ahead and move it over here we'll just drag it over there we are and we'll go ahead and hit play and let's try that again because i zeroed out my stuff and i made a mistake so there we are oh i'm terrible at my own game again i keep getting hit by that guy if i run up to it you can see that it says hey you need a key i don't know if you guys caught that before this started sprawling through but if we scroll up we should probably get rid of this log you can see it says hey you need a key right here so that's good so what we can do now is let's go and fetch our key so now we got our key let's come over here and it crashed oh no what did we get unhandled exception object not set instance to an object uh normally i can double click this but since i can't it's going to be on line 27. i know that and i'm pointing at it with my finger but right here um you can see it says cs27 that means that it's an issue on line 27. so we're going to go over there and take a look on line 27 it's attempting to fetch that key of door that contains door key oh you know what i think did i forget yeah this is null okay so let's just call this blue door right it's the blue door and then we'll come over here to our key let's call this the blue key right let's go ahead and hit play let's try that now oh i'm bad at my own game goodness i am very bad at my own game also goodness come on there we are so now we'll come over here you can see it deleted our key which is good and if i scroll up in our output open door so we successfully made our door work right so that's good so basically we now have everything working but we need to go into our door and move our door out of the way right so we'll go ahead and go into our door and let's go ahead and right click it and add in a child node and add in an animation player node here and we're going to go ahead and add in a new animation and call it open door and what we'll do is we will go ahead and click on our let's see i actually want yeah i'm going to go ahead and add in a child node node 2d and i'm going to go ahead and grab these two and drag them under my node 2d i think this is still hooked correct i'm going to disconnect it just in case and reconnect it just in case i don't know if that's going to be a problem but i want to make sure and then i'm going to go ahead and animate our node 2d here so i'm going to go ahead and go transform and lock a keyframe use bezier curves yes i want to use my bezier curves and we'll go ahead and pull this over here and we'll move this up whoops is my oh my sprite's not under there that would help all right so sprite i like my sprite at the top so we'll do is we'll move this here and we'll go ahead and drag this up something like i don't know we'll say minus 30 maybe let's go ahead and lock a keyframe there so you can see it's going to just go up simple enough and let's go ahead and check out our player test scene see if that's going to work there we go we can see yeah i think that'll work that'll give us enough space for the player to be able to move so that's perfect so we'll go ahead and take that okay we'll do this ctrl s and that'll bring this back down and you'll notice that this isn't great right you can see how it's kind of rendering above our level here we can either drag this up right and put it underneath our tile map which i believe we can just kind of drag this up and place it right here and now it's behind our tile map or we can actually go into our z index and change this to -1 so that way no matter what even if it's down here like it was it's underneath the level so that's just something to keep in mind by the way is this z index is super useful so you see zero and then if i bring that back down to minus one it goes behind so that's perfect now what we can do is we can go ahead and open up our controller here we can go over to our door we can come down here and say instead of open door we can just say get node animation player and we can just go ahead and grab our animation player so and then we can say dot play and i believe we named it open door simple enough so quote open door there we go so it's going to go ahead and open up our door so now if we hit play we can run over there and i'm going to get hit because i'm awful at my own game there we are and you can come over here and it says i need a key because i completely forgot my key so i'm probably going to die here and then i'm going to look like a fool all right oh goodness hi we'll do that we'll dash over here we'll jump jump dash dodge you can see now the gate is open simple enough so that's pretty much it so the nice thing about this is the key got deleted it's gone the player doesn't have the key as far as we can tell and we can go ahead and um open doors and the nice thing about this is if i were to say come into my player test scene let's duplicate this door and let's just move it i don't know over here somewhere let's say that there's a door here for whatever reason right so let's just put a door i don't know over here right and we change this from blue to green right and then we hit play and we run over here and we dash ah goodness i'm just so bad at this game all right we dash and land on here and we jump down here you can see it doesn't fetch it doesn't open our door see because we have the wrong key it won't care so that's something to keep in mind is it actually works like it works as intended which is awesome so that's pretty much it so if you guys like this video go ahead and hit that like button and hey you know if you dislike this video go ahead and hit that dislike button because i am here to make content for you now this video was a viewer suggested tutorial as with all of my videos so if you guys have any suggestions throw them in the comments below and i'm more than happy to get to them it might take some time because the the board has gotten pretty full but i'll be i will get to them but that's all i have for you guys today so thank you so much again for watching and i will see you all next time thanks [Music] you
Info
Channel: FinePointCGI
Views: 367
Rating: undefined out of 5
Keywords: c# godot | creating a scalable dialogue system, godot engine, godot talent system, godot ability system, godot skill system, godot game engine, create a database in godot, godot beginner tutorial, make a database in godot, godot dialog system, godot tutorial, godot dialogue system, godot dialogue system tutorial, godot ui system, godot, godot 3, godot 2d tutorial, computer simulation and gaming, computers simulation and gaming conference, godot engine tutorial
Id: lDbExrNu2gI
Channel Id: undefined
Length: 42min 46sec (2566 seconds)
Published: Mon Nov 08 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.