 This video was brought to you by my patrons. Thank you so much for your support Hello there Following the series where we are talking about object-oriented design and especially this series is about design patterns In this video, we will talk about the strategy pattern and this one is very very useful But for some reason I couldn't find it on that book called game programming design patterns I had to search on other books such as the dive into design patterns So I will put a link in the description for other books where you can find these design patterns there But for now, let's get started So a brief definition of the strategy pattern is that it is a behavioral design pattern that lets you define a family of algorithms put each of them into a separate class and make their objects Interchangeable and I'd like to highlight this word because it's the key point of the strategy pattern Make their objects interchangeable You should be able to pick a a member of this family of algorithms and use another member in their place And the algorithm should work just fine So they should be the same thing just with different strategies make sense, right? So here in Goro, I try to implement this strategy pattern But I don't know if this will run because the example that I try to make have some other dependencies But I will explain this to you and it will make sense after I explain, okay? Okay, so here I have a boss that has two strategies for taking its target It has a sort by the closer target and sort by the thread I will open the boss script here so we can understand what it does So it has a life It has a list of targets and it has a target sort This target sort will be one of these two strategies we have here The two are members of the same family, which is this target sort family And they are supposed to work on this targets array So here we have the attack when the boss attacks it will sort the targets Then it will pick the first element of the targets list and it will play the animation attack and apply the damage and other attack stuff so here in the sort target, we have The conditions that will basically change the strategy that the boss should take to pick a target So if the life is smaller than 20% of its life, it will go berserk Otherwise, it would just pick this sort by thread So after that it will go to the target sort which will be set here So we have one of these inside this Verbal inside the target sort variable and it will assign the actor So if I go here in the target sort it asks for a actor parameter and After that it will go to the targets and sort custom It will use one of these strategies to sort the targets array So if you go here in the help search help and search for sort custom The sort custom method is a method in the array class Which asks for an object and a string that is a function a method that is inside this object here So it sorts the array using a crystal method and this Function this method that is inside this object must return either true or false It must return a bullion So if we go here in the target sort by default it always return true So it will keep the order of the array, but if we go here in the closer For instance, we will go and calculate the distance From a to the actor which in the case of the boss will be the distance from one target to the boss and it calculates the distance of B to the boss and it returns if the Distance of a is greater than the distance of B So it will sort the array by the distance taking the one that is closer to the boss I didn't check for that, but this can be actually the further But let's presume that this one make any change. So we are trying to figure out who is the closest one Okay, so let's just pretend that this is correct And in the target sort thread it will go to the actor Which in this case will be the boss and calculate the thread of a given element So this thread can be calculated by a number of factors if we go here in the boss We have here the calculate thread and it can be like the damage of the attacker So if we have a player attacking the boss the boss will take in account the damage The player is dealing the role of the player So like if it is a healer it will have a higher thread it is a tanker It will be the highest thread if it is just a damage dealer It will have a lower thread if it is a rogue for instance It will be the lowest thread and it also calculates some distances to calculate the the thread But taking account a lot of other factors and it returns the thread So this is what this target sort thread does if you go back into boss If the life is smaller than 20% of the boss life It will go berserk and when the character goes berserk It will just pick the the first thing on its range and Attack it just like this it stopped making sense It stopped calculating stuff and just pick whatever is closer to it So it will modify the strength of the boss It will make it scream like go and rage and then it becomes Rather though this berserketing but here is the thing that is important It will change the strategy of the boss It will make the boss takes this target close strategy This strategy of picking the closest target So it will set whatever is in this target sort to actually be this Target sort by closer thing here, which is this algorithm We just saw that calculates the distance And you can see how powerful this strategy pattern is Because instead of having like a method for each of these strategies that this boss can have We can have a class and just inject this object into a verbal and use it as we want So we encapsulate these algorithms inside a class and note that we can just Interchange these objects because we have like a kind of interface here Which is this target sort, which is the parent of this target sort family of algorithms And we can just use this sort method the base method of this class and just change how it calculates everything and The behavior of the team the strategy that the boss for instance will use will change So we can keep expanding the strategies that we want a given boss to have just by creating new Strategies using a base class just by using a same family of algorithms. So let's say I will go back to boss here Let's say we want the boss to at some point Calculate the the targets by the speed of the target So let's say it's a boss that will chase a target and it will chase the slowest targets So I can just go here in the scene new inherited scene and I will go here to target sort I will go here and extend the script Let me just change this name to target sort speed And I will extend the script. So let me just save this target sorts Speed and extend this script. Okay, and it will use this sort method a and b and it will return rather the a Dot speed is greater than b dot speed and There we have it. We already have another strategy. And this is not just inside the boss we can use the same strategy on players or Artificial intelligence we can pick this and use on let's say helper or a minion that the player can summon in the In the game and this minion will take the the highest speed target Or the the slowest target and it will share the same strategy with the boss So we can expand the strategies that we use in our game. I can't describe how powerful this can be So to sum up we use the power of polymorphism So we have a parent class and we have a family of Algorithms that share the same method, but they all behave in different ways So they have different strategies to to work on so we have this parent class Which basically just return the same array and then we calculate the rate by the closest target Then by the speed of the target then by the thread of the target and all of these can be shared And they are interchangeable because we are using the power of polymorphism and to me this is one of the most blowmined Design patterns because it can make us think about a very reusable code Like we can have a library of different strategies of different families of algorithms So let's say we can create a family of algorithms for steering So we can have a steering behavior family of algorithms and we can change this steering behaviors just by changing What is inside a given variable? So this is it if you have some doubts or if you think that I've made something wrong here Or if you don't quite get how this strategy pattern applied to this example Please leave a comment below I will be more than happy to help you and to also get some feedback Don't forget to subscribe and turn on the notifications so you get notified when I release new videos We still have a lot of things to see in this design pattern series Also, if you like my work if you like what I'm doing here You can support my work by becoming one of my patrons and this is it Thank you so much for watching keep developing and until the next time