How to Pass Data Between Scenes in Godot (Building a Level Switcher #3)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video we are going to finish the series of switching scenes that we've been doing in the last two videos by talking about how to send data from one scene to another and so here you'll see i've added this clicks label and this button and if i click it this counter will increment by one now if i change scenes to our desert scene you'll see that the clicks persist so we're sending the data from our clicks in one scene to the other and if i continue clicking so we're at 14 and i change the scene again we continue to persist and transfer that data to new scenes let's get started so the first thing that we want to do is actually add some piece of data that we can pass between each of our scenes our grass scene and our desert scene in order to do that we're just going to do a classic and make a button that increments a counter by one so in order to do that i'm coming to our grass level here and what i'm going to do is just copy our change scene bun and our label so i'll take both of them and just hit command d or ctrl d to copy them and i'm gonna have the move key and i'm just gonna hold down for a little bit until they go below slowly so slowly okay and then i'm just gonna have a label here and this is just to be clicks and it'll say zero and i'm going to center align this text and then our button here is actually going to say click button and i'll change our our name of our button here our text here we'll say that there we go okay and so now what we can do is actually click our click button here and i'm going to connect our or disconnect our pressed signal here because it's still because we duplicated our button it's connected to our on change scene button press signal from the previous videos so i'm going to disconnect that one and then reconnect it to a new method which we'll just use the default on click button pressed and so what we need to do here is first add a variable that we're going to use to keep track of our click so i'll say var clicks and this is just going to be zero it'll be an integer and then whenever we do on click button pressed in this function whenever we click the button we'll say clicks well actually let's make another function we'll say like increment or set clicks and this is going to be the current clicks plus one so clicks plus one and so we need to make the set clicks function i'll just make it right up here we'll save function set clicks and this will be new click amount this will be an integer we can give that a type and in this set clicks function all we're going to do is say clicks equals new click amount another thing we need to do is actually update the label so i'm going to say label 2 i guess is the label first let's give this a better name we'll say click label so if i go into our code here we can change this to be click label and we'll say click label that text equals and then this is going to be clicks like we currently have space and then we will just add in clicks here okay so that's simple and oh we need to wrap this in a string call to convert from integer to string and now if i run our grass level here and open it up you'll see that if i click this button oh and whoops we had this disabled by default on our previous video so we'll need to make sure that's enabled too so if if you're having that problem just make sure you enable your click button so let's add these the button and the label to our desert level we did this in a previous video but we'll go to our desert level right click on it and then hit merge from scene select our grass level and we can merge in the click label and the click button perfect let me just move these up so the scene tree looks the same and again we're going to need to make sure we connect our click button here because when we merge it over it won't be connected by default so we'll just double click pressed and we can just hit connect the default value will be perfect and it'll connect right into our same function that we're using for the for the grass level so now again if i run our scene switcher scene i can click a few times but when i change levels we are not going to have those clicks persisted because we're not saving them so how do we pass this data in between scenes well there's a couple ways to do it one way to do it that's quite common is to use what's called a singleton a singleton is a node or a script or an object that lives and there's only one of its kind there's only one instance of it and it usually lives or exists at the top of your tree or as a global object that any tree can act or any object node or scene can access and so what we would do is we'd make the singleton and godot they're auto loads is what they're called um or that that's how you create an object that loads in at a global level for your whole game and so we would create an auto load script and then we would have our clicks variable live there and so now whenever we increment it or hit the click button we would increment our clicks variable in our global script in our auto load now that's an okay solution but if you're doing that for every piece of data in your game it's going to be really cumbersome every object in your game is going to have access to all of your data which you definitely don't want that's kind of dangerous and not very good architecture another problem is that you're just going to be it's going to take up more memory than you need you're going to have access to that everywhere in your game when at a given time you're only going to need a subset of all of that data really even though there's definitely room for global variables and storing global values in your game whenever you can store something locally you want to do it if it makes sense and is easy to do and it's pretty easy to do for us so we're going to come up with a way here to actually pass some information in to another scene from our current scene and in order to do that we're going to leverage some of the architecture we've already built and so if you're trying to do this um and you haven't followed in the previous two videos that's okay you'll be able to implement it and make it work in your own level but the scene switching system that we've built over the last two videos is really going to help do that otherwise you might have to do a little bit of adapting on your own but you'll still be able to do it no issue so how can we transfer this data from one scene to another well the way we're going to do it is actually to do it in our handle level changed and so what we're going to do is make a new function here we're going to say function transfer data between scenes and we're going to need to pass in two scenes here the old scene and new scene almost i almost typed and there don't want to do that so when we do this and we're passing in our old and new scene here is where we're going to want to transfer all of the data from one to another and now what we're going to do is actually call this new function this transfer data between scenes here when we are in our handle level change function so as soon as we connect our next level and do all this or as soon as we load in and instance it then we are going to transfer data between scenes so what i'm going to do is after we've connected this here i'm going to call this function and we need to pass in two things our current level and our next level and so remember current level will be our old scene next level will be our new scene slightly different naming but it's the same thing going on and now within this function we can handle all of the data transfer from one to another that we need to do so how can we do this without making a global singleton or an auto load and passing it in that way well the simplest way to do it is actually really really easy we can just say new clicks new scene.clicks because remember on our level clicks is the name of the variable that we made we can say new scene.clicks equals old scene.clicks so we're just going to set the value of clicks on our new scene to what our value was on the old scene and if i run this you'll see that if i click three or four times six we'll stop at six and i hit change scene you're going to see two things one is that our clicks is zero so that seems incorrect but if i actually hit click me it bumps up to seven and if you remember we were at six this makes sense so the value of our clicks is being transferred correctly but we're not actually initializing the text of this label the way that we need to and so in order to actually fix that we're going to slightly change how we're doing this here to make it a little bit more robust so that if eventually we have many or multiple types of data we want to transfer over not just one variable we'll be able to do that really easily so how do we have a little bit more robust of a transfer system that isn't just one value equals another well one thing that we can do is actually in level we can change it from just being a variable clicks to we can say level parameters and you can change this name to be whatever you want level parameters i think just kind of gives it it makes it clear that these are the parameters of the level or this is the date of the level but really what it's storing is the data inherent to our level that we need to pass into another and so what we're actually going to do is change this to be a dictionary not just a value so now we have a dictionary here and within it we are going to create a value and it's going to be clicks so we'll have a value of clicks which is just going to be an integer here and now when we do set clicks we need to do level parameters dot clicks equals new click amount and then same thing here we need to change this to be level parameters.clicks and so really what we're doing is we're kind of obfuscating here we're making this more complicated than it needs to be so for our simple example where all we care about is clicks this is more complex than it needs to be but what i want to show you is a way that you can create and pass through complex data types or multiple pieces of data if you've got a more complex use case than our game has which is probably the case most games are going to want more information to transfer between scenes than our simple dumb little game here but just to show you how this works we're instead going to have a variable called level parameters that's a dictionary here for us it'll only have clicks in here but you can put as many variables as you want in this dictionary and so what's going to happen now is we're actually going to say new scene.level parameters equals old scene.level parameters and okay so so far this is looking exactly the same as it did before but let's add one more change that's going to make it a little bit more robust what we're going to have is another function and we're going to say function load level parameters and we'll say level parameters and this is going to be a dictionary and to differentiate this parameter from our actual variable we'll say new level parameters so that they have different names and what's going to happen is we're going to say level parameters in this function are equal to new level parameters so again very simple we're just setting them equal to what we're passing in but then we're going to be able to now within this function do any initialization that we need to and so what we can do is actually borrow line 31 down here where we're setting the text of our click label we can now set this so that whenever we load in our level parameters we do the initialization and do the updating that our level needs to reflect the parameters that have been transferred into it and so now whenever our level loads and this function is called we're actually going to update the text right away and so not only are we passing the parameters through now but we're actually giving ourselves a space in this function to do the initialization that we need the only thing we have to do now is actually call this function so i'm going to copy and paste or copy this function name come into our scene switcher and then i'm going to instead of saying new scene.level parameters i'm going to call this function new scene load or dot load level parameters and pass in the old scenes level parameters and so again in review this is not that complicated it's pretty simple so transferring data between scenes in godot is relatively simple and you don't have to do it this way you could like we mentioned before do it via a global script and we'll come back to the reason this is so simple and the reason this works in just a second but let's make sure this is actually working so if i run our scene switcher we'll see i'll click three times and then if i change our scene we have three clicks if i click another few times so we're at six and change our scene now we're at six again and so look we're able to pass in data between our scenes so this is really great we've created a simple way to pass data between our scenes and because we are using a dictionary and passing in a dictionary between our scenes to add new pieces of data to give between one scene and another is as easy as just adding another property on this parameter and it'll automatically get loaded in anytime we switch scenes and the reason this is so easy the reason that transferring data was so such a simple solution is because of the scene switching system that we've built up it was really easy to just plug in this transfer data between scenes function and just have it work because of the system we've built around transferring scenes here or switching scenes one important thing to know about why this is so easy because i think a lot of people that are new to godot struggle with how to send data between scenes one important thing to note is that we are letting a parent node handle the transfer we are not communicating directly from one scene to another but rather it's within this parent node this scene switcher that's telling the new scene to grab the parameters from our old scene so we have a parent node that's handling that transfer and this is crucial this is imperative and key for anyone who's new to godot when you're doing level switching having a parent node like our scene switcher here or like the main node in your game or another manager or something you can call it whatever you want but letting a parent node handle communication between siblings is crucial in godot the best way to find success in a commonly said paradigm throughout many godot developers in the ghetto community is to call down and signal up so if you're a parent you can call directly into a child node but if you're a child you need to use signals to communicate up the tree that's why using a parent node to talk between sibling nodes who are both children of the same parent is really powerful because our scene switcher is able to call directly into both our new scene and our old scene and so rather than having to wire up signals between our old and new scene which is a totally valid way to do this another way to send data between scenes would be to wire up signals between the two but having a parent scene that manages that data transfer is much easier than doing that so just a little bit of explanation about why we did it this way and why it's so easy why i think this is kind of the best way to do transferring scenes or to switch scenes and transfer data between those scenes is to have a parent node that handles the switching and handles the scenes actually being its children and then handles transferring the data from one scene to another so anyway everyone pretty quick simple episode but wanted to give some explanation in this tutorial of why we're doing the things the way we are and to kind of give a climax to the previous two videos that i've done as part of this scene switching series to show why this is uh this this method works in the long run and why as you continue to expand on your game having a scene switching system like this one will pay dividends and make your code a lot easier to read and expand on later on so thanks so much for watching i hope you found this video helpful if you have a like and subscribe are always appreciated we'd love to have you in our discord server the link to that is in the description below we'll answer any questions you have in there and if you do find my work helpful donating me a coffee on buy me a coffee helps me continue to make great videos the link to that is in the description below as well thanks so much for watching everyone i'll see you in the next video
Info
Channel: jmbiv
Views: 4,742
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, indie game development, hobby game development, gamedev, godot game engine, jmbiv, godot change scene, godot switching scenes, godot scene transition, godot level transition, godot scenes, godot scene change, godot level change, godot transition between scenes, gdscript
Id: N4iV1L6xb04
Channel Id: undefined
Length: 15min 54sec (954 seconds)
Published: Thu May 06 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.