 If for the exploration something important in your game, maybe I'm making an adventure game, you'll definitely want to use this pattern that I will show in this video. The Portal 2D is one of the main recipes from the Platformer Syncios Cookbook, which is a book that presents 12 design patterns that help you build platformer games with Godot Engine. The Portal 2D pattern, or recipe as I like to call it, allows players character to teleport between discrete world locations. So when I say discrete world locations, I want to communicate this in your position to continuous world locations. So what I mean by that, I think that you will understand better with two examples. Open world games such as Skyrim have continuous world locations, so you transition between one location and another and you don't even realize that you are entering in your locations. You don't even notice that you are entering in a new location until the title of the location, the level, the map appears on your screen. On the other hand, take Cuphead for instance. On Cuphead you start and finish a level, you have to select a level, you enter the level, you have a very clear definition on where the level starts, where the level finishes and when you finish the level you go back to the level selection interface or menu and you have to select another level that has no explicit interconnection with the previous level. So this is what I call a discrete world location, where there is no clear transition between one location and another, there is no clear interconnection between them. In that sense the Portrait2D fits the role of being this interconnection between locations. So it's ideal to use it when you want to have a layer of depth to a 3D levels. For instance you can use it to create doors to NPCs houses and when the players interact with this door, which is a Portrait2D, they'll be teleported to the NPC house and have dialogues with the NPCs or find out a mystery thing or an item there. You can also use it to create cave entrances and when the players interact with these entrances which will be a Portrait2D, they'll be teleported to the cave which can be a dungeon. You can also add Portrait2Ds to the beginning and end of a level or a location and allow players to move between locations smoothly, using basically just a fade in fade out transition. There is a saying from Scott Rogers, the game designer that wrote level up, that he says that walking is not a mechanic. So if not an interesting will happen between moving from one location to another, why not just teleport a player there already? This is a useful thing that you can do with Portrait2D. You can basically add a fade in fade out transition, implying that something happened between moving to let's say from the forest level to the city level, but it's nothing interesting. So players won't waste their time just walking around to the city. So you can delegate this to the players imagination, you can allow them to imagine how will be the journey that move from moving from the forest level to the city level, but you can focus your resources, especially the artist's time, on what actually matters to your game. Notice that using the Portrait2D may break the game flow if the players were expecting something more smooth or to actually walk through these locations. So if players are expecting something like an open world game and you use the Portrait2D to teleport them from one location to another, this might be a bad usage of the Portrait2D, so keep that in mind. Well, let's say let's go to the tutorial part, which is what you will actually come to, right? So what I will do here is to explain the code and then I will show how to use the Portrait2D, right? Here I have the Portrait2D class, which as you can see is an interactive error 2D. So it extends the interactive error 2D. If you don't know what an interactive error 2D is, at the end of the video you can watch the interactive error 2D video from this series, which is the previous one. But basically, an interactive error 2D is an error 2D, so it extends error 2D from Guru, that once players also have an interaction error 2D on them, which basically is an error 2D that is only a specific interaction layer, that once they overlap, players can press a button or maybe they can be passive that will warn that interaction error 2D overlaps with them. But usually we use an interaction button, an interaction action, and they will trigger an event. So in this case, in this specific case, we have the teleport in event and the teleport out event. When a player, when a level starts, the level asks the Portrait2D to teleport an object to, and the object will be teleported to the global position of this specific Portrait2D. So players enter a level, the level will ask the Portrait2D to teleport this player where the Portrait2D is. Teleporting out will make something different. It will ask the scene tree to load a new scene, but it will store some important information on a teleport data single tone, because we have to maintain some data between these transitions, namely the next portal name, the target portal name, and if the player can teleport back or not. So if the player can teleport back, they will teleport to the same portal that they come from. And then after that we change the scene to the next scene that we can pass through in this argument. And when the players overlap with this Portrait2D, the Portrait2D data will restore the current portal. It will restore this Portrait2D as the current portal. So let's go to the Portrait2D data, to the teleport data to see what else can we do with this. So it is a very, very simple class that mainly just keep information between these levels, because when we transition to another level, basically Godot will erase everything from this scene and load the next scene. So a lot of data will be lost in this process. But we can keep some data by storing it in a single tone or an outload, as we call it here, Godot, which if I'm not mistaken, there are different things. We can toggle an outload as a single tone or not. But let's move on with that. So the teleport datum node, which I just called it teleport, extends a node because it's very simple. And we have a teleport back flag that we can set to our files. We have a current portal, which is the portal that the player just interacted with previously. And we also have the target portal name, which is very important. Because when we are teleporting, let me open the actual portal class here. So this is a portal, an area, which currently it doesn't have a collision shape yet. But we can set the next scene here on the inspector in the target portal name, which will be the name of the portal in the next level that the player should appear on. Sounds complicated, right? Let's let's open the actual level so you can see how this all ends up. But before that, let's open the portal level script, which is something that if you want to use a portal, you want to use this logic on your top most note on your level scene. Because what it will do is that it will take the teleport data if if we can teleport back, and it will find the the portal in the that has the target portal name. So it will find a child in this target in this current level that has the target portal name. So remember to use always indexed values, not indexes, but unique IDs for these porters at least so you you don't mess with this logic. Okay. And after that, it asks the portal to teleport the player into itself. So it in the main portal here. And then when the players entered a new port, it will wait for the fading fade out to happen. And then it will teleport the players out to the next portal level. So let's open the portal level one. And here I have two two portals, which are the door start and the door go. And you can see that the door start has a target. Let me see next thing path, which will be the portal to the the portal level two, which is the next level. And the target portal name will be the door goal. So let's open this level two here, the player will be teleported to this scene or to this door right here to this one. And the door start will teleport the player to the door goal from the other level. So basically, this is a cycling level, right? I'm not going to save it because I don't want to lose the tooling of the canvas layer. But if we play this level, if I press F, which is my interaction action, I'll be teleported to the the goal, the door goal of the previous level. I can interact with this NPC. I used to be just a pixel without dreams and desires. But this was before. So this is also an interactive era to the go back to this video that I talked about. So you can see how we can create any pieces using interactive error to this. But if I press on this other on this other property, I'll be teleported to the goal, door goal of the previous level, right? So this is how the portrait to the works. So why does this recipe works? Why does the portrait to the recipe works? Well, from a design perspective, it saves a lot of time because, as I said, you won't have to draw, design and implement a whole part of your world that basically players will just walk through it. As I said, or as Scott Rogers said, walking is not a mechanic. If nothing is important will happen in this part of your game, you can basically just fading fade out and teleport the place between these locations and let the imagination feel the gap that they imagine how will be the journey between the forest and the city. But also from from another point of view, the portrait to the adds a multilayer rate level exploration because instead of just moving horizontally and vertically in the level, which is a lot by the way, you can also add a depth to the level because now players will be able to enter houses, enter buildings, enter dungeons, and you can also add like crossroads if players like interact with the portrait today, you can have a wood sign telling that if you moved in this direction, they will go to city A. And if they move in this direction, they will go to city B. If they move to this direction, they will go to forest part three, for instance. So the portrait today works in a very it basically enriches the world building of your game. But also from an engineering aspect, so from an engineering point of view, the portrait today works because we rely on the very same tree structure from Godot itself. So we use the fine notes to find where the player will appear in the next scene. We use the chain scene method to transition to load the next the next level. And also we can rely on the single tons of the outloads to keep and maintain the data from the previous level. So we can know where the player will be at the next level and where the player came from the previous level. You can find more about the portrait today and 11 other design patterns or recipes as I said in the platformer essentials cookbook. You can grab a copy of the cookbook at pigdev.each.io slash platformer dash essentials. I'll put it in the description to make it easy for you. So did you like this kind of content and video format? Leave a thumbs up so people know that this is worth watching. And also subscribe for more. If you have any questions regarding the platform essentials cookbook, the portrait to the implementation, maybe you have some doubts regarding the code, leave a comment below. I love when you guys do. But for now, that's it. Thank you so much for watching. Keep developing and we'll see you next time. See you there.