Adding Rooms (Godot Retro Text Adventure Tutorial #7)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video we're gonna actually implement rooms into our game we're gonna start with one basic room we're gonna learn how to add names and descriptions to those rooms and then output them to the player whenever the player changes rooms and how to set the player to start off in one room let's get started okay so at the end of our last video we had implemented the go command so i can type go any direction or really go anything at this point and type go west and it'll say you go west but the problem is that we don't have a concept of rooms or space really in our game right now it's just kind of repeating whatever the player types so what we need to do is make a room object that we can use in our game we need to make a room system that will let the player go between rooms and it'll keep track of which rooms are connected to each other and so we're going to do that now now in most game engines if you were making a text adventure and you weren't using like unity or unreal or godot you were using a framework or code specifically you'd probably just do this all via code and at some point with a simple game like a text adventure you've got to ask yourself why would i do this in a game engine like godot or unity etc instead of just kind of rolling it on my own because there's not a lot of graphical interface and i would argue there's two reasons for that one reason why you might want to use godot or another engine is because it makes it really easy to format what we've got here this graphical user interface it's really easy to use godot's container system so it does a lot of that work for you so that's one thing but even that if you're pretty good at that or you just prefer to do it via code it's not a big draw so one other thing that might be really nice about making a game that is not overly graphical in nature like a text adventure in godot is that you get access to a bunch of other built-in editor functions so for example instead of just like making a bunch of rooms via code where you just instance this room and this room by just calling a line of code each like you would in a game framework or just like if you're writing this in typescript or javascript or java one nice thing we can do is we can actually use godo's editor to instance all of our rooms and our scene tree and edit them visually and eventually one thing i hope we get to do is we can make a visual map of our rooms and edit and connect them like that so we actually have a visual map editor rather than just having to rely on connecting things via code so we're not going to get there quite yet that's something i hope we'll get to in the future but just wanted to bring that up as a reason why doing a game like this in an engine like godot is actually might be worth the effort it might not depending on what you want to do but it's something worth considering as you get started okay so let's start building our rooms so what i'm going to do is come over to our game here and you'll see now we've got our background this is really just our graphical user interface it's probably what i should rename that to and our command processor so we've really only got two nodes within our game we're going to add another one and this is going to be our room manager and this is the node we're going to house all of our room nodes in and so what i'm going to do is create uh we'll just keep this as a regular node for now we might end up making this a control node later on again we'll you know we don't need to be perfect right now we can kind of refactor as we need to go i'm going to call this room manager and right now our room manager isn't really going to do anything we just need it as a parent because we're going to add our room nodes to it so now we need to actually create our room nodes and what i'm going to do is add a panel container as a child to our room manager so i'll select panel container here and add that and you might be wondering at this point why i'm using a panel container and it's a very good question especially because rooms ostensibly right at the start at least won't really have a visual representation of the player but i'm kind of trying to plan ahead a little bit and make a way so that eventually if we do have that visual map i talked about we've already got some kind of a gui for our rooms that works really nice so i'm just going to call this room there's really no reason there's no downside to using a panel container here so we might as well just get it started now and i'm going to hit w so that i'll hit this move key up here i'm going to move our room up here so our room is a panel container right now it doesn't really have any there's nothing to it so we need to start adding some things if we want it to show anything so one thing we can do is we can add a margin container and with our margin container all i want to do is just provide a super super simple graphical interface or kind of heads-up display for what our room is we're not really going to do much with this graphical part but just to get something there so now we have a margin container i'm going to add two things i'm going to add a v box container because we want rows here again under my uh kind of standard style i'm just going to call this rows because they're stacked vertically so they're rows and then what i'm going to do here is add two things one is a label that i'm going to call room name and i'm going to duplicate it with command d and i'm going to call this room description we might add more later on but for now we'll do this and i'll just add some basic text room name and we'll say this is the description of the room and in order to make these a little bit uh i just kind of have a common theme or look to them i'm going to go to erect here and what i'm going to do is set a min size so right now this has 239 in the x direction so let's just set it i think that's a good let's do 250 pixels wide and then we will set let's do 100 pixels high and this can kind of be our default room i might make that a little bit bigger 200 let's go we'll do 100 for now and then i'm going to do a little bit of changing of the fonts here since we've done some work with the fonts before so i will make our room name be our 24 font size 24 of the plex mono and our room description is going to be 16. there we go just drag those in there and all of a sudden this is what we got and i want to make a room description label wrap so i'll say auto wrap and now it'll automatically fit to the uh if if a fixed width is set for a parent it'll automatically fit to that and i don't want this to wrap but i might center it who knows yeah we can do that for now and so this is kind of what we got for our room let me readjust our room here our min size we'll make this 200 just so it looks a little bit better and there we go so now we've got a really basic graphical descriptor graphical interface into our rooms again we're not going to use this right now what it will help us see though is from our scene tree we can kind of get a glimpse of what our rooms look like without having to go into our code so that is really nice and then later on if we end up showing some kind of a map to the player we might be able to just do a viewport directly into you know wherever these are in our scene tree and our player will just be able to see it so what i'm trying to do is make some or make something right now for a room system that works for us right now but also is going to make it easy to add cool new features later on and you might be asking isn't it bad that these are in our kind of world space they might show on the screen and it's a great question they would if i drag them down here but remember our camera is or our game node itself just takes up this space here so we can have other things in our tree and in our world space as long as they're not within the bounds of our our parent node or our camera itself so if i keep them up here they'll exist in our world space but not actually be visible to the player and you'll see that that if i open this up we can't see our room node and so this is good because we can maybe just have this random space up here as our map later on just have a viewport or a camera that looks this kind of a square in here and we could just look into that but getting ahead of ourselves we'll get back to that later on okay so we've got a room node let's actually add the script because this is you know kind of what is going to determine our gameplay here so one thing i'm going to do is right click on a room node and i'm going to say save branches scene because we want to save this as its own scene that we can continue to reuse as we continue to develop our game so i'll do that then i'll open up our room scene here and i'm just going to it's wrecked position set that or restore that to the default and i'm going to add a script here so that all of our rooms will have it and it's just going to be room gd and we'll be good to go and i'm just going to make sure since i reset our position on our room node our base room scene i'm going to need to drag this one up somewhere else over here so it's not back at zero zero since the position got reset okay so let's add some functionality to our room one thing we'll notice is we've got a room name and a description uh so we probably wanna set that within our code so what i'm going to do is add two export variables i'm going to say export and this will be string export string var room name and then i'm going to do the same thing for the room description this will be room description okay and then we can just default this to room name and default this to this is the description of the room okay perfect so now we've got these things we can change from the editor so what i can do for example now is come back to our game and you'll see a room here i could change this to be a house and a random house so all of a sudden we're able to add rooms and edit them really easily from our scene tree we don't actually have to do it via code so that's already a nice step but what we want to do is actually add some ways to get around and add rooms to our game okay so now we've got a room i just randomly called it house you can call it whatever you want we'll probably change this as we go but just get started now we've got to tell our game that we need to start it off in this house we need to actually tell our game this concept of rooms and make sure that we can move between stuff and there's a couple ways we could do that we could for example have another export variable where we drag one of our rooms in our room manager um that's one way to do it i think what i want to do for now is actually do it via code despite you know how we've been leaning towards the editor earlier on i think it makes a little bit more sense to start this one off by code and i'll explain that in a second let me just also rename this to be house room so we can actually tell which one it is from the scene tree and so we need our command processor to know about rooms so that when we type go the command processor can actually figure out where we're going um but at the same time our command processor shouldn't be super responsible for doing a bunch of the like it's not the one that should know about all the different exits and entrances to a certain room each room should keep track of that on its own and the command processor shouldn't be the place where we initialize our game state that should be in our game script so what i'm going to do this is a common pattern of mine whenever i need a parent node to pass data into a child node right at the start i use a function called initialize you can really call it whatever you want that's just my like kind of go-to because it's it's pretty easy to understand hey this is a function that needs to be called when this node should really start running it's different than like the built-in ready because this one can actually be called by a parent and so what i'm going to do here is just say starting room and so we're going to have initialized function where our game is able to pass in the starting room to our command processor we'll just do pass here and what that means is that we need to keep track of our current room and our command processor we could also keep track of this in our game state and pass that in to our command processor here that might be the better way to separate those concerns so that might be a better architectural decision but we'll just kind of start with this now and then we can change it pretty easily later on so what i'm going to do is just say var current room and this is going to be null and so what's going to happen is that when we call initialize and we pass in our starting room i'm going to say current room equals starting room so we're going to pass in our starting room and set that to be our current room and then whenever we process a command we can use our current room anywhere else to process that so if we so say go west we can get the west exit of our current room and then we can change our current room to be the room that is the whatever is to the west so we're going to kind of use that as our pattern just to start working on our command processing one other important note that we need to think about is how the player in the room should be separated we could make it so that the player knows what room they're in but i think it's slightly better to sign design if we make the player a little agnostic of the current room and we let rooms handle all of the items and npcs and even the player that's in it and we might have some you know we might go back and forth on that later on but i think it's a good way to try and keep rooms and players separate for right now so what we need to do is actually pass in and call this initialize function from our game script so i'm going to come up here and then when we start our game up and are ready what i'm going to do we're going to have to change some of this but what i'm going to do is we'll add a room manager on righty here so i'll say unreadyvar manager and this is just going to be a connection or we're going to just grab our room manager pretty simple and what i'm going to do is say command processor dot initialize because remember we added that initialize function and here we need to get our starting room now again this is where we could have an export variable we could drag it in or whatever but i think it's good enough for now just say room manager that gets child and we'll just say zero so this is going to get the first child of our room manager because get child is zero indexed so this is going to be number one this will crash if you don't have a child in your room manager obviously but we'll just assume we do for now it's a pretty simple fix so now what's going to happen is our command processor whenever our game starts is going to assume or it's going to set our current room the room the player starts in to be this house room and then the player will be able to go from there now one thing we want to do we've added a description and a name to our room we actually want to change the starting text to line up to what the current room we're in is so whenever you go to a room it should say you are now in this room here are the things that are here here are the exits etc we want to make a new kind of locating output be given to the player so they can understand where they're at each time they change rooms and so that's what we're going to need to do now so in order to make that change we need a way for a command processor to tell our game hey there's a new response that has come based on whatever the players just outputted and right now we really care about moving rooms but there's a lot of things that are going to need to happen with that it might be an item to respond and say hey the player picked up this item and so we want to provide an easy way for our command processor to tell our game that there's a new response that needs to be um that needs to be printed out and so what we're going to do is add a signal to our command processor so i'm going to say signal and we're going to just call this a response generated this response generated signal is going to take a parameter which is going to say response text and here we're going to pass in the text of the response that needs to be sent to our game script and so what we can do here on our game is actually connect to our command processor so we use a command processor dot connect and here we'll say response generated we'll say self and we'll say handle response generated so we are going to need to add this handle response generated signal or this function that responds to the signal somewhere in our game script and it's important here that we connect this before we initialize our game and the reason this is important is because we want to make sure that a response gets printed out when we initialize our game and our player is moved to their starting room so if we connect this after no when we move our player to our the starting room this initialize will get called before we've actually connected this so nothing will get printed out so just make sure you have this line of code before you initialize your command processor and so we can just come down here i'll try and group all the things that are doing responses down in this area so what i'm going to do is just say function and i'll just command or copy in what i said there this will be response text we'll do pass here and so you'll notice when we're doing our starting message up on line 22 that we are on line 21 we are instancing a response and then saying what that text is so i'm actually going to just copy 21 to 23 i'm going to cut and paste them come down here and throw those in and so what we want to do now is say um we'll just say response here so whenever a response needs to be generated we're going to instance our response which remember that is the type of output that we made it's a scene that we made a custom one that should be displayed whenever the game has a response for the player and then we're going to say response.txt equals and no hard-coded anymore it's going to be response text amazing and then we're going to add this response to the game and so now we've made a really dynamic way to do what we're doing with the starting text but now it's going to be able to respond to anything not just starting text so now we've got that this is going to get called because of this connection our handle response generated function will get called when we move to our current room which is great now we just need to actually emit this signal and handle emitting it correctly from within our command processor and we'll do that right now so if i go to our command processor we need to output this script or output this signal whenever the player moves to a new room and because we're handling our current room here we're going to keep this within our same our command processor script because that's where we're handling it again we might move this out to our game later so our command processor doesn't have to do a lot of that state changing stuff but for now just to get something that works we'll keep it in here and so there's a couple we're going to need to move rooms a couple times we're going to do it right here when we initialize our game and we're going to be doing it whenever we go to a new room and so i'm going to make a helper function down at the bottom here that's just going to say function and it'll be change room and what we're going to do here is omit signal and this is going to be response generated and now here we've got to have the text of the response that's generated and so what we need to do is actually pass in a new room here and so when you go to this room we can say new room dot description or room description and it'd be really nice to make sure that room description is actually a property on a room i know it is one because if i go to room i'll see room description but it'd be nice if godot helped me by giving me that autocomplete and so what we're going to do as we've done before is come into our room.gd and i'm going to say last name and this will be room and so we'll define an actual class for our room and now what we can do here is say that new room is of type room and then if i get rid of room description start typing again you'll see it'll auto-complete so this is really nice so whenever we change rooms we are going to then output our room description what we could actually do to jury rig this a little bit is do something like let me just copy this line so the problem is that our description is not going to be is not super helpful right now so we could do we could add another line like you go to and then we'll just do new room and then room name and so what this will do is it'll output two lines it'll say you go to this new room and then it'll output the description of that room so calling this emit signal twice isn't ideal um we can make a better signal or just like a better response type you know like a move room response we can have individual response types you know so we there's there's tons of avenues we can explore to make it better but we'll just do this for now again just trying to get something that works and what we're actually going to need to do now to is say current room equals new room and so one thing we're going to need to do is eventually call change room from go but what we can definitely do right now is from initialize we can just say change room and push in starting room and so whenever we initialize the game we're going to change rooms it's not really changing because we don't have a current room but it'll still work we're going to change rooms quote unquote into the starting room and we're going to print this out so now in our game we don't have to have this hard-coded starting message anymore because when we initialize our game it's just gonna get printed out so now that we have it let's see this in action so if i run our game we'll see you go to a house a random house okay so this is obviously not very helpful at all but we are now able to actually go to a room and see text specific to that room which is really really nice okay so this is what we have and it's really nice that it's tied to a room now but there's three quick things i want to do before we end this video one is i want to add back some of our welcome text just to like kind of indicate to the player the game has started and give them some helpful hints two i want to make this message a bit better and three i want to make it so we don't have to emit that signal twice every time we call it so we'll start with the welcome text because that's going to be the easiest thing to change and what we're actually going to do is use our new handle response generated function now that this is actually doing everything we need we can just pass in some response text oh also i think off video i typed this as a string so make sure you also do that just to get a little bit of better autocomplete here but um so what i'll do now just to add some starting text is i can say handle response generated and here i'm just going to say welcome to the retro text adventure you can type help to see available commands and so it's just a simple message but now what it's going to do is just show that response right at the beginning and it just gives the player something to start off with welcome to the retro text adventure the game is started you can type help to see available commands so let's have help and i see this helpful message so it's just a little bit better than what we have before and okay cool that's good now i want to edit this message a little bit just to make it better because you go to house a random house is kind of lame so we'll fix that right now so if i go to our command processor rather than saying you go to what i can say instead is you are now in and then it'll just be so you are now in something like you are now in house which is just slightly better um so that kind of changes our message a little bit but the third thing we wanted to do is not call emit signal twice and this will also do number two which is making our message better is we can actually make our signal taken an array of text so the problem right now is that when we emit here we are creating two response variables because this response is getting instanced twice because we're calling this function twice so we don't want to we don't want to omit two signals we just want to emit one but potentially pass in multiple text values and so right now our handle response generated function takes a string so it just has to be one long string but there's a couple different things godot lets us do as far as string processing that can make this look a little bit better and so what i mean by that is rather than changing our function here and we might eventually do that maybe eventually we'll need different types of responses we might want to add some more colored text we might want to have some variations to make our game feel a little bit better but right now what we can do is actually call emit signal once but pass in an array of strings that we join together so in order to do this we're going to create a new variable which is going to say variable we can call it strings or messages whatever you want and we're not actually going to create an array so what we want to do is have an array of strings that we join together with a new line and so what we can do then is use the same response instance but then have new lines to kind of differentiate between them just so it looks a little bit better and this is going to provide us a lot of flexibility later on to whenever we change rooms not only say what room we're in but also say the exits the description the items etc so it'll give us flexibility to add all those things later on the problem though is we don't want to use an array in godot because basic arrays do not have a join function instead godot has a built-in type called a pool string array which is an array specifically meant for strings that's very memory efficient and has a lot of those standard string functions or standard array functions to strings and so we're going to use that instead in order to make that instead of just creating a basic array like this we just say pool string array and we're just going to use the constructor to make one and what we're going to do here is just have an array and here we're going to actually add in all of our strings so this first one we're just going to copy in and say grab this and now we've got this and we can start going from here and what i'm going to do now is get rid of this emit response and we are just going to say strings and with our strings variable here we need to make sure that and we join them and our delimiter excuse me is going to be the new line and if you're not familiar with this this new line character this backslash n is a standard character in um in strings and really in any language in just computer programming in general that has a new line between values so what this is going to do is for every value in our array it's going to add a new line to the end of it which means that when it gets shown to the user it'll be like hitting the enter key it'll be the next line down and so this is going to let us format our strings very nicely even though we've got multiple it's going to join this array into a single string with new line characters in between each separate message so let's say that we want to have not just our room name but our room description on the same line so i can say here we're gonna add another plus and i'll just have a period here and i'll say it is and then we can do some things later with articles so like a house or the house or like the castle gates there's you don't always want to have a or the there's different articles you can have so we'll do some more like string parsing for rooms later on but here we can just say new room that room description and we'll want to have some standard standard practices for how we name our rooms and descriptions so one thing we can do is if we always want to display our room description as part of the latter half of a sentence we can just have a convention now that all of our room descriptions will be lower cased so i can say a random house that you do not recognize okay so now we've got that and i can say a house for now um eventually later instead of putting the a here like i said we could have an article field perhaps but this will work for now so now in our command processor when we print this out it's going to say you are now in a house it is a random house you do not recognize and so that's just going to look and feel a little bit better of a message to the player it's going to be a little bit more easy to understand and look at and now we've also got room in our string array here too later on to display the exits so right now our rooms don't have exits but we could add a new line that says like exits you know and then just like have a list of exits from there we will just have this as a stub for now and then in the next video we'll learn how to add exits to our rooms that we can move between but for now this should be good so if we run our game we'll now see our welcome message up here welcome to the retro text adventure and then it'll say you are now in a house it is a random house that you do not recognize and then exits which again we don't have exits yet but this is way way better now i can still type stuff and nothing happens i can type help and go so we haven't yet connected our go command that'll be next video when we do exits but we have rooms in our game now and these rooms are able to have a name and a description and we're going to add so much more stuff later on i'm hoping that we'll get to at least add some stuff like items but uh yeah here it is now we've got a game that has rooms and we're gonna be able to actually move between them next video so get excited for that all right everyone thanks so much for watching hope you found this video helpful and if you did like and subscribe to support the channel i always appreciate it if you have any questions we'd love to have you in the discord server we can help you there the link to the server is in the description below and if you found my work helpful donated coffee on buy me a coffee is much appreciated helps me to continue making great tutorial videos link to that also in the description thanks everyone see in the next video you
Info
Channel: jmbiv
Views: 1,685
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: 8VjNGztTFgo
Channel Id: undefined
Length: 29min 10sec (1750 seconds)
Published: Tue Mar 23 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.