Godot GDScript Pro Tips

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
gdscript the unique language of the godot engine is much more powerful than you would think at first here are five pro tips and five features we use all the time and you want to know about we'll see that unlike with c-sharp you do not need to bother with pooling for most projects have you ever done save games in json there are more efficient built-in functions to save and load data without effort we'll also look at why preload is cool and it's not just because it preloads stuff in gd script you can use any values as keys for dictionaries we'll see how to use that to map units to grid cells in grid based games finally we'll see how to turn any script into a custom iterator with three short functions let's get started with pooling with godot and gd script you most likely don't need pulling pulling is a performance optimization for games where you spawn lots of objects like this shooter 2 000 good heads per second at some point the computer can't keep up so uh there's that optimization called pulling that you can use to make this a bit faster these are still 2 000 bullets per second but now it's a bit smoother although not perfect i would say maybe the recording's playing a role there this optimization is absolutely necessary with languages like c sharp which are garbage collected it consists of creating an array where you're going to pre-create many instances of whatever here are uh bullets i'm creating 3000 of them and then every time you fire a bullet instead of creating one from scratch you get one from that array and you cycle over the array so you constantly reuse them again and again so you trade some memory because you pre-create the bullets and store them for some instantiation performance now here you can see while creating 2 000 per second so we end up with thousands and thousands of objects and almost 100 created every frame unless you reach these numbers in your game you're not going to have too much trouble so here are 500 bullets per second buttery smooth it's because in gdescript the cost of creating objects is pretty low and not having to create these pools is going to make your code simpler this does the same thing as this other example but just with less code now as always the rule of thumb for optimization is only do something if the game starts to get slow then you inspect y you have these lag spikes and you adapt your code accordingly do you ever save games with jason in godot if so you're doing it wrong there's a simpler way that's built in so here's a little demo where i can move my ships save the game with the button and if i click load it restores the save game does it use json no not at all why don't you want to use json well because it's a format for javascript that does not support positions as in the good at game engine you have to convert values it only supports floating point numbers as far as numbers are concerned it's not ideal you need a lot of extra code to save everything compared to this built-in function so if i go to my code there's quite a bit that collects the data from each unit in this loop here in the save function but here is the line that converts the data to text var2str it takes any jscrip data like this dictionary which contains rotation which contains vector2 data and it turns it into text i can then save it to a file with these four lines of code create a file open it store the string close the file in loading i do the opposite create a file open it read its content here and with string to variable sdr to var i can convert back my text i saved with var2sdr into a godot dictionary done you can save colors you can say vector3 you can save transform mattresses anything you want it just works and then you can do the loop the other way around and re-inject your saved data back into the unit so the two functions are var2 sdr and sdr2 var please start using them you have two functions to load files in gdscript load and preload they both do something very similar you give them a file path and they're going to load the file from the disk and return the resource put it in the variable now i recommend to use preload when you can over load and here's why first let's talk about the difference so load allows you to create dynamic paths so you could store some for example uh directory in a variable and we're going to say load versus preload and then construct your thing like that so you could say directory dot plus file this is how you construct file paths and go to bullet.gt and load files dynamically like that so you can loop over a game directory to find all the files those kinds of things and now because of that guru can't really check ahead of time if the path is valid and for example a relative path like that is invalid with load but gude won't give you any error until you run the game i run it you're going to see an error down there in the debugger with preload fast you can load relative file paths which is really nice and you won't get any errors so now if i run the game you're going to see just a warning in the debugger but there are no errors left because it's finding the bullet file in the same directory you can also use complete file paths but more importantly you can type some gibberish and get an error gude will tell you we can't preload the results at the path you know the file does not exist essentially so that's one first bonus the second bonus is that you can use it with constants preventing a teammate from changing the loaded path here third bonus which is pretty small but preload is going to load the files as soon as google compiles the gd script so when you open a scene it's going to preload everything as you open it it's not going to later load which happens only when godot reads that line of code or executes it all right a little thing but the most important thing perhaps is that you can use it to create types by for example here preloading the file that are only available within the scope of that file which is really nice when you are working with add-ons when you want to to create add-ons for the community here if i define my constant name bullet and i load my bullet.gd file when i create a variable bullet i can use the bullet.new here and i can print for example check that bullet is of type bullet what do we see in the output this is true except that the the bullet class here the bullet type only exists in that file so so this makes it pretty good when you want to distribute your self-contained add-on to people and still do type checking now a limitation of the load function and preload is that they freeze the game until the file is loaded which is almost instant in small games but in large ones if you want a loading bar you want to check this page that i will link in the description the resource loader and resource interactive loader classes allow you to go further and to know the loading progress jesus script dictionaries are really nice they allow you to use any kind of value as a key be it an object a vector 2 vector 3 whatever this allows you in a grid based game like this one to map the current cell a ship is in to the corresponding ship so if i play the game here down there in the output i'm printing a dictionary that does just that so you have the coordinates and they are mapped to one of the units the code is relatively simple i'm just calculating the cell each unit is in and then in my units dictionary i'm mapping the cell coordinates to the corresponding unit this is exactly what i use and what i would use for a tactical rpg or any board game like this one here i have a dictionary with a list of the cells that contain a unit and when the cursor is over a cell and i click it just tries to get the unit in that cell you can see it printed down there that it maps again just the positions to one of these units on the board it's really handy few persons know that you can turn any class into an iterator in godot with only three functions this allows you in turn to loop over the content of this object and for example to draw all the tars inside a room inside a ship like we do here so you need three functions to do that it's a init it turn next and it's a get and here we use the fourth one just to avoid writing the same code twice it's uh init is going to start the iterator reset its index most of the time it's a next is going to increment the iterator typically increase the index by one and these two functions return true if the iterator should keep running or false if it should stop which we wrap into a little fall function finally is its rate to get which returns the current value at this stage in the iteration process and this is all you need and this is exactly what we use to take our ships in this game from these areas and collision shapes which we use for plenty of things like pathfinding and turn them into a completely drawn ship with the connected rooms those were our five tips for today if you liked the video let us know in the comments below because we can make more we have plenty to share we're also working on a long course dedicated to goodu's nodes unveiling all their features and secrets all the useful ones you can get three guides for completely free right now there's a link in the description on the screen you head to the page enter your email and we will send them directly to your inbox with that be creative have fun and let's see one another in the next one bye bye
Info
Channel: GDQuest
Views: 28,192
Rating: undefined out of 5
Keywords: gdscript pro tips, gdscript tips, godot tips, godot game engine, godot tutorial, godot gdscript
Id: Kt5leCS8e8M
Channel Id: undefined
Length: 11min 41sec (701 seconds)
Published: Tue Jul 20 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.