Godot 4: Better Level/Scene Manager

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello this is Patrick with royes today we'll be learning how to make a level scene manager for Gau this will help simplify loading and managing your level scenes all right let's Jump Right In so as you can see I've got three scenes here uh level scene manager scene a level one and a level two scene so the level scene manager is just this node 2D that I have a script attached to and then your levels after that can be whatever you want them to be so I'm going to go and open up this script and I've gotten rid of some of the EXs codes so the comments and stuff from here so the first things first is we're going to want to get a list of the scenes that we want to load so to do that we'll go using G do Collections and then we will do export array and then we will make this take in a type of packed scene then we will call these um edit editor scenes then we will export uh actually I'm going to put this up above a int and then this will be the uh index of the scene we load first scene index we'll call this and we'll set this by default B zero all right now that we've got the scenes from the editor in here we will go and we'll have to try and make them into a different format so that we can work with them easier and it'll do a few more things for us so we'll go public class we'll make a class in here and we'll call this loadable scenes or scene and then in this we'll have a public string scene scene name and then we'll have another public packed scene and then this will be the scene we'll make a Constructor for this so we'll go public loadable scene then we'll take in a string scene name and then we will take in a packed scene and we'll call this once again scene so now what we'll do is we'll say this do scene name is equal to scene name so the scene name this one is equal to this one and then we'll say this do uh scene is equal to scene doing basically the same thing there so now we will have to put these packed scenes into to this loadable scene format so we'll go up here and we'll change this to system do collections. generic because we'll be also switching this away from the gdau array system because when you hover over this it'll tell you that you should probably be using uh C lists rather than gdau lists unless you're interfacing directly with the uh game engine and we won't be doing it after this so so we'll go private list and then we will call this or have load uh take in loadable scene and we'll call this loadable scenes and set this equal to new list loadable scenes all right now what we need to do is populate this list so we'll make a function down here called private void populate loadable scenes list and then will go for and we'll set this the length to be the editors scenes. count so it won't go over the amount of scenes that we have in this and then we'll say if editor scenes I equals equals null we'll continue so we won't add this to the loadable scenes list if it's null then we'll make another function down here because we'll make a function to add them to the list we'll call this function public int add level scene to scene list then we'll take in the parameters packed scene oops we'll call this scene now we'll say if scene is equal equal to null we will return minus one which will mean it failed now we'll want to get the name of the scene that we're getting or we're adding to the list to populate this field so we'll go string name is equal to scene doore bundled and then bundled has an array of like a field that's called names and that's an array of all the node names within the bundle or within the scene sorry and then we'll say as go dot do collection do array and then we'll type and then we'll just get the first one so that'll be if we go back into Gau here that'll be like this node or in level one it'll be this top node that's called level one so that'll be the name of our scene and then we'll have to cast this again so as a string so that it knows what this is and there we go oh wait we can get of this since I got rid of the systems library in that up here perfect okay so now we're going to go and loop through all the scenes we've got so four and then we'll say loadable scenes. count now we just want to make sure that the name of the scene is unique cuz we can use the the name to identify each scene so we don't want any duplicates of the names so we can go if name is equal equal to loadable scenes I do scene name then we will print an error out and return so GD do print error duplicate scenes with the name then let's just depend then name then we will return minus one all right so now that that is done we can go loadable scene loadable scene is equal to new loadable scene lots of that and then we'll put pass in the name and we will pass in the scene now perfect we've got that in the format we want so now we'll go loadable scenes do add and then we'll add the loadable scene and then we can return the loadable scenes. count minus1 since this is adding it onto the end this will give us the index that the scene is stored at within the list so if you want if you added something to the list and you wanted to keep track of what position it's at you'll have that all right now we need to add this back to the or now we need to add this scene to the scene list so we'll go add level to scene list and then we will go editor scenes I and there we go so now we'll have a list of all the scenes from the editor that we can now load that are now ready to load now we're going to make three functions all with the same name and these will just be three different ways of loading a scene so we'll go public load or we'll just do a public void for now and we'll change this later public void load level scene and then we'll pass in an INT and we'll call this scene index and this will be the index of the scene that we'll load so we'll just check to see if the scene index is less than zero or if the scene in index is greater than the loadable scenes. count and if it is we will return now we will um make another function we're just going to override this a couple of times and each one will do similar things but so we'll go a public void load level scene and this time we'll take in a string and this one will be a scene name and then we'll do another one and this one will be the final one public void load level scene and then we will take a loadable scene and then scene to load Perfect all right so now we're going to finish this one and then we'll go back and finish the other two functions after this so we'll say if the or if not the scene to load do scene and then we'll check if we can instantiate so if we can't instantiate it we will return now we'll go um int last scene actually we don't have a storage to the last scene we've loaded yet so I'll do that later so we'll say node not name node new scene is equal to scene to load do seene do instantiate now we'll just check again so if new scene is equal to null we will return just in case it failed at instantiating it or something and then we will um add Child new scene then we will print the scenes name so we'll just say GD do print and then scene to low. scene name we'll print that out and I guess let's go finish these functions now first one let's go back to this one and finish it and so we'll just go load level scene and then we will get the loadable scenes and then we'll just pass in the scene index and then this one is done so if we want to get uh loaded level seen by the index we'll just call this function and it will load it for us now we'll have to check the name this scene name to see if it matches the scene name of the level scene that we want to load so we'll go four and then we will type loadable scenes. count and then we'll see if the scene name matches of the index matches the index of the scene that we're on so we'll go if loadable scenes I do scene name is equal to scene name then we will load uh level scene loadable scenes and then we'll pass in or we'll send the one at the index that we're currently at and then so we're not continuously looping through we will return all right so let's test this out so first things we need to do is in ready we will populate loable scenes list and then we will call the load level scene so load level scene and we will do it with the index for now I guess or I guess we've got this up here first scene index perfect so we're we'll go back to G do I'm going to run it just quickly to get it to show the updates here so that we can actually add the scenes to the lists there they are and and let's add both the scenes to the list so we've got level one you'll go in the first position level two you'll go in the second so let's run this again and it loaded the first scene now let's change this to one and try it again and it loaded the second scene perfect so everything's working so far all right so now what we're going to want to do is get a reference to the current scene that we have loaded so we can kind of just um manage that so if we want to like switch scenes or something we know exactly which scene we're switching from and to and yeah then we can do all kinds of cool stuff with that so we'll go down here we'll make another public class and we'll call this loaded scene and then we will have a couple fields in here here so we'll have a public uh let's go public int scene index and then we'll have a public string scene name and then this is why we'll have a different class for this is we'll go public node scene rather than a pack scene we are having a note so we'll make a Constructor for this as well we'll go public loed scene and we will take in all three of these so we will go int scene index we will take in string scene name and then we will take in a node scene okay we'll do the same thing again where this do scene index is equal to scene index we'll go this do scene name is equal to scene name and we will go this do scene is equal to scene all right perfect so now what we'll do is we'll go up here and we'll create a per uh let's do this public actually public load loaded scene not loadable scene and we'll have this be current scene and we will have a um let's say get private set so externally from the class we can get it but we can only set this parameter or this variable inside the class itself so I guess the current scene we will go into here in this one and say if current SC scene does not equal null while we'll have to unload that scene so let's do that so we will go current scene do scene. Q3 and that will unload the scene for us and then we'll set the current scene to be equal to null now we will go down here and type current scene is equal to new loaded scene then we will get the scene index so we'll actually need this to be passed in here so we'll go int scene index so we'll take the c Index pass that in we will take the scene to load and then pass in the scene name and then we will pass in the new scene and there we go that will run that correctly and we may actually want to return this new loaded scene that we've got so we let's do that so just so that the you have them so loaded scene and then we will go return current scene then we'll switch these to returning null now we'll have to update these let's update these with the uh Lo returning loaded scenes as well perfect okay and then we will have to return null here we will return this since we're getting the loaded scene from this function we will pass in the scene index and we will do the same with this so we will turn here pass in I as the scene Index this return is now redundant and we will return null down here it since it couldn't find the scene in the list it failed now that we've got adding and loading scenes to the list and um or now that we've got adding and loading scenes sorry we should be removing scenes so let's go down here and give you I'll make a couple functions to remove scenes so we'll go public weol move uh level scene from list these will be very similar to these load scenes up here where we'll uh be able to remove scene by index or by name so we'll go int scene index and we'll just write the other one down here so I'll just copy this and we'll change this to string scene name okay so we will go and actually just copy this if statement up here no need to write that again paste that and we will return false now we'll go loadable scenes remove at scene index and then we will return true now we will have to do the four here and then we will Loop through the loadable scenes do count and we'll see if the name matches so we'll go if loadable scenes I do scene name is equal equal to scene name then we will remove at um so we will go loadable scenes. remove at I then we will turn true if not we will return false now there's a problem here because of this the current scene that we've got stored may not since it's got the scene index saved to it that may be shifted now so we may have removed the scene behind it and so now the current scene is say at index 3 when it was at index 4 before so we're going to have to readjust that so let's make a function called private void adjust and level scenes X cool now to do that we'll just do another for Loop and then we'll go loable scenes. count and then if the loadable scenes dot or I do scen name is equal to current scene. scen name see this is why we allow you to check by name and by index just in case it gets thrown off then we'll say the current scene do scene index is equal to I and we'll return since we've already found it and we don't need to Loop through anymore so now we've got adding removing all that good stuff from the list if now this is like super expandable you could add like um signals so if you wanted to be notified every time the scene changed or every time a scene was added from the list or whatever you could could add signals to these things like a signal here a signal here and a signal here and here to let you know that those things have happened but yeah this is quite expandable so let's uh do a couple more tests I guess so first off let's just do this with a string to make sure the that work so we'll go level one and it loaded level one Perfect all right so now let's try adding a a duplicate scene so let's add level one twice and see if it'll accept that so we'll do that and it detected that perfect all right so let's remove that let's say H what's something else we can do I'm just going to switch this back to the scene index I guess we can do public override uh process and then I'm just going to do a double here time and and that to zero okay so time plus equals Delta and a if time is over a second we will just switch to scene two I guess I'll also do a buol as so it's not constantly loading the new scene after the time Hits 1 second so if time is greater than or equal to one and has switched is equal is equal to false then we will switch the scene so we will go and load scene two so load level scene then we'll load scene two so let's see if this works uh whoops has switched is equal to true and it's just oh cuz the first scene yeah okay that makes sense okay switch that and boom perfect we've got Scene switching going all right I hope you like the video if you liked it please like subscribe thanks bye
Info
Channel: Royas
Views: 568
Rating: undefined out of 5
Keywords: Godot C# Tutorial, Game Development in Godot, Level Scene Manager Godot, Godot Engine C# Programming, Godot 4.2 Tutorial, C# Game Development, Godot Beginner Tutorial, Godot Scripting, Godot 4.2 C# Basics, Godot Custom Level Manager, Godot Game Design, Godot Programming Tips, C# Game Programming, Godot Engine for Beginners, Game Dev Tips and Tricks, Indie Game Development, Godot Open Source Engine, C# Scripting in Godot
Id: B37cLqcEtpk
Channel Id: undefined
Length: 28min 1sec (1681 seconds)
Published: Fri Dec 22 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.