Game Manager - Controlling the flow of your game [Unity Tutorial]

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
a game manager is a way to keep track of the state and flow of your game uh as you can see here i've got a little game set up we've got our players here and the enemies on the right there with the fire on their head and i can press this button and my team will attack the other team but as you can see i can just keep pressing it and overwhelm the enemy and there's not really much challenge to it so i would like to be able to firstly i would like to show the player a select color screen so that they can choose the color of their unit and then i would like to only let the player attack when it's their turn and then have the enemy attack when it's their turn and then ultimately either change to a victory state or a lose state so let's get started let's create a new object and let's call it game manager and then a new script by the same name [Music] and as you can see it's such a common practice that unity detects the word game manager and if they say it they'll change the little icon to a cog there put that on your game manager object and open it up let's start by making a static instance of this game manager so we can easily grab it from any where in our game [Music] just set instance equals this i'm not going to go too much into the detail of singletons or static instances but for now you just need to know that it just allows us to grab it from anywhere so the whole point of the game manager is to manage the state of your game so that's what we're going to do first create an enum and call it gamestate and the first state that we want is the color select screen isn't it so this state will be called select color and then let's say it's the player turn and then the enemy turn and then after the player turn enemy turn player turn enemy turn we eventually will want either a victory screen or a lose screen now in our game manager let's create a variable of type game state let's call it state and now we need a way to actually change the state of our game let's make a method for that public void update game states and this method will take in a type game state and we'll just call that the new state in here we will set our state equals to the new state and then we might want to run unique logic depending on the state so let's create a little switch statement here which takes in the new state and if you press alt enter you can just generate the switch levels and it will generate all of those for you and now in here we'll be setting the state and then we'll come down here and then if we need to run specific logic for each of these states we could simply add a function here for example handle select color and then that will be run when the state is changed to select color another thing we need in here is to actually notify anybody who cares or any script that cares that we have in fact changed the state and this is the new state so we need an event for that so let's make a public static event of type action and it will take in our new state and we'll call this on game states changed [Music] all right so now that we've got that event let's actually trigger it after this so we'll do that and we'll send in our new stage sorry this is actually warning me it's basically just saying if i trigger this and nobody has subscribed to it i'm going to throw an error at you a null error so you can protect against that by just saying has anybody subscribed if so invoke this function just like that so now that we've got that when the game starts up [Music] let's actually call our first state change let's say update game state and we'll send in our first state select color okay so easy as that now this is not actually doing anything right yet because we haven't actually attached any uh state-specific logic to it uh so what we need to do first is show uh the player the select color screen so this event down here let's let's hook into that event so over in my menu manager i've got a reference to that color select panel which i showed you at the start of the video so what we need to do is subscribe to this game manager on state changed event so in our menu manager in our awake function let's subscribe to it game manager on state changed plus equals which is how we subscribe to events in c sharp and let's create a new method now it's good uh it's good practice to always unsubscribe from an event uh when you're finished with it to avoid memory leaks and so on so on our on destroy event when this class gets destroyed we will instead of subscribing to it we will unsubscribe from it just like that so now when game manager calls this calls this on state changed we're going to run this function let's change that to state and basically all we need to do here is if it's if the state is select color we will show our panel otherwise it's always going to be hidden so an easy way to do that is just color select panel set active and if state equals select color then we're going to show it otherwise hide it okay so now when we start our game up it shows our color select screen which doesn't actually do anything at the moment so let's do something about it on our color select we can just move my face over here so you can see you'll see on these on the button events i have hooked into the unit manager select color function so blue or red okay so over on our unit manager i've got this function called select color it takes in the color and basically it will just say to all of our player units here's a red material here's a blue material pretty simple um you can just ignore that though now once the u once the player has actually selected this color we're done with the select color state so now we can say game manager instance update game state and what was our next state it was the player turn so let's do that let's send in player turn so now it's going to come down here again we're going to set the new state it's going to come to player turn we might need to handle some player turn logic here so let's create a new function just like that and then it's going to perform that logic and then come down here again and trigger the state changed event again so what do we need to do here well it would be nice to only allow the player to actually press this button when it's on his turn uh we don't want him to press it when the enemy's turn is playing that would be unfair so in in my uh menu manager i've got a reference to that attack button as you can see there so in our menu manager uh we've actually already subscribed to this event so now we can say we need to actually change this attack button to an actual button so let's do that button attack button and we'll remove that reference and now we only want to allow the users to press the button when it's the player turn so we can do attack button interactable equals and we just say if the state is equal to player tone also when the player actually presses the attack button we want to then take it to the next state we don't want the player to be able to continuously press the button so as you can see here i've got this attack button pressed and on the attack button i've just got a button trigger and the menu manager attack pressed so when the when it's pressed i'm just i'm just telling the unit manager to tell all the players to attack and then i will say the game manager um instance update game state and now let's say it's the enemies turn in our game manager now that we go into the enemy turn let's create some logic for that handle enemy turn uh now in here let's actually make this an async function and we'll just add a little bit of uh artificial wait time just so that the player units have a time to attack this is uh not great design i'm just doing it for the sake of the tutorial and we'll just do a task delay let's wait for about actually let's wait for about two seconds now our enemies can attack so let's say unit manager instance attack and we'll say the enemy faction [Music] let's add another artificial wait time there to allow the enemy to attack and then here we should then find out if the all the enemies are dead or all the players are dead so a good way to do that would be a another state here and we'll just call it decide and we can add a new case [Music] handle decide now uh this is not the most performant way to do it but for the sake of the tutorial basically what i'm going to do is i'm just going to grab all the units find objects of type units i'll say if units any uh unit of faction basically just looking for are there any units of enemy and if not if no enemy units will say update game state and we'll say win victory because there's no more enemies else if now this is a very sloppy way of doing it but just just for the sake of the tutorial uh if there are no players left then we'll say well you lost otherwise let's update the game state back to the player turn let's also add an artificial weight here just so that we can see the decide or else it's just going to flash by our eyes way too fast all right so let's see what we have made a red team attack enemy turn over to the side back to player 10 oh look at that damage oh it's not looking good not looking good 1v3 at least i took one of them out and i lose so there you go i showed you how to create a game manager a fundamental of game management i showed you how to run specific logic for each state and trigger an event which other parts of your game logic can hook into and run their own logic a game manager is perfect for keeping all of your game flow all in one place keeps it neat organized and easy to debug so if you learn something give it a thumbs up subscribe and i'll see you next time you
Info
Channel: Tarodev
Views: 15,763
Rating: undefined out of 5
Keywords: Tarodev, gamemanager, game manager, unity, unity3d, tutorial, game state, gamestate, playerturn, game flow, turn based, game development, unity tutorial, unity3d tutorial, how to make a game, unity game manager, game design
Id: 4I0vonyqMi8
Channel Id: undefined
Length: 13min 38sec (818 seconds)
Published: Sat Feb 20 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.