Make A Game Like Pokemon in Unity | #48 - Persisting Scene States

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

This is part 48 of my Pokemon Game tutorial series in unity. In this video, we'll look at how to persist data between scenes. So before a scene is unloaded, we'll store all it states, and we'll restore it when it's loaded again.

If you're new to it, this a series where we'll create a Turn-Based RPG like Pokemon in unity using Design Patterns and Good Game Development Practices. I'd love hear what you think about it :)

Here is the entire playlist

👍︎︎ 4 👤︎︎ u/GameDevExperiments 📅︎︎ May 15 2021 🗫︎ replies

Cool as.

👍︎︎ 2 👤︎︎ u/ultimaIV 📅︎︎ May 16 2021 🗫︎ replies
Captions
hey everyone welcome to part 48 of my pokemon game series in unity so in this video we will look at how to persist the state between scenes so before we unload a scene we will store all the state in it and we'll restore them when they are loaded again so we look at how to do that special thanks to all my patreons for making the series possible by becoming a patreon you can support me in the making of the series and get access to some cool rewards like the complete project files of the series which also contains some advanced features that are not covered here so let's start the video so let's look at how to save data between scenes so right now when we move to a different scene we will unload all the scenes that are not connected right so if i go from route 1 to hometown you can see that the town 2 scene was unloaded since it's not connected to hometown so the problem is when down 2 is unloaded it'll lose the state of all its game objects so what we have to do is before we unload a scene we have to capture the states of all the objects in it so let me explain this issue with an example let me just maximize this and i have a trainer over here in route 1 scene so if i go battle this trainer and beat him okay so i beat the trainer so now the battle loss state of the trainer will be true right so the problem is now if we unload the root one scene this trainer will also be unloaded and it will lose its state so let me show you so if i go to the house one scene so if i minimize this so if i enter the house one then root one scene will be unloaded right so yeah it's unloaded and now if i go back to hometown it's loaded again but it'll lose all its state right the state of the trainer over here along with all the other objects in that scene will be lost so now if we go and speak to that trainer you can see that he challenges for a battle again since he lost his state so what we have to do is before unloading a scene we have to save the states of all the objects in it so let's look at how to do that so i'll open my scene detail script and in here before unloading the scene we have to capture all its state and after loading the scene we have to restore all states that's how we can implement this so how can we find all the objects in the scene whose state should be saved so if you go to a scene you can see that all the objects that has savable states will have disable entity script attached to it right this is how we are telling the saving system that we need to save this object so what we can do is we can find all the objects in the scene that has the savable entity script okay so after loading the scene i'll find all the objects with the save entity script so i can easily do that by using find objects of type right and for the type i'll pass say april entity so this will return all the objects with say a plenty script but there is a problem we only want to find the objects in the current scene right in the current scene that we are loading and usually find objects of type will only load the objects from the current scene but the problem here is we are using additive scene loading and we have multiple scenes open at the same time so now this function will load objects from all the scenes not just the current scene that's loaded so these are some of the troubles of using additive scene loading here if we want to get just the objects from the current scene then we'll have to write some additional code so to do that i'll add bare condition so in order to use the wave function we have to import link and here we have to check if the object belongs to the current scene right how can we check that so every game object has a property called scene so if i say x dot gameobject dot scene so yeah you can see the game object has a property called scene which specifies the scene it belongs to okay so we just have to check if that scene is equal to the scene that is currently loaded so right now we don't have a reference to the current scene we just have the name of it okay so we can actually get eruptions using its name so for that i'll say scene manager dot get scene by name and for the name i'll just pass game object dot name okay and i'll store this in a variable called scene let me change that to current scene for clarity and here i'll check if gameobject.scene is equal to the current scene so now i can just use to list function on where and get a list of savable entities in the scene all right so i'll store this in a variable called savable entities so let me just encapsulate this code into a function to keep our code clean so i'll create a function called get savable entities in scene and this function is going to return a list of saved entity okay i'll paste those two lines here and at the end i'll just return the saved entities all right so now from here i can just call get saved entities in scene and get a list of saved entities okay so after we load the scene we have to restore the several entities and before we unload the scene we have to store the states of the sample entities somewhere so we have to call this function from the unload scene also so to avoid calling this twice i'll just store it in a global variable while loading so i'll create a list of saved entities as a global variable and while loading i'll save it into that let me just call up the spelling so before unloading we have to store the state of disabled entity somewhere so to do that we have a function inside of a saving system so if you go to the same system script here we have a dictionary called gain state which can be used to store the states of all the objects whose state has been changed right and this is a function used to store the state and this is used to restore it okay so before unloading the scene we can call saving system dot instance dot capture entity states and for the entity states i'll just pass by disable entities list okay so we are capturing it before unloading the scene next we have to restore it after we load the scene right so i'll call saving system dot i dot free store entity states and i'll pass saved entities so now we are restoring the entity states after we load the scene but there's a small problem are we really restoring it after we load the scene no if we write these lines over here the restoring might happen even before the scene loading is complete so why is that so the reason is load scene sync is actually in a sync function okay and what's nursing function essence functions are functions that are executed in the background right they are executed parallely without blocking our current script so here you can see this function loads the scene asynchronously in background so what happens is even before the scene loading is complete it will go to the next lines right and it will execute trust of the code so in our case it will be a problem because we only want to try and restore the entities once the scene loading is complete so how can we do that so if you look at the load sensing function you can see that it returns a class called sync operation so first let's actually store the return value in a variable i'll just call it something like operation and inside this async operation object we have a way to check if this operation is complete or not okay so if i say operation dot completed here you can see we have an event called completed so this will be triggered once this operation is complete so let's actually attach a lambda function to this event so we have an error here since the action in this event is expecting a parameter of async operation so i'll just add that parameter there so now this function will only be called once this operation is complete which means once the scene loading is complete so we can move these two lines into this lambda function and now it'll only be called once the scene loading is fully complete all right so this is all we have to do we are storing the entity states before unloading the scene and we are restoring it after loading it so let's go to unity and test if this is working oops i want to run the gameplay scene instead okay so first let me go and beat this trainer real quick so now if we unload the root one scene by going to some unconnected scene let me actually minimize this and show you so yeah now if we go to house one root one is unloaded but hopefully this time all its states should be saved before unloading so if we go back to hometown root one is loaded again and hopefully all its states should also be restored so let's go talk to the trainer and confirm that so yeah trainer doesn't challenge me to a battle so its state is stored and restored correctly okay so one thing to note is that even though we are storing the state into a dictionary we are not actually saving it into a file okay the saving will only be done when you press s and save the game all right so now we have a system which will automatically persist the states of all the game objects in a scene even if the scene gets unloaded so next let's fix another issue that we have so if i go to house one scene and save and then if i go to a scene that's not connected to house one so i go to root one root one is not connected to house one if i minimize it you can see that house one is unloaded since it's not connected to root one so now if i load the game we should load back in house one right so i'll press l and we are back in house one but the issue is the root once scene is still not unloaded right it should be unloaded since house one scene is not connected to root one so the issue is when we load the game the scene in which we were previously on will not unload even if it's not connected okay so this is not a big issue or anything but let's say if the previous thing was a huge scene then it's gonna have some impact on the performance so let's look at the code to see how we can fix this issue so in our scene details script whenever we load a new scene we are also storing an instance of the previous scene right and then we are unloading all the connected scenes of the previous scenes if it's not in the connected scene of the currency okay so we are unloading the connected scenes of the previous scene but we are not unloading the previous scene itself so we didn't do that because until now the previous scene would always be a connected scene to the current scene right we can only go from one scene to another if they are connected so until now it was like that but now since we implemented saving and loading we can load the game and go to a scene that is not connected to the current scene right so in that case previous scene won't be a connected scene so we will have to unload the previous scene also so we can just unload the previous scene by calling previous scene dot unload and call previous scene dot unload scene function to unload it but we only want to do this if the previous scene is not in the connected scene right let me actually store this in a variable so that we don't have to type all this every time you want to access the previous scene so i'll store it in a variable called previous scene now we can use that variable instead all right so we are unloading the previous scene but we only want to do it if the previous scene is not a connected theme right so before doing this i'll check if previous scene is not in the connected theme all right so now the previous scene will also be unloaded if it's not a connected scene so let's go ahead and test this i'm gonna go ahead and minimize this and now if i go to root 1 and press l to load my last save you can see that root 1 has been unloaded correctly so that issue is fixed so i'll stop the video here if you think these videos are helpful please leave a like and consider subscribing to my channel that'll really help me out so i'll see you in the next video
Info
Channel: Game Dev Experiments
Views: 1,567
Rating: undefined out of 5
Keywords: make a game like pokemon in unity, how to make a game like pokemon in unity, make pokemon in unity, make pokemon game in unity, how to make pokemon game in unity, make a pokemon game in unity, unity pokemon game tutorial
Id: 1VQnA5_0Y_s
Channel Id: undefined
Length: 19min 9sec (1149 seconds)
Published: Sat May 15 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.