Creating Exits Between Rooms (Godot Retro Text Adventure Tutorial #8)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

This is a great tutorial for anyone wanting to make any type of user interface heavy game not just text adventure games.

Also on the same channel is great tutorial for making animated menu scenes that inspired me to stop procrastinating creating the start menu.

👍︎︎ 2 👤︎︎ u/spectrumspaceship 📅︎︎ Mar 30 2021 🗫︎ replies
Captions
in this video we're going to expand on the room system we implemented in the last game to actually allow going between rooms so we've created an exit dictionary that exists on each room so you'll see that now we start in a house and i'm allowed to go east if i try and go west or a different direction it'll give me an error saying there's no exit there and if i try and go east it'll tell me hey you've gone east you're now outside and you'll see i can go back west to the room we're just in so with that let's get started so we need to add exits to our rooms now and the way we're going to do that is to actually use dictionaries we're going to add a dictionary to each room and that dictionary is going to be the exits from that room and we're going to add some nice helpers to automatically connect one room to another and vice versa so you don't have to manually set up both sides of connection so if you go from house to outside you don't also have to connect outside the house it'll happen automatically so in order to do that let's first go to our room script and here we're going to need to add exits so i'm going to say variable exits and this is going to be a dictionary and we'll just make it an empty dictionary for now now we could make this an export variable that you would be able to edit from the editor and we might do that in the future but i think we're gonna try and do it from code right now and i know we keep kind of going back and forth on should we do it by the editor should we do it by code the right answer to that question is pretty contextual any specific decision about should you do it in the editor or should you do it in code does not necessarily have a right answer sometimes you're going to mix and match sometimes you'll stick to one sometimes you'll stick to the other i think right now i would like to have things in the editor as much as possible because again looking forward to potential future features it'll make it easier to have a visual connector a visual map to have rooms that we can edit totally from the editor without having to go to code but we're not quite there yet there's a lot of foundation we have to build and so i think if we do things via code first when it doesn't make sense to do it by the editor it'll be easy enough to change over later on so making our rooms be visual was pretty easy to do them in the editor but to actually connect them is going to take a little bit more work and i think it's going to be easier to start off doing it via code so that's why we're kind of flip-flopping back and forth again feel free to experiment as you want with your own game to try and do different things but i think for now we're going to keep our connections by code and we'll try and change that later on if we get enough time in this series so let's think about what it means to connect a room well we're probably going to want to function right and we'll call this function connect exit i guess we'll say and we're going to need a couple things whenever we call this function we need to say which direction this exit is in so we will say direction and we'll make this a string for now we might have an enum later about potential directions that we can use but for now we'll just do a string and so we need to have a direction and then we also need a room to connect it to so this is going to be the other room that we are going to connect this direction to and one thing i want to do to make this really easy is i don't want to have to connect the exit both ways so like if i have a house right and i can go outside i think i've mentioned this before but i don't want to have to connect the house to the outside and then also the house or the outside back to the house if possible now there are sometimes you might not want that to be the case so you might have to adjust for your own game but right now at least i don't have plans for one-way connections i want them to be both and so i'm gonna bake that into our connecting function we might eventually you know one thing you could do is have a separate like funk connect exit one way that you could call so that that might be a really easy way to do this but for now we're just gonna do this and so what i want to do is actually have a match statement here because i'm planning on having pretty standard connection names so if i say match direction and let's say that direction is west this will make a lot of sense in just a second i can say exits of direction equals room and one thing i can do to make this connection automatic is also say room dot exits of east equals self okay so i'm not saying this is exactly what i want to have also i think um that there's a i can't a reference room here because it's cyclical but that's fine um sometimes good o gets freaked out if you reference the class name from within that class or from another class there's there's some weird stuff there but um so what i'm doing here is saying hey if the direction we want to connect is west then create an entry for west in our this room's exits and connect it to our new room but in the new rooms exits connect the inverse of that which is east connect that to our self to this room and so this is kind of verbose i'm gonna we're gonna add a way to change this later but say i can do the same thing for east and i can do exits and basically the same thing here so i'm actually just gonna like add all these for the four cardinal directions and then follow up in a second okay so here you can see i've added all four cardinal directions and then i'm connecting this other room the inverse of that so west is east east west north south south north et cetera so what this is going to do is just make it really easy if we want to connect things in the four cardinal directions to just automatically connect both ways of that connection if you don't want to do that or you want to use something besides the cardinal directions you could add a third parameter perhaps that says like override um whoops override direction or something like this so you could make this a boolean you could say bool and you could default it to false and then if it's true you could then not use this match statement and then instead have an override and you could pass that in as maybe another fourth optional parameter so just some options there just because you do it this way doesn't mean you have to stick to connecting it automatically you could have multiple connections for example connect exit both ways connect one way connect override connect ran whatever you can do you know and you can also make this more than just the four cardinal directions you could do up and down you could do portal the portal you can do northwest to southeast for example you can do whatever you want so this system is not actually that rigid it's pretty flexible but i just wanted to show this as a really easy way that we're going to get off the ground running quickly by connecting rooms okay so we have this connect function but where are we going to actually connect these from where are we going to call this function and i think where we want to do this is in our room manager so this is why we added this node earlier is we could do it in our game state but i want our game to really handle kind of connecting between our player input and our responses and it shouldn't really have to know about how rooms are being set up so in our room manager we're actually going to add a script and here what we're going to do is just in ready so let me clear everything out except this there is a way to get rid of all the boilerplate comments on these guidos or gd script templates but i just never get around to doing it but what we want to do is manually connect all these rooms so first let's add another room so if i duplicate our house room and i'm going to say this is outside room let me just drag this over here so we can kind of see i'm going to say you are just outside this would be um you are in like outside in the village or something like that um and so what i want to do is connect these two together and what i can say is houseroom dot connect exits and here we're going to connect and exit so if we look here i'm trying to keep this uh keep our room manager the same looking the same as how we actually want to connect it so if our house room is on the left then we want to connect our outside room to the east so from out house to outside and then so we can do is connect to the east and this is going to be outside room now remember because connect exit is currently connecting both ways we don't have to say outside room.connect exit west house room it'll happen automatically and in order to remember that i like to work outside or inside out so i start with our house room and connect all the rooms there and then if i go to our next room i only connect the further connections from there i don't connect back and you just kind of work your way inside out so that's just one way to do it again you'll find a way that works for you but now when our game starts we're going to connect our house room in our outside room that connection is going to happen both ways and what we can now do is actually print out the connections that our current room has any time we move to a new room so in order to do that we can come to our command processor and remember we are keeping track of our current room here so we can get the exits of that room and one thing we want to do now is when we're changing rooms we want to print out the available exits for that room so how can we do this well one thing we can do is we can say we'll just say var exit string and we're going to say new room dot exits so first we're going to get the exits and now remember exits is a hash or a dictionary which is a hash and so one way or a hash map at least and what we can do now is actually get the keys here so we can say dot keys and this is a function that's going to return an array of keys now we already know what we can do with arrays so i'm actually going to convert this to be a pool string array and so now we're going to have an array of strings and what we can do here is join just like we're doing below but we're going to join with a space and so we're going to get a space separated list of exits and so this is going to be a string so we are going to do same as above we're going to do exits and then plus exit string and now when we start our game we'll see that we have an exit of the east and we're not currently wired up so say go west or go east nothing happens but you're seeing that we can now output the actual exits that the room we're in has and so now we can actually wire up changing rooms from our go command so there's a couple things we need to keep in mind as we try and do this one is that we only want to let the user go to a valid room you can only use a valid exit so in our go command there's a couple conditions we'll need first is to say if current room dot exits dot keys dot includes and then here we're going to have second word oh whoops not as a string there so if our current exits includes um the second word so if the second word is west we want to make sure that west is an actual key in our hash so if it includes it then we can say you go to second word etc but we can do else we'll have another message which will just be returned that is not a valid exit from this room or you can say something a little less i mean that sounds super stiff maybe we can say like um this room has no exits in that direction that sounds a little bit uh less like a programmer speak here um so yeah now we are saying hey if this is an actual um room this way then you go there else there it will just print out a message to help the player saying hey you can't actually go that way it's not a valid direction and so this is almost good except the one thing we need to do is actually call our change room function here and so what we need to pass in is the room in that direction so we can say change room and how are we going to get this new room well we can say current room dot exits and we can get second word and we know that this will be a valid key here because we're already we're calling this includes here we're only doing this if the second word this direction is included in the keys for our current room so we're going to get a valid exit here which will return an actual room and then we're going to change our room to that okay so what we can do is we're going to return you go this way i'm gonna actually add um a period here and that way it'll be a little bit more grammatically correct okay awesome so i think we're good to go except um oh actually i always sorry i do this sometimes it's not includes it's actually has here so on arrays the function to find something or to see if it has something is just has um i kind of get that confused sometimes sorry about that so i think if we run this now we should see that our game works we're in a house we can go east so what if i go west we should see our error message which we do this room has no exit in that direction but if i go east we should see we are now outside it is outside in the village exit is west so if i say go east now we get the error message if i say go west boom we are back inside the house so look at that just like that we are able to move between our rooms we have added a simple system of exits which are just dictionaries that live on each of our rooms and we can go west we can go east and oh and you can easily customize this like i said super easy to add other directions really easy to add another function to connect things one way or to connect things where maybe the uh message between each room is kind of different you can customize this as needed it should be pretty easy to do but we've handled our ability to go back and forth between rooms now okay so things are looking pretty good uh there's just one weird thing though if i type go east you'll see that it gives us the output about the new room we're in here now and outside it's outside in the village etc before it actually says what command we want and it's kind of confusing it'd be a lot better if this go east appeared before this text right here that tells us what just happened and so we're going to change that now and in order to do that we're going to get rid of this signal that we created earlier this video and i know it's kind of quick when we change rooms and the reason why is because this is so i wanted to show the signal and how it worked to show one way to do it you could either change your process command system to use signals almost exclusively this would be one way to do it you could change all these returns to emit a signal but because we're using a system where we're returning values all around we're actually going to get rid of this response generated here and what we're going to do is instead of changing room emitting a signal it's just going to return strings here and we're going to add this return value and so in the places we're using this we actually need to return this so in change room we're going to say change response this is going to be a variable and then we're going to say so you go this way second word but what we're actually going to do is is do kind of the same thing down here where we're joining the string together and so now that we have our change response we want to output this in the same message but below this message saying hey you do go this way we're confirming what the user did so we're kind of saying this is what the user did here's the new thing because of that here's the new room you're in and so what we're going to do or what we're going to do here very similar to what we did before is a pool string away a string array and because we are making so many of these it's probably a good idea now for us to make some kind of a helper function that will do a lot of this work and this joining concatenating for us so we can do that in just a second but so we're going to make an array we want the first element to be you go here you go west etcetera because we want that to appear first and then after that we're going to add our change response and so here we'll have this and again we want to join this and we're going to join it our delimiter is going to be a new line and so now if i do this you'll see that if i go east it looks way better you go east you are now here it's outside the village here your exit so this is this just looks way better one thing you'll notice we've lost though is that we're not actually outputting the welcome text or the initial room text at the beginning so we've got to change that real quick and the reason why is because this initialize here this is returning um a string but we're not actually returning it from initialize so we can do that and again we might have to change this later on this we're kind of doing some fluid work here and changing things a lot on the fly the reason why is because with a game like a text adventure there's just so much background stuff you've got to start building up there's a lot of foundation that's why it took so long really even to get to adding gameplay mechanics to our game it's just because there's a lot of different systems you're trying to build up so apologies that we're kind of overriding and changing and editing deleting etc as we go but that's just kind of how game development is and as you continue to develop your game you'll realize better and worse things and more ways to do it and um some of this has been planned ways that i've tried to change it and knew we'd be changing it some of it is just as i've been making these tutorials i've realized there might be better ways to do it so thanks for sticking with it and i hope that this process of iteration just kind of introduces you to what game dev is really like and helps you start building some of those iterative skills on your own and kind of identifying better ways to do things as you are developing so with that said we're now returning something from initialize so in our game what we can do is say of our uh initial or whoops so we'll say like starting room response or something this is going to be a string and what we can do is just handle response generated etc for this one so we can say handle response generated for starting room response and we don't have this response generated signal anymore we took it away because we're not using it so we can actually get rid of this connect line and handle response generated is kind of not the um right word here because this is a past tense action but we want it to be more imperative in the imperative tense now because it's a function we're calling to do or to do something it's not handling a signal that was called so we can change this to be create response so we're kind of differentiating between this create response method which will actually create it itself and then add response which adds it to the game so there are two separate things so we'll call this create response and then it kind of makes sense this function creates our response instance and gives it its values and then add response is what actually puts it into the game and so now that we have create response we can come up here and say create response create response and now when we play our game we'll see our initial welcome text and our initial starting room text so i can go west air go east and now you go east here you are and this is just a lot more clear it looks a lot better and we've now standardized our command processing system so it's always returning values again we might change it later on if we find a better way but for now it's just working one standard way across every command word every function it has so that's really nice and here we go we've added exits to our game we've added the ability to move between rooms and we've added helpful error messages when you try and go somewhere you can't go so i'm really excited about this guys thanks so much for watching i hope you've enjoyed the video and found it helpful if you have a like and subscribe is much appreciated supports the channel we'd love to have you on our discord server link to that is in the description below feel free to ask any questions about the tutorial there and if you find my work helpful buying me a coffee on buy me a coffee helps me continue to make great videos and is much appreciated thanks so much for watching everyone i'll see you in the next video you
Info
Channel: jmbiv
Views: 955
Rating: undefined out of 5
Keywords: godot, godot engine, godot 3.2, godot tutorial, godot 2d, how to make a game in godot, game development, game development tutorial, game development for beginners, godot for beginners, game dev, indie game dev, how to make video games, how to godot, gamedev, godot game engine, godot text adventure, godot text adventure tutorial, godot text, godot input, godot user interface, godot control node, godot control, first godot game, my first godot game, godot beginner tutorial
Id: 6rHrGs0hmpQ
Channel Id: undefined
Length: 20min 3sec (1203 seconds)
Published: Tue Mar 30 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.