Locked Exits and Keys (Godot Retro Text Adventure Tutorial #12)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video we're going to actually add locked exits to our games and implement keys that will actually unlock those exits so here if i'm in a room and i say go north you'll see that it is currently locked but if i take a key and then i use the key you'll see that it unlocks the door to a shad and so now if i go north it'll actually let us go through we're now in a shed we've unlocked the door so that's what we're going to do in this video it's going to be amazing let's get started so in our last video we added a key that the player could pick up and take or take and drop but we didn't actually do anything with it we don't have locked exits yet so that's what we're going to do finally in this video so the first thing i want to do just to demonstrate um or to use for making locked exits first is add a new room to our game and i know our room system here our map has been pretty lame with just two rooms and unfortunately we're not going to flush it out all the way right now but we will add one more room so i'm going to click on our room manager here and then i'm going to hit the instance button and i'll find room so we'll create a new room and what i'm going to do is just make this a shad and so as i'm as i'm calling this shed room basically what i want to do is have our house that the player starts in then they can go outside to the east and then to the north is a shed so i'm going to select and move our shed room to the north of outside here and we'll start giving it a name so a shed and remember we're going to do all lower case here because that's the convention we'll be doing and this is just going to be a dusty old shed used for storage okay so now we've got a shed room which is awesome so what we want to do is currently we have our key in the house i want to move this to be outside so the player can find the key outside somewhere and then use that for the shed just to kind of demonstrate the different controls we have so if we come into our room manager here you'll see we're doing a bunch of stuff with our house room here what i'm going to actually do is move line down wow move line nine down one and changes to the outside room so it's gonna be our outside room that has the key and then i'm also gonna connect and exit for our outside room so outside room that connect exit and this is gonna be to the north because remember we placed our shed to the north of our outside room and we're going to connect our shed room okay so now we have our house outside and the shed and it's all connected and remember i'm connecting rooms from the inside out where the inside is where the player starts so we start at the room the player starts with and then move outside so house to outside to shed that's the order i'm connecting just to make sure we're not overriding exits so if i run our game just to make sure this works we'll see that there's no items now in our house so if i go east we'll see there is a key and look we've got two exits now we can go west to go back into the house or north to go into the shed if i go north we are in a shed amazing it works and this is a nice testament to how easy it is to add rooms to our game using the system we've created so it is nice to see that and it's nice to see that when we have multiple exits things work as we would expect them to so all in all this is going really well the only problem now though is that our shed is not locked off the start we need to make it locked so we need to have locked exits and in order to do that we're going to come into our room manager here and we're going to modify our connect exit function or make a new function to create locked exits so in order to add locked exits what i'm going to do is come into our exit here and you'll remember that we have a room one is locked and a room two is locked variable i'm not totally sure if i gave an explanation for that in the last video but basically the reason i wanted to do this is so eventually maybe in your game you want it where both sides of an exit could be locked so for example you could have a key that lets you go from a south room to a north room but you would either need another key to go from the north to the south to go back again or it just could be a one-way exit so that's why we have both of these so that you can only unlock one way for example and can't go back another way but for our purposes for this video where we just want simple locked rooms all we're going to modify is this variable the room 2 is locked so what you need to do is just unlock room 2 will only rot lock the room that is being connected to if you wanted to simplify this you could get rid of both of these variables and just have a exit is locked variable it's probably easier but again i just wanted to show this as an example in case you didn't want one-way exits or et cetera you can modify this in your game as needed anyway so we've got this room to is locked variable and what we need to do now is come into our room and look at our connect exit function here now we've got this one big function that is doing all the connecting of our exits and i think what we want to do here there's a couple ways one is we could add a bunch of different parameters here so start adding a is you know is locked parameter etc and that would we could keep doing that every time we have a new situation for a door like is one way is locked whatever but that's going to get overwhelming pretty quickly if we just have all of these different parameters that we might not need at a given moment so what i want to do is have a private underline connect exit function that we can then make a bunch of other public connect exits connect exit functions that have specialized features so like connect exit locked connect exit normal et cetera and they all call connect exit under the hood and pass in these parameters but that way the player does not have to worry about or the developer doesn't have to worry about passing in all these parameters in the correct order and if that doesn't make sense you'll see what i mean in just a minute here as we get started so in order to do this let's make a new function above connect exit i'm going to say function connect exit locked and this is going to take the same parameters that our connect exit does so it's going to be direction which will be a string and a room which again remember we're not typing just to prevent i think we're preventing circular dependencies with that and we don't need any other parameters for this and what i'm going to do is say connect exit and i'm going to say direction room we're just passing through here but what i'm going to do is turn connect exit into a private function now in godot at least in godot 3 guido 3.3 is what i'm using but any good o3 until the new gdscript updates in godot 4 you can't actually make a function private so the the way you designate a function as being private is by adding a underscore before it this kind of tells the engine hey this is private outside things should call it and you'll see that's convention used all over the engine there's a bunch of built-in functions that have these underscores in front of them basically saying hey don't call this somewhere else but it's really just a courtesy an outside script can call these functions but you're asking the engine or you're asking the developer basically not to do that so again this isn't truly a private method or a private function but we're going to use it like that in respect as developers the underscore here as best as we can so now connect exit is a private function we're not going to call it anywhere else and so this is going to need a bunch of parameters that can handle basically any kind of exit that we might need so what i'm going to do is now pass in an is locked parameter this is going to be a boolean and we're going to default it to false so it will default to unlocked and now what we can do here is first we have to change our connect exit call in connect exit locked to have the underscore and now what we can do is actually pass in an islocked parameter which is going to be true and remember whenever we're calling connect exit locked we obviously want to lock the exit so we can always pass true in here and now we also need another function since this is connect exit is now a private function and we don't want to call it outside or anywhere else we'll get in we're going to need another function that tells it to create an exit that is unlocked and this is going to be a public function so we'll just say connect whoops wow that was terrible connect exit unlocked and this is going to take the exact same parameters i could have just copied them but direction in a room and we're going to do again just pretty much the same thing connect exit here direction whoops direction room and then remember we have a default value here for false so we don't actually need to pass in anything for is locked but just for the sake of being explicit i am going to pass in a false since this function is called connect unlocked so it's just making it clear if someone looked at this hey we are not locking this exit so now we've got these two public functions that we can use to create exits that are locked or unlocked respectively and we don't need to call this connect exit one anymore now we haven't actually changed the code of our underlying connect exit function to actually handle locking doors or not so that's what we can do right now and remember looking at our exit script here i said we're just gonna handle for now locking room two this room two is locked again if this is confusing you can just change it to just have one is locked variable but for now we're just gonna handle the room two is locked so in our room what i'm gonna do is say exit dot room two is locked and we're just gonna set this equal to is locked so whether the second room is locked is just gonna be dependent on what uh whether the is locked parameter we're passing in is true or false and so now all of a sudden we've got this underlying connect exit function that can handle locking or unlocking rooms etc i can do whatever we need but we've got really easy to use and well named you know anyone who saw a function called connect exit unlocked would instantly know oh it's creating an exit that's unlocked and so we've made our developer experience better because developers don't have to worry about all these different parameters here and by developer i mean future you when you come back to your project and you've forgotten how your code is set up if you haven't experienced that you will but anyway so we've made this a really nice interface and we're good to go we're actually locking exits now so what we need to do here is actually change our house room to use connect exit unlocked and then our outside room to connect exit locked because remember we are locking our shed so now we have rooms and exits that are locked and unlocked now we need to actually implement using our key to unlock them so we're going to have to add a new use command to our command processor before we get started on any of that though first we never actually implemented uh preventing the player from moving to a locked room so if we go to our command processor and look at our go command here we'll just see the way we're doing it now is we're saying hey if the current room has an exit then we go into that exit we actually need to check and make sure that the room is locked before we let the player go through or make sure it's unlocked otherwise the player can't go through and if it is locked we'll print out a little error message and so in order to do this we're going to do something similar to this get other room function we created on our exit and we're going to let the exit itself determine if we're trying to go into a locked room or not so in our exit we'll come over to the script we'll add a new function called function is other room locked and again we will pass in the current room because that's really all we have access to right now and then we're going to do something very similar to what we did in the other room so we're going to say if current room equals room one what we're gonna do is return so we'll say return room two is locked because if we're currently in room one we only want to go through to room two if it is unlocked and we're going to do the same thing for the other side of this equation so alif current room equals room two we will return room one is locked and if neither of these are the case which again it shouldn't happen we're just going to do we're going to print out an error and just say actually let me just copy this line from line 28 down on the other room we're just going to return an error saying the room you tried to find is not connected to this exit again it shouldn't happen and we'll just return true and say if the player tries to go to an invalid exit we'll just say it's locked to prevent them from going through but again we shouldn't get there but just as a quick explanation again what's happening here is say we are outside right in our outside room and we try to go to our shed well if our current room is the outside room we want to see if the shed is locked which it will be but say we get to the shed from some other path through our world map in that case if we're currently in the shed we and room one is not locked if the outside isn't locked we want to just be able to move through we don't need to unlock it and i think it makes sense because if you are inside the shed even if the door outside is locked you can unlock it from the inside so it should make sense um but again feel free to cut or to simplify this in your own game and what you might also want to do in your room is have a connect exit locked both ways where remember here we were just doing is locked and we were just locking the second room again you can make another function that locks both sides of the room you can customize as needed but we're just doing room two for now so now we have our exit and now we have this is other room locked function which is going to actually return a bool so let's add that return type here and so this is good now we can actually call this from our command processor so if i go over here we can say now remember we're getting our exit here so we can say if exit dot is other room locked uh we're not getting auto complete so let me just come back here and copy this is of the room locked we need to pass in the current room so we'll say is other room locked and again we're passing in current room so if it is locked then we are going to instead of returning this response we're going to do an error message and just return you can't go there that room is locked so we can replace this and say that exit is currently locked so this will tell the player hey that exit exists but it's currently locked and you can't go there so if the room's locked we return this error message otherwise we actually let the player go through so we are not letting the player go through if the exit is locked okay so now that we've got locked exits that will prevent you from going through let's actually give it a try so if i run our game we should be able to go east not locked now we're outside now if i try to go north we'll see an error message you can't go north the exit is currently locked and if i go west we can now go into the house so things are working as we are as we would expect we are not able to go through locked exits now we just need to make sure that we can actually use our items to unlock and exit okay so we are almost there and before we actually add our use command to our command processor we've got to run into item and actually provide a way for item to tell what using it does and the way we're going to do this for now is very simple and probably not the best way but just because all we have are keys there's only one type of item right now it's going to be totally fine and as we add more item types as we add more item types later or you do in your own game you can find a better way to do this but for now we're just going to say variable use value and this is going to be null we're not going to type it we're not going to do anything we're just going to keep it as a generic use value that an item can set to be literally whatever it wants now for keys this use value we're going to set it within our room manager to actually be the room it's going to unlock and we're going to do like our checking to make sure of what type of item it is within our command processor so this is going to make sense in just a second but for now we need to come into our room manager and here we are going to say for our key we're going to see k dot use value and this is going to be shed room because our key when you use it is going to unlock our shed room and we know it's going to unlock something because it's type is a key and like i said we're going to add this functionality this checking into our command processor for now ideally like we've done with exits we want to move that out of our processor and into our item as much as we can but again sometimes the best way to do things is find a way that works and then make it better later on when you have a better idea of how you're going to actually need to use it we just don't have a great idea of how items are going to play out right now since we only have one type so we'll just do the simple thing so with that said let's add our use command and what i'm going to do actually just after inventory i'll say function use and again same as all of our other commands it's going to return a string and the way we're going to do this is it's going to be pretty similar to drop so first uh we need to check and make sure it has a second word because you have to use an item and then we are going to loop through all the items in our player's inventory so i'll copy all these and so we're going to loop through the items in the player's inventory and then if we find the item that's going to be used then we're going to do something with it else if we get to the end we don't find it we're going to return this so we'll say you don't have that item you can't use it etc oh and i totally forgot uh use we need to pass in our second word here as a parameter okay so now we're good now we are we have a use command it doesn't do anything but we can actually start adding that functionality and so in order to do that there's two things you want to do the first is actually get what type of item it is so we know that if we get within this if statement we've found the item and the player has it and so if we have found it we're going to match and we'll say match on item.item type which we know is a property on our item since we go back to item we see item type so we're going to match an item type and we're going to say if types.itemtypes.key so if it's a key we're going to do something if it's an invalid type which is any other type we're going to well for now we can just return error try to use an item with an invalid type okay so we'll we can hopefully we should never get to this error case but we'll just have it there just in case because we need to return something now if it is an item type of key we need to actually unlock a door now this is extra complicated because we need to make sure that the exit or the room for which this key unlocks is actually connected to the room we're currently in so the way we're going to do that is take a cue from our go command where we're looping through all of our current exit or that all the exits of the current room here we're getting all the keys but remember it's a dictionary in our actual exit resources or exit values are the values of that dictionary not the keys so we're going to grab this and instead of doing keys though we're going to change this to be values which is another godot function that will let you get the values of a dictionary so we're going to change this to be values so now we're gonna be getting all the values but we don't want an if here we need to say four exits in current room dot exit stop value so we're gonna go through all the exits and we're gonna say if exit and remember this is one of the reasons why we're specifically focusing on room two of the exit not room one so we can say if exit.room two equals item.use value so okay before we go further on this let's explain what we're doing here so if exit dot room two again remember when we lock something in our room manager when we connect exit locked it's locking room two which is shed room so it's gonna set shed room as room two and be locked and then in our command processor item.use value remember for key items our use value is the room itself so we're setting the use value to be the shed room and we're also locking the shed room in our exit so what we're going to do here is say if exit.room2 which is the shedroom equals item.use value which will also be the shedroom if we are in the outside room if we're in the correct room where we can unlock this then we're actually going to unlock it otherwise we're just going to keep moving through so if this is the case then we're going to unlock something if this is not the case we're just going to get to end of our for loop and we're going to return that item does not unlock any doors in this room and so what this is going to say is tell the player hey you tried to use a key to unlock something but this is not the right room to unlock it in so it's going to give the player a helpful message but it's not going to unlock anything now this is kind of complicated there are better ways to do this which again experimentation on your own will help one way to make this better is if we actually started implementing third words into our command processor so that way we could say use key north for example then we wouldn't have to do this lookup here we could just check the specific exit that the player enters it would make things a lot better and give the player a bit more agency because they would be trying specific doors i think it's a better player experience to do that and i'm hopeful that we'll get to the third words in our command processor before too long but for now again we're just going to do something and try and get something that works so that's why we've got this complicated loop here so if exit.room2 equals item.use value we've actually got to do two things we need to one delete our key if it gets used and two unlock that exit now we don't have any functions right now to unlock exits well on our exit script so we need to add that first so what we can do actually i mean we could use a function for now like what we can do is just say exit dot room two is locked and we can set this to be false again we're assuming that room two is locked here um there's a chance it might be but again we'll assume that we don't have to do that check now and so what we also need to do now is to remove that item from our player inventory in order to do that we can use the function we used for our drop command which is player.drop item which will remove it from the player's inventory and you might be asking okay and don't we need to also delete that item so it doesn't take up any memory well remember this is a really cool thing about using a resource for our item is that resources are reference counted by godot so when there's nothing left in our game that references them anymore they'll get deleted or collect or garbage collected automatically so we don't need to manually delete them and once it's out of our player's inventory then it's not being referenced in that array and so it's not going to be referenced anywhere else in our game so once we drop it the only place it'll be referenced is as item within this function and once this function's over all of a sudden it's got no references anymore and so it's going to get automatically deleted so we don't need to manually delete it we just need to remove it from our player inventory and godot will handle the rest under the hood so here we are we have a really complex super nested function i hate doing stuff like this but sometimes again it's just this is just how we're doing it for now trying to get something that works so we have a use command that we're going to go through and all the items in our inventory until we find the item that was specified and then we are gonna if it's a key we are going to look for the exit that matches that key's value and if it's there we are going to unlock the room so there we go we've got all this now the only other thing we can do here and that we should do actually is return a string telling the player they unlocked door so we can say return you use and then we could try and make this fancy by saying you use other percent s and then this is going to be the name of item to unlock a door to the and then what we're gonna do is just um or here we can just say two and then i'll do another percent s again we will you know we can make this better later on but we're just gonna concatenate in some strings here so the first one you use blank is going to be the item name so we'll say item dot item name and then the second one this the unlock a door to whatever is going to be the room so or the other room so we'll say exit dot room two dot room name and we'll just make sure it's item name and room name so an item it's item name and then in room it is scroll to the top room name okay so we're using the right variables here and now we should see if we play our game we should be able to test it and make sure it all works sorry we test to make sure it all works right after we actually add it to our command processor i totally forgot we haven't done that so we scroll up to our command processor we're going to need to add our use key here or use command so same as we've done before with everything else whoops get that and then we're just going to say return use second word just like all of the commands okay now that we've got that we're actually ready to run it and see all right so we are in a house if i go east we'll see that we are now outside if i try going north just again to make sure and test our exit is currently locked let's test all of our error messages here so what i'm going to do is say take key we'll pick up the key i'll go back west and try and use the key just to make sure we've got a good error message here so if i try to use the key within our house it says that doesn't unlock any doors here which is perfect that's what we want now if i go back east i believe if i try and say use key now it should say you use key okay it should be a key but whatever you use key to unlock a door to a shed so it's not the most grammatically amazing error message but it works it tells the player they've done something so that means the shed should be unlocked so now if i try and say go north we're in a shed and if i go back south i can go north again the door should remain unlocked perfect wow so it was quite a lot of work and you know there's some stuff we're probably gonna have to change as we expand our game but we have doors that are locked and we can tie keys we can tie items to those doors to unlock them so now we have a system where we can lock doors in our game and we've got a really awesome room and item system we can move and expand on going forward thanks so much for watching everyone this is a lot but i hope you enjoyed it and learned a lot if you did a like and subscribe to support the channel is always appreciated joining our discord server is a great way to get involved with the community and ask any questions you may have and if you do find my work helpful supporting my channel and buying me a coffee is much appreciated and helps me continue to make great videos thanks so much everyone i'll see you in the next video you
Info
Channel: jmbiv
Views: 1,030
Rating: undefined out of 5
Keywords: godot, godot engine, godot 3.2, godot tutorial, godot 2d, how to make a game in godot, godot for beginners, game dev, how to godot, godot game engine, godot text adventure, godot text adventure tutorial, godot text, godot input, godot user interface, godot control node, first godot game, godot beginner tutorial, gdscript, godot items, godot beginner tutorial 2d, godot room transition, godot room, godot scene transition, godot scenes, godot how to make a 2d game, godot 3.3
Id: FFFZY2qfuc4
Channel Id: undefined
Length: 25min 53sec (1553 seconds)
Published: Mon Jun 07 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.