 If you already played with RPG Maker, you probably know what a switch is. So on RPG Maker we can attribute some behaviors to tiles on the map and when the player interacts with this specific tile it can trigger a switch which will be able to switch the object state or another object state and this is the interesting part. So for instance the player can pull a lever or push a button and open a door for instance and this is what inspired the switch to the recipe from the platform essentials cookbook and this is what we are going to talk about in this video. So as you can guess we can find a switch recipe or the switch pattern on many platformer games both the classic ones and modern ones especially the ones that focus on adventure as we can use them to open doors to extend bridges to even change the state of a character. So for instance on RPG, I mentioned RPG Maker because it's very clear this is what inspired me to to create this pattern but what triggered me to actually implement it was Ori and the Blind Forest. So when we are on the Mount Huru stage where we have to pull a lever to make a platformer start moving so Ori can hop on top of it and cross a path across a pit. So this is exactly what the switch pattern does it can it can change an object state based on players interaction with another object. Besides the switch pattern is very simple and easy to implement the approach that I use to implement it relies a bit on duck typing and this can be a drawback for some people. So what we are going to do in this pattern is to create a method that all objects that this switch will interact with will have to have in their interface in order to this switch to call this method on this object to switch its state and we can use duck typing to check that so we can use like does this node has this method if it does we are going to execute this on this node and for some people this can be this can be like a smell a cold smell and I don't think that it is I really like duck typing is one of my favorite techniques but for some people this doesn't work nicely especially because if you change the interface of the target object you have to change the the method on the switch as well but let's see how this goes. So let's get started here I have the switch level which is a level that I created to experiment a bit with the switch recipe and in this level we have the match pig which will be our switch and it will switch the state of this path follow platform so currently the platform is up but I wanted to switch between these two states so move up or down up in this case and down and basically what we are going to do is to make this pig here the the switch for this behavior and for that we are going to import the switch recipe from the from the platform essential assets so let's go there import platform essentials assets I will ignore the asset route and I will change the install folder to this recipes folder right here I don't know why but good old likes to select a lot of garbage here so I will uncheck all of these things that I don't want I just want the switch right here and I don't I definitely don't want this this folder and I also don't want the the project okay so I was going to install the switch pattern in this project install okay installation successful and I think that I had to I have to fix some dependencies but must be something very easy to fix yep so close and now we already have our switch so let's see what the switch recipe does I will open its script as you can see it acts asks for an object a target object path it has a target object variable so even if we don't have the path for the object while we are designing stuff we can inject the target object in room time this is very cool right and it also asks for a method in this case a switch method on its ready callback it tries to get the object we can even try something like get node or no here to ensure that this this path actually exists or returns a valid node if it does it will basically inject the node into this target object otherwise it won't do anything but uh this is the the important part right here we have the to go method which accepts an object so if you want to pass another object other than the target object you can do that as well but by default it will go to the target object and it will check if this object has the target method the target switch method which you can call it whatever you like but this is the ducking part type the duck duck typing part so we are checking if this object can walk if this object can walk we are going to call quacking on it the quack method on it so if this object can switch its state we are going to call the switch method on it well essentially this is what the the switch recipe does i will going to attach i'm going to instantiate it right here on the match pick and note that if we go back to the switch it is an interactive error to this so we have access to all the that the switch the interactive area does as well especially to its signals so if we open the switch scene here you can see that the interactive method is um it's connected to the to go method of the switch so once we interact with it it will call this to go method changing effectively changing the object state so this is this is kind of like one pack one pattern retroactively interacting with the the children the children recipe so the the switch is an interactive object an interactive method an interactive recipe an interactive area actually that once interacted it will call the to go method on itself so let's create this area i think that i will use a collision shape collision shape right here this will be a rectangle shape and it will take all of the space right here maybe even right here something like that maybe right here i'll change this to make sure that we understand that this is an interactive area maybe green i think that i'll go with green basically i would set this target object path to be the path to the right here which is this platform right i already set up this method so i have a lift variable that basically controls the state of the platform so is the the platform lift it is not currently so it will to go this state basically setting it to the inverse of itself so the opposite of what it is currently so if it is true it will become false if it is false it will become true and i think that wait a second just to make sure that about something we said yeah it started lift lift so i think that this will go to the bottom here and it will start with true so if it is lift it will play the move down animation otherwise it will play the move up animation and these are the two states the two states indeed the switch will to go this platform the same thing happens with this match pig here it has a match on variable that control its own state states but i don't want to create two switches so i will use this switch interactive method with this pig switch method and all that it does is that on this pig right here we have an animation tree that once this match on property is true it will play some animations you are going to see that and since the switch is also an interactive error 2d i'm going to use the interaction available and the interaction unavailable methods as well to give a visual cue to the player that they can interact with something once they enter in this area right here and for that i'm going to use this exclamation balloon so if we play this animation i don't know why people from goodwill took out the the property that allowed us to just make the animation animated sprite play itself now we have to use it together with an animation player which is very inconvenient it's not convenient at all but anyway once the player is inside this area it will play this animation telling the player that there's something that they can do right there but once the players leave this area it will play this out animation so let's connect these signals interaction available to this animation player play method and the in animation and once then once it emits the interaction unavailable signal we are going to the animation player and play the out animation and well with that i think that we have everything set to play the scene so i'm going to get close yeah we can interact with it uh it will light up the the match telling the platform to calm down we can hop into the platform and interact with it again because remember the switch will too go between two stages it will not do anything else so everything else here is basically just animations so the player will have to interact with it again to to to go the state back to what it was before we switch it on so i'll interact with it again and it will leave the platform and we can keep on doing that because this is very cool and well we can do whatever you want with the switches as long as the object has two states you can actually use this in very uh in many creative ways so for instance maybe you have a boss that demands that there are like three of five torches light up and you can change the state of this torches to light them up and once there is there are five torches the the boss will wake up or maybe you can create some something like a button for or a lever for to open a door uh the switch is also perfect for that so this is how we can use the switch recipe and remember the switch recipe is one of the 12 main recipes from the platform essentials cookbook in which i will highly suggest you to grab a copy by using the link in the description and in the pinned comment to make it even easier for you also we are actually on the third extra recipe so extra recipes are recipes that are sponsored by customers so when i was making the crown funding campaign for this book uh people could pay $30 or more and request a recipe all all for them i created the wall jumping character to the recipe extra recipe which guys i'm very proud of that is one of the best implementations of wall jumping that i saw in the whole good old community the second recipe is the input remat button in which it will help you create key biting key biting menus so your players can rearrange the the settings of the controllers this is a fundamental recipe to having your platforming game and i'm currently working on the infinite moving background recipe that was requested by tom and in which we have like parallax backgrounds that repeat infinitely and that can even move independent from the player so you can create like moving clouds or water or something like this but uh that's it go there get a copy of the platform extensions cookbook and i'll see you in the next one keep developing and until next time see you there