Godot 3.2 - Changing Scenes Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone today I'll be showing you how to change scenes in the Godot game engine so here's the game I've been working on recently and the changing scenes feature will kind of look something like this so I'm walking through the end of the map and then while I walk through up it's the wrong way so I was on the wrong branch here's the working branch so as you can see as I walk through the end of the map I'm able to go to the next map and if I go to back I'm able to go back to the next map and notice how the position of the character and also the direction he's facing is also in the correct way this is what I'll go over today let me just exit out let's come into our scenes in which we want the transition to be it so here in my game I have a mind scene which is one map and I'll press ctrl-a to add a new scene and I'll add an area 2d note and let's name this area 2d portal and within the area 2d we need a collision shape 2d and we'll give it a shape come in here new rectangle shape 2d would work just fine for a use case and I'll resize it to something like this and then I'll put it in a spot where we want the player to walk through so it can trigger the function to transition to the next scene well something like this would work just fine and we'll come to portal and then right-click and we'll say branch as scene and we do this because we want to be able to reuse this scene in multiple maps great so now we all right click and actually we'll click this edit button so we can just edit this scene so let's create a new script for our portal click on portal and then click add script and then create and let's get rid of all this stuff the top of the script let's add tool and I'll tell you why we add tool later on first we want to create a variable call next scene path which tells us which scene we want to transition into and the default value will set it to an empty string and then we'll add another function called get configurations warning and we check if the next seen path is empty then we give the user our warning and this is where the the tool keyword comes into play it tells the editor what warning to give portal to work a string like that would be just fine and else return nothing ctrl s to save and then when we go to portal when we click here we're able to see the script variable next scene path and now we're able to edit whatever map we want easily and quickly let's go back to the script we want to create a signal to let the portal know that the player entered its area and to do that we click area the area to denote and then we go to the node tab on the right hand side and since the player is usually a kinematic body type and if it's an area type you will click this but usually it's a body so we'll click body entered double click that and then we'll click portal on the top and then click connect so in this function we want to change the scene to whatever is set as the next scene path to do this we will first get the tree by doing get tree and then we'll call call the function change scene and within it will pass in the next scene string and the change scene function returns an error message so we can get the value of it actually I don't need parentheses and then we'll check if it's equal to ok if it's not that means something went wrong or the next scene path is invalid so we can do some error handling here right now just print an 8 available C and type in air now we want to assign collision masking so that the portal only looks for the player body and the player body is also looking for the portal to do that we go to project settings and we'll scroll down all the way to layer names 2d physics so I have other layers and the only layers that you should care about is the player layer which you signed to your player and also the portals layer so to add a portal to add a layer you just type in a name and that's pretty much it we'll click close and then we'll click on portal to edit the scene button click on portal again and then go to inspector and in the collision tab we can set which layer is currently on so the portal would be on the portal layer that we created and we'll get rid of that so that's what layer means what layer is the current node on and mask is you can essentially think about it as what layer does it look towards so we want to look towards the player layer I'll get rid of the other layer that's selected this helps us as now the portal only looks for the player and not all the other nodes in the scene and this helps speed up our program so let's open our player scene we're seeing right here and then in the collision section we can see that the player layer is selected for the layer and for masks it's on the world and it's also looking to the portal layer so make sure those options are selected okay so now let's go back to our portal will control us to save and then we'll go back to the scene in which we encapsulated the portal which in my case is a the mines scene and then let's click click on portal and let's set the next scene path and the map I want transition to is I have a scene called route which is another map of mine we click that so that's set and let's try running this I spawned so one thing that I forgot to do is also add the portal in the other map which is my roof so let's quickly add that wheel instance will click on route first and then instance a child scene and we'll type in portal portal so we got portal and it is right here I'll put it there and then also we have to set the next scene path parameter and we'll set it to the mines so it goes back and forth click OK now that we have the portal scene instance in both our maps one other thing that we need to specify is where the player is actually going to spine we didn't specify a position an easy way to do this is to auto load a script and auto load is essentially a global script which can be referenced by any node in the game so you can create a new script called global and I have one already set up here and all we really care about is a variable called player initial map position so this specifies where the player should spawn so right now I have it arbitrarily set around a value and then within our portal scene let's go to the portal script let's create another export variable and this would be a type vector to player spawn location and then we will set it at vector to zero initially and then here we want to set the global variable so it's a global dot let me go back and get the correct variable name blow it up player initial map position and then we set it to the player spawn location that we set so now this is setup but we have to make sure that in our ready function for our player node we set the location to player initial map position so let's go to our player node and we go to our ready function in our player node you can see that I have self global position which is a player's location and we set it to the global variable player initial map position so this should set the location of the player and now if we go to the mines scene we can see that there is a script variable called player spawn location now we can set this differently for any scene that we add the portal to so within our mind scene we have to set the player spawn location to where would be in the root scene so we go to root 2d and we want the player to spawn somewhere around here and then we look the position is 1005 and 76 so let's go back to the mines and then we have set X to 1005 and then Y to 76 so now we have to do the same thing for the root portal so we go here player spawn location let's go back to mines and figure out a good spot somewhere here is fine and we will check the position where it's at 79 78 79 78 so now let's try running this let's keep going to the right and cool so we're able to spawn in the position that we specified we go back and we go to the route scene don't you keep going back and forth one more thing to do is to fix the player direction so say I'm in my first map and I keep walking towards the right and I go to the next map and I'm facing towards the right which is correct and expected but if I go towards the left and go back to the next map I'm facing towards the right when I spawn and we can easily fix this by also specifying the player direction within our global script so let's go back to our global script and I already have a variable called player facing direction and since my game is my game is a platformer game with only two directions I only care about the X direction and it would be either one to represent right and negative one to represent left and if you were to do a top-down game you would have four directions to worry about if you had portals on the top and bottom of the screen so we have this variable set here and then within our portal script let's create another script variable X for int for our player direction and I'm going to just set it to one and then within the function we will call global dot player facing direction and we'll set it to a player direction let me just make sure that's the right variable name yep and now within our nodes within the scenes so in the portal scene we can set what player direction should be when they go to the next map if I'm coming from the right to the left I should be facing the left so this should be negative one and in the root it should be one so let's try this out make sure I got that right okay cool and I think I forgot to tell you we also have to set the position or the direction of the player within the player script in it's ready function I have a function called set blend position and I pass in the X direction to correctly set the animation of facing to the left or the right that's pretty much it thank you for watching
Info
Channel: Arkeve
Views: 10,351
Rating: undefined out of 5
Keywords:
Id: tp1OiUEq3Pk
Channel Id: undefined
Length: 13min 57sec (837 seconds)
Published: Thu Jul 02 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.