New Rooms and Refactoring Exits (Godot Retro Text Adventure Tutorial #13)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

I just wanted to say this series was really helpful when I made a game for a game jam. I ended up using a .json database for rooms and items, but this was immensely helpful nonetheless.

👍︎︎ 2 👤︎︎ u/HeroRobb 📅︎︎ Dec 14 2021 🗫︎ replies
Captions
welcome back to the godot retro text adventure tutorial it's been a second since i've recorded a video for this series but i'm excited to get a couple more out there just to round out this tutorial series and make sure that you all have everything you need to make your own retro text adventure games and expand on this tutorial as much as you might want as a quick note ever since godot 3.4 a new native class has been added to the engine called room so previously in this tutorial we've been using the class name room to refer to our rooms in our game we're going to have to change that now because in godot 3.4 that's a reserved keyword so in order to do that you'll see i've got room up here you'll see this air popping out everywhere the classroom shadows and native class we're going to have to change it and the way i'm going to do it and you can give it whatever name you want but i'm just going to call it game room instead of room so i'll change the class name within our room script so it'll be game room you're also going to want to change your scene name from room to game room and then you're also going to want to change the references to room everywhere else in your code you can also change your room script's file name so just as a heads up we're not going to get to that in this video because i recorded this back in 3.3 in the same with the next video but just wanted to point this your way so that if you're seeing this error you know how to fix it so toward the end let me go through the changes i've made off camera just to expedite this video a bit and it's really just adding rooms that round out my own game's world so i've kind of talked a little bit you know i had the shed room and the outside room just kind of rooms that were placeholders but now i've kind of i've finished all the rooms that i want and have made a plan for an actual story an actual sequence of events for a game so the way it's going to work now is you wake up in a shed down here you can see um you're going to start off in shed and this shed is going to be behind and in and so you can go up to the back of the in you'll see there's a door to the in there but it's going to be locked and so you can actually this door north into the inn's kitchen to the back of the inn is locked but we're actually going to add a new a new exit key which is path so you can say go path and there will be a path from the back to the end that takes you to the village square from the village square you can go to the indoor enter the inn there will be an innkeeper there you can talk to to get some valuable information you can also go to the inn's kitchen and unlock the door at the back of the end so you can move freely between the back of the and the in itself so we'll keep our locking and unlocking functionality there and keep our item using but i'm also going to add npcs in a future video where we'll just have a way to talk to an npc and they'll give you some basic information but it's not going to end there we're also going to have a way where you can win the game just again trying to provide a small win condition where you can win the game by going out of the town and escaping or leaving the town and getting out to the forest except in your way is a guard who won't let you go through unless you recover something he lost and so we're not only going to add npcs you can just talk to we'll add a give command where you can give an item and doing that will unlock something for you it'll be like an unofficial quest system so that's all we're going to add in the next few videos but i just wanted to go through and show kind of the progression of the game that i've developed here and that'll make more sense for the changes that we'll make in the rest of this video so what we actually want to do in this video is a couple things we want to rework our exit system a bit so that it's a little bit more robust and we also want to provide ways where we can override the names of exits so we can define custom exits in both directions that the player will type in to move between them so in order to do that let's go to our room manager script here and you'll notice i've replaced all of the code that i used to have here with code that matches our new game layout so you'll start in the shed because it's first in our room manager you can go west to the back of the inn at the back the in you can go along the path but right now the way that the game is set up this is going to throw an error because if you go to our room script you'll remember we only had the cardinal directions fortunately our system is really easy to add new ones to so that won't be an issue but we'll have to do that so we'll have to add a path key here and then from the back the end you can go to the village square the village square will have three different exits to the in to the gate and then also to a field where we'll recover the guards lost item if you go to the indoor you can get inside the inn if you go inside the inn you can get into the kitchen and then from the kitchen you'll see we have this connect exit lock to the back of the inn and we'll also have the key in the kitchen there just to make sure that's working and then you'll also see that our gate to the north of town has a locked exit to the forest we won't actually provide a key to unlock this we'll unlock it by giving the guard back his item but what would be really cool is we don't always want to have directions you know we can make instead of saying west east north it's just hard to remember for the player so having something like path or i had for the indoor here you can go inside or outside that makes it really easy and clear from the name of the direction what the player is going to do but what if we could have even better ones there where it wasn't the same both ways so here we can make it so that inside automatically has the reverse of outside which is nice it would be great if by default the game would try and provide a matching exit on the other side but it would be perfect if we could have an easy way to override what the the reverse exit name would be to make it nice for the player so that way if we wanted to have the exit from the gate to go out into forest be forest but then back to be gate but we didn't want to lock it in so every time we had a gate or forest in our game it would have to be these two we wanted to provide an override well we could do that pretty easily by just changing some of the code in our room connection script so that's all the stuff we're going to do is just beef up how our room exits work in this video so first let's add path and inside because these are two new keys for our exits that we have so if i go into our room script which is where we do the connections i'll do path first so i think if we do path we really just want and let me just copy this line here so i'm going to add a new key for path and i think what we really want here is it to just be path on both ways if you go from once one part of the path to the next room you can take that same path to get back so i think just having path on both sides of the exit here makes sense but let's also do inside and outside here so we'll see if i do inside then we can do room exits and change this to be outside so if you are inside you'll go to the outside on the flip side of that and remember we had to do west and east and north we have to do all the opposite directions above so we'll do the same for outside here just so that if we ever add a room in the future that is going from outside to in and not inside outside the game will handle that automatically so this is really nice we can run our game and make sure it works and we're going to have a default system where the game is trying to help us out as much as it can by just automatically providing exits so it saves the work that we have to do so that's really great but again like we said it'd be great if we could provide an override and if you're again if you're making this game on your own you can add all sorts of things like portal you know say you wanted to have a portal or something like that that was just a portal on both sides you see how easily expandable this system is it's pretty robust and it's easy to use we just want to provide a way to override that second room name if necessary i'm gonna get rid of the portal here so before we get into that let's actually start our game and because we don't yet have a way an override name for our second room let's just comment out this line right here and we'll start our game okay so you'll see we're in a shed and also during this whole thing you'll just see a lot of text because this is a text adventure game it makes sense you know you'd expect to see a ton of text in your game so i think that's okay but it would be really nice if we had different colors and different font sizes or ways that made the text easier to read and parse without having to go back and read every word so that's something we'll do in a future video too is add some colors and a little bit of different styles to our text output just to make it a little bit easier on the player but you can see we started shed and if i say go west we'll be in the back of the inn if i try going north this is the locked exit you'll see that we can get through because right now our locks were locking the south exit of our kitchen but not the north exit of the back of the inn so this is something i did in that previous video when we did locked exits where i made a separate room one and room two is locked variable one thing we're going to do in this video is actually get rid of that and just make it so there it just is locked is an exit locked and it's locked from both sides i know in the video when we did that i gave some reasons why this was a good idea and it is a good idea you might want to keep it for your game if you want to have one-way locks but for the sake of this we'll just redo it and redo the code here so that it's locked both ways i kind of wish i would have just done that at the start but i think i thought it was a good idea to do this then which is totally fine that's how development works you iterate as you go but anyway so if we go back to our game we can try and make sure that our new movement commands work so if i go west we'll now see that one of the exits here is a path so if i say go path when i do this we should be in the village square and we should see that there's another exit also called path that will take us back to the back of the end you'll see that works we're now in the village square and we do have an exit that's path so if i say go path we're back at the back of the end so this is good it seems like our new movement things are working right and if i go back to path and then let's try the village or the in entrance so if i go east to the inn we'll see we're in the indoor and now i can just say go inside and i think go inside is way easier to understand just a better player experience than saying go east when you actually go inside so this will be go inside and now if i want to go back outside i can say go outside so there you go simple change but it just kind of makes a nicer player experience it's a little bit more clear where our directions are going and the player doesn't have to keep reading through the wall of text that they have just to remember what they need to type to go where they want to go i know that kind of thing was popular in the 80s and 90s but it's 20 21 at the time of recording and we can do a little bit better so now that we have that let's actually revamp in our connect exit function so that we can add an override for our second room's exit name so i'm back in our exit manager and it's this line right here where we want to pass in a third parameter which can be our override name for an exit so i can get rid of these two comments here because we do know how to handle path and inside but this comment here this is what we're trying to get rid of we don't want this to throw an error which you see it's throwing or whoops the error is actually just because i typed that wrong but we don't want this to throw an arrow when we run our game because it can't handle a string as our parameter here so if we go into room we'll see that in our connect exit functions we only take two parameters now the direction and the room so what we can do is add to both of these a room to override name and you can call this what you want i just want to be really specific so room two override name is kind of a weird thing but it clearly says this is the override name that you're gonna use for room two and the thing is we want this to be an optional parameter we don't want the player to have to specify this so we can just set it to something clearly optional clearly not intentional like no this you know you can change it to be something else if you want but what we'll do then below is check whether this override name is null and if so then we will not have an override name we'll try and use a default name i'm going to add this to our connect exit locked function 2 just to make sure that it's possible to pass in for locked and unlocked exits so this is good we've got this for both of our functions now but what we need to do is actually pass this into connect exit which means we need to edit the function or the parameters that get passed into connect exit so i'm going to add another optional parameter at the end here after is locked which is just going to be room 2 override name and again we'll just make this a default parameter here because we don't have to pass it in but what we'll do is in our two connect exit functions we will add our room to override name as a parameter that we are passing in into both of these and again it's just going to be the parameter name itself we don't pass it in so just again as in to explain what we're doing here we're adding an optional parameters to both of our public connect exit functions and we're going to default it to null so that the player doesn't or the developer really doesn't have to pass it in but you can if you want we're going to add that same optional parameter to connect exit of course we are always going to be passing it in because this is a private function so it's only going to get called from these two functions up above the other two connect exits so we will always be passing it in but we'll keep it as an optional with the same default value here just to make it line up with everything else and so now that we have this we can actually add some code that will determine whether we need to override our name or use our default matching name system and in order to do that we can just add a new line line 72 so after we do our things with our exit directions up here now we can say if room 2 override name does not equal null so we only want to do this if there is an actual override name that's been specified and it's not just the default parameter of null now what we can do is say it says and this is going to be the same line of code that we see down here except we're going to dynamically create it rather than hard coding in the property name or the key name for that exit so it's going to be room.exits and then inside of the brackets here we're going to pass in room to override name equals exit so what we're doing is saying we're already connecting the exit for the direction the player or the developer specifies in this line on line 71. but what we're doing down here is saying hey if there's an override name for room two we're going to explicitly connect the exit on that room to be the override name and now what we can do then is we don't want to do the rest of this match statement if there's an override so we can do is say else here and then we will indent all of this including our error at the bottom because we only want this error to be printed if the developer does not provide an override name and the name that they try and pass in is not one of our automatic matching ones it's basically an error saying hey you didn't provide an override and you haven't used one of our pre-built matching directions so you either have to add it or you've made a mistake so this is good we can go back to our room manager now and i can get rid of this comment here because what should happen is our game will recognize now that this third parameter here is actually a string key for the reverse side of our gate exit so if we want to go from the forest back to the gate this is what we have to do now we won't be able to see this while the exit's locked because we have no way to unlock it but if i unlock this and we go up to our forest so i'll do that by going west go path go north i just you know i made all these so i know where we're going now i can say go forest and the reverse of this should be go gate and now we see there's our exit go gate so on one hand we have go forest and then on the other we have go gate and this brings us back to the town gate and we're able to doing this without actually hard coding gate and forest as a pair of directional names in our room so rather than having to hard code gate and forest here within our match statement we're able to just pass in an override and let the game handle it itself so we've just added a really nice layer of flexibility for us as developers to make our game and continue making rooms going forward is now we're able to provide exits that have whatever name we want on both sides of the exits yet at the same time for most of our basic rooms where we don't care about the exit name and we just want it to be what we would expect like east and west et cetera we can just let the game handle that too so a nice layer of flexibility that keeps all of our underlying just additional help to the developer in place underneath okay so the last thing that i want to do in this video that i mentioned before is revamp our exits a little bit so that it is locked both ways and not just one so rip to the code that i wrote in a previous episode i'm not sure exactly why i wanted to do that but we'll just get rid of it here so i'm going to get rid of that room 2 is locked variable and our room 1 is locked variable and replace it with a new variable just is locked this is going to be a boolean which will default to false again just a refresher on this syntax here with the colon equals that's just telling godot to automatically infer what the type is it's the same as saying boolean except now i'm letting godot determine what the type is by whatever default value i provide so because false is a boolean value godot is going to know by default okay this is a boolean value is locked so that's what the colon equals syntax does so now that we have this we don't actually need this is other room locked function here all we have to do is just access our is locked property so if something's locked we just ask is locked that's all we have to do so this is way simpler but we are going to need to make a few edits around here so one place that we'll need to do it is in our room script so if we come over here we'll see in the connect exit function we are doing or we're looking at a room 2 is locked variable again we want this to just be is locked so now we're just going to set whether the entire exit itself both ways is locked just by the is locked variable that we were already passing in to our private connect exit function here so this doesn't change anything functionally it's just changing the variable name that we're looking at which is nice but the place we're really going to need to change things is in our command processor because that's where we're taking a look at whether the exit the player is trying to go to is locked so if we come back to our command processor you'll see that within our go function here we have a check saying okay so if the current room has an exit that matches the second word the player end or the player entered then we look for that exit and then we ask is this other room locked and then we say the x is currently locked but remember now all we have to do is check whether the exit itself is locked so i'll just change this to be the is locked property because it's a boolean we can just say if exit is locked look how nicely this line reads it's totally clear what this code is doing if exit is locked and one thing i was thinking about another way that we can improve the text to our player just to help it out a bit is we can actually change it from saying that exit is currently locked to maybe something like telling the player the direction they're trying to go is locked so maybe if we replace exit here with percent s and this is just some string interpolation if you've never used this before it's just common i think the syntax is really from c i'm sure there's a language before that but that's where this syntax comes from percent ass just means the percent tells the godot interpreter that it needs to be replaced well i guess we've already done this a bit i'll just refresh again the percent tells the gno interpreter that this needs to be replaced with a value that's later on in this line of code and s indicates that it'll be a string value so we can do percent and then here we will just say second word so it'll be better if we say something maybe like the way to the blank is currently locked so if it's to the north it'll be the way to the north is currently locked it won't work as well for something like the way to the path or the way outside is locked maybe we can actually just change it and be the way blank is currently locked so the way north is currently locked the way south is currently locked the way outside is currently locked if you have something like path so the way path is currently locked that's not gonna really make sense but i think this is still a little bit better than what we had before so let's save that and in your own game if you're frustrated with that syntax you can find a system to maybe be a little bit more dynamic about what text is printed out here but we'll just do that for now and now we're doing is we're just checking whether the room is locked and i think that might be all we have to do except in our use function here we have to actually change the code that's happening when we use a key so now we're saying when we use a key we're checking for the exit that matches that and we're checking if exit equals the item's used value and this is remember we added a used value to an item and this is how we were determining whether the item was this key was unlocking the current room we were looking at the room two on the exit but i think we can just change this exit or we can change the use value to not be a specific room but to be the exit itself so we'll come back down here in a second let's actually change that first so i can explain to you what i'm talking about so if you look at our item it's just got a use value right this can be anything and if we go to our room manager you'll see that we are setting our keys use value to be the back of the and we're sending it to be a room but what if we could just set it to be an exit itself in order to do that we'd actually have to be able to get a certain exit which we aren't doing right now but we could do pretty easily by just returning whatever exit is created by our connect exit functions so let's try doing that if we come down to our room we can just look for our connect exit and then we can just have these return and exit so what i'll do is just add a line at the end of this function let me get rid of the indents here and we'll just say return exit and now this private function is going to return an exit but it means that we have to return it from all of our public connect exit functions so again i can just say return connect exit function and these are just going to return the return values of this private connect exit function so now very easily our connect exit functions are returning and exit what that means is that in our room manager we can actually get the return value of any of these so now if we want our key to unlock the exit between the back of the in and the in kitchen now what i can actually do is connect or get the return value of this connect exit lock so i can say variable exit and now i can actually change it so that the keys use value i'm going to copy and paste line 7 here so i'll just cut it and then add it down here so now i can say the keys use value is this exit so just to recap we are getting the locked exit between the kitchen and the back of the inn we are saying our keys use value to be that exit and then we're adding the key to the kitchen so the keys use value being the exit is good we just actually need to change our command processor code now to actually look for the exit rather than the room so if i go back to our command processor and look at the line that we were looking at before we took that detour now instead of saying if exit.room2 equals or is the item's use value we can just ask whether the exit itself is the item's use value because remember we changed the use value to line up with the exit so we're just checking now does is this the exit that the item is meant to unlock and if so we change the exit not just room two is locked but now just exit is locked to be false so there we go we now have code that is actually unlocking an entire exit from both sides which means the exit is going to be locked from both sides if you lock it and i think for most games this is going to be a better system than the way the code was before so sorry that i didn't do this at the start i i definitely had some good reasons for it but just in retrospect i think this is better and i just wanted to show for all of you who might want to implement it like this how you might do that now there's one other slight thing i want to change and then we'll be able to test it and make sure it all works and you'll notice that in line 97 here right under the code we just changed we have a message that printout prints out when the player unlocks something it says you use a key for example to unlock a door to the back of the kitchen the problem is that the ex the room name that's going to be printed out here is always hard-coded to room two so take our back of the in and the in kitchen example if we take our key from the kitchen and instead of unlocking the door there we go all the way back around to the back of the in and then we unlock it the text here is going to say you unlock a door to the back of the inn but you're unlocking it from the back of the ant and that's not going to make a total amount of sense i mean it's totally fine so this isn't something we need to change but again i think just for a player experience and making the game feel better luckily we have a function that gets the other room from the current room so what we can actually do in our exit you'll see we have this get other room function so in our command processor we can actually change this to be exit get other room again this is that function that we have on exit and we're going to pass in our current room which we just have that variable up there called current room you'll see that remember this is the room that we're getting exit in current room here so we'll get the get the other room from the current room and then get the room name of that room so now if we unlock our door from the in kitchen it'll say you unlock a door to the back of the inn but if we unlock it from the back of the inn what it's going to say is you unlock a door to the in kitchen and it's the same exit it's the same door that we're unlocking but for the player it's just going to make more sense and help them understand that the door they just unlocked where it goes to so i think this is a really nice change that'll just make it easy and more flexible for us to keep adding more locked rooms going forward alright so let's test it out and make sure it works so if i go west now i'm at the back of the end if i try to go north it'll tell me that the way north is locked so if i go into the end go path go east go inside go south sorry i've got all the all the directions memorized now we're in the inn's kitchen so this is the door that's locked to the back of the in if i say go south it says it's locked too so it's now locked both ways so that's an improvement from what we had before but if i take the key i can try unlocking this so if i say use key it'll say the key to or use the key to unlock a door to the back in this is perfect so this is exactly what we wanted but it's what we had before what we really need to test is that if i take the key back to the back of the in and try and unlock the door there that it'll give me a text that says you unlock a door to the inn's kitchen so let me run the game again and i'll fast forward to that moment okay so i went inside to the inn's kitchen and got the key and i came all the way back round so i'm at the back of the ante you'll see if i try to go north it's locked but if i say use key remember now the key is going to be checking for the exit itself not the specific room on the other side so if i say use key it'll say you use a key to unlock well it should say a key but whatever you use key to unlock a door to the inn's kitchen it's the same door but now it's telling us where we've unlocked it to where it's going so again this is just a really small change that i think makes a big difference for user experience alrighty we did a lot of small kind of code heavy things but i hope this has been helpful thanks so much for watching this episode it's good to be back in the series if you found my work helpful a like and subscribe to support the channel is always appreciated we'd love to have you in our discord server the link to that is in the description below you can ask any questions you may have there and if you do find my work helpful you can donate a coffee to me on buy me a coffee link to that is also in the description below that helps me continue to make great videos thanks so much for watching see in the next video [Music] [Music] you
Info
Channel: jmbiv
Views: 332
Rating: undefined out of 5
Keywords: godot, godot engine, 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 3.3, godot 3.4, godot tutorials
Id: 1VW5lK6BbM8
Channel Id: undefined
Length: 27min 48sec (1668 seconds)
Published: Tue Dec 14 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.