Remembering User Input and Deleting Input History (Godot Retro Text Adventure #5)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video you're going to learn how to prevent the user from entering empty lines so if you see here i'm pressing enter nothing's happening so make sure that the user is only entering a valid input and we're also going to learn how to forget values after a certain amount so you're going to set a max lines remembered so for example you'll only remember 30 lines and you'll see this a up here if i start typing a bunch eventually it'll get deleted if i keep typing and gone and now we're back to s's so you can set a customizable amount of max lines remembered that you will continue to forget lines after that amount of input has been entered to kind of help free up memory as your game continues to go on let's get started so in the last video we started adding functionality to scroll to the bottom but one thing we didn't catch is that if you try to enter a blank line you're going to just be able to enter it indefinitely so we need to make sure that the user can only actually enter something or it's only responded to if there's actually content on that line to respond to so the simple way that we're going to do that is just in our game script here in our on input text entered function where we're handling user input we're going to say if new text oops if new text dot empty which is a built-in function on strings that returns a boolean true if there's no text in that string so if new text is empty we're just going to return so we're not going to do anything if the user enters just an empty line of text so if i play this now and just try entering nothing's there but if i type something we can still type but if i just hit enter with an empty line there's nothing there so that's an easy way to add some handling to make sure that you can't type empty lines so the next thing we want to do is add a customizable max lines remembered variable so that we don't keep track of an infinite amount of lines and keep a bunch of memory we don't need so in order to do that we're going to introduce a new concept in godot and that's export variables so below our input response up here i'm going to type export and then parentheses and i'm going to say ins here and then variable max lines remembered and i'm going to default set this to 30. and so what this means this should all look familiar we're just creating a variable we're giving it a type and we're setting it to 30 so it's an integer but these two things are new and what export means is the value of this variable can actually be edited in the editor and it's going to be unique for each instance of this scene so if we had a bunch of different games you could change their max line remembered in the editor for each one and it would be remembered for that specific instance even if they were all different instances so export is nice because it lets you change it in the editor so this is what that looks like you'll now see if i select our game over on the right there's a script variable section and we see max lines remembered here and it's set to 30. and so it's populating that with the value we give it here as a default but i can change it in the editor so i can set it to be 20 and you'll see it recognizes that this is different than the default value so i can reset it but if i set it to 20 that's going to be the value that that variable has so it lets you adjust the value of certain variables from the editor it's super nice and a really powerful feature and we're going to explore more ways that that's helpful in the future this is just like one nice way we can use it so i'm going to reset this to 30. so and this right here is giving a type hit to the editor so if i try and do because i i hinted it that it's an integer if i try to do 30.1 it's going to round it to the nearest integer so if i do 20.5 it'll be 21. so type hints are a great way to kind of use some built-in editor functionality to set or to help guide the user in which values they can set and prevent them from setting invalid values so now that we have our max lines remembered we need to actually check and make sure that we don't remember more than those lines and the way we're going to do that is down here when we add a new line so in our history rows when we add a new input response we're going to check and see if historyrose.child count or i think it's get child count so this is going to return the number of children in history rows and we're going to see if this is greater than our max lines remembered then we need to delete the earliest lines and so the way that we can do that is actually take advantage of how godot structures its scene tree so whenever you add a child of something so let's actually try this i'm going to add some children of our history rows just input responses and the you can always guarantee in godot that the unless they're manly moved which we're not going to do so in our case because we won't manually move it we can guarantee this will be true we can guarantee that the the children that are farther up in the tree are the ones that were first added so the first element the first child of our history rose is the one that was first added and so if we want to forget the earliest lines that the user typed we can get rid of the nodes or the scenes in our history rows we can get rid of its earliest children the ones that are furthest up the tree so we know that the ones at the bottom the children that are most recently added are the ones that are at the bottom of the children array and vice versa the ones added earliest are at the top of that array so we can take advantage of this by saying hey if we have to delete rows we're going to say var rows to forget and this is going to be max lines remember or sorry this is going to be history rows that get child count minus max lines remembered so if there are 35 children and we're only remembering 30 lines this is going to be 5. so we're going to get the rows to forget and then we're going to say 4 whoops 4 underscore i in range of rows to forget range is a function here and then we're just going to write pass so what this means is gdscript has arrays built into it you can use a for loop or it has loops built into it for loops are super nice but this rows to forget here is an integer right we're getting the cha the children the amount of children that we have in our history rows and our max lines remember those are both integers but what we're going to say is we want to loop through that many times and do an operation that many times so to convert a integer into an array of all the integers in that range you can use guido's built-in range function so this is going to create an integer from 0 to i believe rows like the number you pass in here minus 1. either way it's going to have the correct number of things in the array and that's what we care about so what we can do now is say history rows dot get child and get child takes an index fortunately we have an index it's going to be i we actually don't want to underscore this when you prefix a variable with an underscore it's like telling the gdscript that it's a private variable you can still access it it's not actually private but it tells the editor not to print an error for example if it's unused so we're going to say for ion range we're going to get that child that we're currently looking at for this current index and then we're going to free that child so we'll type q free and q free is the way in godot to delete nodes from your scene it not only removes them from the tree but it totally deletes them so they're freed from memory now there is just a free function but you almost always want to use cue free because what it does is it cues that node to be deleted as soon as it's safe to do so which is almost always at the start of the next frame or during the next frame and so it'll make sure that all the things that that node is being used for relied on are finished up and then cued free you don't have to worry about this making it take forever for your node to get freed or it's sticking around too long that's not what's going to happen it's just going to make sure that it's safe for the engine to disengage with that node and get rid of all the connections it has before deleting it so we're going to go through for our history rows and we're going to get the specific child we're looking at and remember for example if we have 35 children and our max lines remembered as 30 that means we're gonna have five rows we need to forget range is going to create an array of numbers from zero to four and because the child array is zero indexed it's going to work out perfectly here so we're going to get children 0 1 2 3 and 4 and remember we said that the children at the top with the lowest indices are the ones that were first added so we're using we're using features of gd script to make it happen to make this happen without having to code any special logic it just works out so we're going to go through this range of five or whatever this number is and q free and delete all of those children so we'll see this in play now that if i run our game and i'm just going to type every key on our keyboard oh whoops i need to actually get rid of these first because we don't want those and so if i run our game and i'm going to type every key on our keyboard real quick okay so you'll see here that i've typed pretty much every letter on the keyboard so we're close to 30. and i started with q so i just went across the keyboard so we've got q-w-e-r-t-y qwerty at the top and now since we're getting close to 30 lines if i start typing some we should start to see these top rows get deleted so if i type just again q w e we should see oh it's still there let me do a couple more qw i'll do all right r t y now the q and the w are gone so we've gone over 30 rows and the q and the w are gone we're left with only the e r t y and everything before if i type x now we'll see that that e is gone r is the most recent one at the top and if i keep typing stuff obviously they'll still be deleted and so on so we've just implemented a way to to limit how much history can be remembered in our scroll container in order just to make sure we're not continuously adding things and never deleting some of that memory so we don't have thousands and thousands of lines just to kind of keep our game stable and not have to worry about remembering so much stuff so you can adjust this number as needed it does not have to be 30 you could bump it up to 100 even a thousand would probably be fine just depending on your computer you know it doesn't have to be this low but i'm just going to keep it there for now for the purposes of our game just to keep things kind of nice and clean but anyway i think we'll cap it there for now but in this tutorial you've learned how to keep a scroll history and delete things and how to make sure that we're not letting users enter empty lines so now we're starting to get a game that has a really nice text input feature there's lots of stuff that you can do it feels good it responds as you would expect the scroll works as you would expect it moves you down but lets you see its history things are starting to feel really good and we're ready to start actually adding some actual gaminess some functionality some fun to our game anyway guys thanks so much for watching i hope this video has been helpful to you if it has a like and subscribe to support the channel are always appreciated we'd love to have you in our discord server the link to that is in the description you can ask any questions you have about the tutorial there and if you find my work helpful donating a coffee on buy me a coffee also linked to in the description helps me continue to make great tutorials thanks so much for watching see you in the next video
Info
Channel: jmbiv
Views: 1,440
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, gamedev, godot game engine, godot text adventure, godot text adventure tutorial, godot text, godot input, godot user interface, godot control node, godot control, my first godot game, godot beginner tutorial, godot delete node, godot lineedit, godot user input
Id: 3pHaPkN0Bms
Channel Id: undefined
Length: 11min 42sec (702 seconds)
Published: Mon Mar 08 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.