Godot Resource Loading Deep Dive

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video i'm going to go over resources and how they work and how some of the memory management in good old works this video is not necessarily for beginners if you don't know what garbage collection is or what the word caching means this video is probably not for you so you've been warned to start off i want to go over to the script tab and then go to search help this will bring up a window that basically contains pretty much everything in godot you may have already seen this window if you collapse object here these are basically what a variant or a variable generally is it's one of these things in this list there's vector 2 3 which you've probably used abool float hint also in this list is object generally everything you're going to create will inherit from object in order to create one of these other types you'd have to go in and actually change the engine code itself in cpr plus so you'll probably never add anything to the rest of this list but when you make your own type you can inherit from something under object if i open it up and collapse the things under there it's a fairly long list but again you'll probably ignore most of this almost everything you'll use will be under node or reference now godot is written in c plus and therefore doesn't actually have a built-in garbage collector in a way they've kind of built their own garbage collecting system that relies on these two types here if you inherit from object itself you are getting no memory management you will have to delete the object yourself or it will be leaked and you will have a memory link so you'll want to create anything you create will want to be either a notary reference nodes you've probably had more experience with uh underneath this list you'll see spatial which is going to be node 3d in good o4 you'll also see a canvas item under canvas item by the control nodes and the node 2ds so basically anything you would drop into the scene tree over on the left side now you've probably had some experience with those but you may not know how they are memory managed if you create a node in code and you do not attach it to the scene it can be leaked their memory management is entirely dependent on the tree over on the left here if you do not add it to the main tree at some point that node can get leaked and you need to manage that node yourself so if you add something to the tree and then later remove it but don't delete it it could get leaked generally if you follow the usual way of deleting nodes it will get cleaned up and it'll be fine it's only if you remove the child using the call that kind of warns you about that so just be aware you can technically leak a node references on the other hand you've probably seen and used here and there without really knowing it references our reference counted in other words anytime you have a variable that holds a reference item it's going to add to the counter anytime that variable gets deleted it's going to remove the counter and once the counter reaches zero that reference is cleaned up now underneath reference there's a lot you probably haven't seen but you've probably seen some here under resource for example texture now resources themselves are probably the the more important one under reference to know resources are the ones that can be saved to the file system over here on the left whenever you create something in the file system be it this icon here that is a texture when you create a scene that is way down the list here near the bottom it's it's a packed scene let me see if i can find it uh there it is practicing so that's what this thing is here is a packed scene uh not technically a node in order to be put into the tree this packaging has to be unpacked into a node and then and then added to the scene that's if you use it yourself the long way so this uh resource guy here is basically anything you want to save to disk and resources are used in a kind of magical way if we go up to the search bar here and look at resource loader the resource loader is basically how anything is loaded off the file system so if we look at the methods that the resource loader has and we look at this particular one right here this is load anytime you do the normal load or even preload it is technically using this method right here you can call it a long way by doing resource loader dot load but a lot of people just use the shorthand now this particular method just takes a path to the resource on the disk so that'd be res colon slash icon.png for example a type hint a lot of times you don't really need that it can figure it out and then this last one here is a boolean called no cache now resource loader tries to be magical in a game you don't want to load resources over and over and over so if i'm using this icon here a whole bunch of times all over the game i want to just load the icon once and then have that reference i don't want to duplicate that icon over and over and keep loading it if i can help it so resource loader tries to do that for you and that is where this kind of no cache thing comes in so if i were to set this to true then anytime i did load rescul and slash slash icon.png i would get a new one i would get one that is detached and not the global cached version of it so if i were to load with this set to false so i was getting the cache version and i changed the icon in some way all of them would change you would see that change across the whole game if i did no cache equals true and then changed that icon none of the others would change just the one i have so that's how this no cache kind of works now let's double click on the icon here and over in the inspector you can see it pulled up if i expand this resource section and look under path this path right here is the resource path as it says when you hover over it the resource patch you can path you can think of as a cache key so anytime you call load it's going to use this path this resource path variable as a cache key so in other words let's go over to the main scene here and let's go ahead and attach a script we'll just call it name delete some of this garbage here now let's come into here and do a quick load of the icon dot png that's going to be in res colon slash slash now if i were to do this i would get just this global one and the resource path on it would be this so if i do that load and then i do a print of this dot resource path save that run it really quick we will see down here that it printed out res icon colon or yeah you know the path from the disk now let's go ahead and do it the long way without caching so i'm going to do comma you can add texture here and do true for no cache save that run it boom nothing printed out this time in order to see that better we can quickly make sure that that has quotes around it as you can see nothing's in the quotes so in other words this resource path is empty so the cache key is empty it's an uncached version so what does that mean what are the implications of that well let's get into that next okay so the next thing we're going to do is do some exporting in order to see how to use and abuse resources anytime you export something it gets saved to that scene file or resource whenever you save it to the file system so let's go ahead and do a variable call it icon and we'll just do a quick load of res colon icon.png and now i want to actually right click here to show in file manager it'll open up your project folder now this main.tscn is this scene file and i know it sounds scary but we're just going to open it in code and actually look at it so this is the scene file on the disk not very big and actually not that complicated i first want to show you this ext resource this is a line that basically says the resource you're looking for is not in this file it's external you need to go and load it from this path and all ext resources are basically loading a cached version it's going to the file system getting this cached global guy right there and this particular external resource is the script for this main node so this main node has a script attached you can see the node main right here of type node 2d it has a script variable it's set to the external resource number one id one is right here and as you can see this is that script of main.gd that one right here on the file system so that's how that scene that packed scene resource is being loaded is it's going and scrounging up the external resources and this right here is technically an internal resource which we'll see a bit more later there's another thing you'll see often in these files instead of external resource it can say sub resource and you'll see that in a minute here now let's go back to godot and let's add export in front of this it'll complain if i don't make it a type texture because you can only export certain types as you may have experienced a little bit and we actually don't need this you can do loads in that usual way but we want it to load from the file not load manually through code so if you click on main you can see the icon has appeared on the far right in the inspector if we drag actually let's really quick save go back to this scene file as you can see nothing's changed because we haven't put anything inside of icon if we grab icon drop it in hit save again and come back here boom it appeared and it appeared as an external resource that globally cached file system version is being loaded in when the scene is loaded in so in other words when we go back here and we close the main scene and then double click it it is reloading it from that file and it is getting this if you look the resource path is icon.png it is that version cached with the cache key of that global position now let's mess with this guy a little bit if you go back to maine and you click on the icon here the button next to it the drop down and you say make unique if you open up a resource here the path is now empty you have given up the globally cached version for a local copy click save control s and go back to the scene file and you can see here you change from an external resource to a sub-resource with all the data needed to load it you are no longer getting the globally cached version [Music] in order to understand the locally saved data a little bit better it'll be easier if we don't use a regular stream texture so let's go ahead and create a new resource here that is an atlas texture click create we'll name it atlas icon as you can see this is a dot tres that is a tamil resource or something like that so this is a resource file most of them will have a trace ending icon doesn't only because it is imported so the uh trace thing is actually hidden away in the dot import file so let's double click on this guy in the inspector over here you can see it has a atlas spot that is basically an exported texture variable on this resource so a resource can have resources on them we will drag the icon over so now the global atlas icon resource so if you look at its path it's got the global cache key there its atlas is going to be the global uh icon so let's go ahead and just select some part of the icon here you can see the atlas texture changes accordingly save that and if you wanted we can actually go to the file system here and open up where is it at this icon on trace and you can see uh the icon is an external resource so it's loading that global cached version saving us load time underneath the resource here is its actual local data under atlas here is external resource one that's this guy right here when it's loading this atlas icon resource it's going to go out and get the globally cache atlas texture region relates to this guy margin doesn't have one because it is the default values therefore it doesn't need any saving to the file so going back to main if you were to click and drag atlas icon onto icon click save and you were to go back to the main scene file you'll see it's an external resource cool if you go ahead and click on the atlas icon on the file system right here and you are again to change its bounds the region click save now if you clicked on main if you notice it's updated here it's because that cached version is shared if we were to click the little arrow next to it and do make unique you are breaking the connection this is now underneath path it's empty it's not the cached version click save this now gets filled and if you notice it is now main dot tsen colon colon one that means it is sub resource number one if we open up the file here sub-resource one is the atlas texture right here if you were to go back here and change its bounds its region click save you would see that updated version in the resource file here which doesn't match the other one here 17 11 31 or 27 the last one 43 and 27 i changed the height so in other words if i click on here this one has the old region the main has the shrinked version they're disconnected you don't have that globally cached one if you were to go to the script duplicate that and make icon 2. then go back to main we now have a second icon here if you were to click the drop down click copy and then paste now both of these are main tscn1 both of these so those are cached at this key they are both shared in the scene if i change this one and save they both update sometimes if you change you won't see the change unless you open and close it again to kind of refresh the inspector's view of that and there's a bug out to fix that so i wouldn't worry too much but you can kind of see how the caching works if you were to open up here you can see that this sub resource of the atlas texture is just in the file once but it is used twice for these two different variables if we were to go in and click and make unique on the second one and then change its region these two are now disconnected they are different icon atlas textures and so if you go to the file we now have two sub resource atlas textures with a different height [Music] hopefully this kind of highlights and helps you understand how resources get loaded saved how they work how the caching works you folks are interested i'll probably do another video on custom resources let me know if you want that thanks for watching
Info
Channel: Cory Beutler
Views: 210
Rating: undefined out of 5
Keywords: Godot, resources, memory management, garbage collection, memory, custom resources
Id: -3xW3n9-HBw
Channel Id: undefined
Length: 18min 58sec (1138 seconds)
Published: Wed May 19 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.