Develop Trading Card Game Battle System With Unity 3D: Part XV (New Dragging Scripts and Game Logic) | Vladimir Limarchenko | Skillshare

Develop Trading Card Game Battle System With Unity 3D: Part XV (New Dragging Scripts and Game Logic)

Vladimir Limarchenko, Game Developer, Unity3d and C#

Play Speed
  • 0.5x
  • 1x (Normal)
  • 1.25x
  • 1.5x
  • 2x
4 Lessons (59m)
    • 1. 502 – Adding All the Remaining Scripts

      13:58
    • 2. 503 – Assembling Game Logic and Deck Shuffling

      14:44
    • 3. 504 – Dragging Scripts and GlobalSettings

      12:30
    • 4. 505 – Turn Manager Script

      17:25

About This Class

In this class/series we set an ambitious goal of developing our own trading card game with Unity3d and C#, mostly focusing on card game battle mechanics.

Learn the essentials of setting up the logical part of your game:

  • Establish connections of players with their hands, decks and table areas
  • A quick overview of an extension class that will handle deck shuffling
  • Take a fist look at the Player.cs class that will handle all the stuff that our players can do in the game (playing cards, attacking with creatures, using hero powers, taking damage and so on)
  • Find out about the difference between our human players and AI-controlled players in code.
  • Bring in the TurnManager – the script that will manage the flow of the game and switch turns to the next player when the turn is over. TurnManager will also take major part in controlling the burning rope timer.

With this class you will gain access to a compete, fully functional Unity project that is an example of a card game battle. The game mechanics are somewhat similar to Hearthstone: custom spells, hero powers, creature effects and so on.

Additional information:

  • The files showed in the course are downloadable from the link that you can find in the "Class Project".
  • The next class of this series will be published on 2017.01.07.

Transcripts

1. 502 – Adding All the Remaining Scripts: Hello, everyone, and thank you very much for watching the scores about making your own trading card game. In this lecture, we have finally come to a point when it's time to talk about our game logic, and we want to import all those game logic scripts into our project. Once we import them, we'll most likely have a lot off errors because these scripts, they're all interconnected and they reference some other scripts. So in this lecture, we want to accomplish this. We want to bring all the scripts that we need to have in our projects, and we want to make our project run again, and we don't want to get any errors. And I have already added in you game object to our hair key that is called logic. So far, logic and all the child game objects off our logic game object. They're all empty, but we already have the layout that we have to follow in our next lectures. So as a child off, our logic will have two players and they are actual players that will battle and that who hold all the information about our game and as a child off our player game objects. We have a hand ADAC and the tables for now, to make some tiding up in our scene. I've deleted all those creatures and all those cards that we had there and this game object that is called damage fact tests. I think that it should be a child off our visual, so damage effects their visual by nature and making them a child off our visual game object , I think, will work for us. So now our higher Q looks tidy and need, and it's time to import some new scripts into our project. So let's open our downloads Scripts folder and first, we'd like to import this folder with commands in our project. We have already talked about them just a little commands. They will be a connecting link between our logical part of the game and our visual part of the game, so we definitely should have them. So I'll drag this folder with commands and import them into our unity project. Then we should also have these scripts that are in the root off our Scripts folder. The most interesting out of them all is this strip that is called shuffling extensions. It actually doesn't contain my code. So this is just some code that I found on the Internet. But it will allow us to shuffle our decks, and it will just shuffle any generic list for you. So we're very interesting and useful script. Then we definitely should have the folder that is called Logic in our Project. So let's also drag that into our scripts folder, and we also should have oldest scripts that will be responsible for actual dragging off our game objects in the game. So far, we had this folder that is called Dragging tests that contained some scripts that were used to explain the concepts off, how dragging works and how we're going to handle it in our game And these scripts, they will be applied to our actual game objects to enable all those dragging, attacking and other behaviors in our game. So I'll go to our dragging folder in Unity. I'll jump back to this folder. I'll select all these scripts and I'll drag them into our project. And finally, let's return back to our scripts folder. We also had some scripts in our visual folder that we forgot about or we didn't talk about them yet, So now it's also time to import them into our project as well. I'll return to scripts, open our visual folder, returned back to this folder in the Explorer, and then I'll have to select all the scripts that we don't have yet. And they are creature attack visual. We don't have it yet. Then we don't have this script and turn button global settings. We've already talked about this script hand visual hero, power button, a de holder, clear area, clear deck, a visual rope visual and table visual and this last one that is called Where is the card or creature? So we also want to have all these scripts in our project and al just regular them and bring them into my visual folder in unity. And once we've done this, we can return to unity and all the errors. They'll go away so we will not have old errors in our consul. I can launch the game. Nothing will happen because we have not configured our game yet, but we also should not get any errors. So all the scripts have everything they need. We haven't left out any scripts in our downloads folder So now we have everything that we need to configure this project and eventually run our application. And another reason where we had to do this, why we had to import all those scripts into our project at once these projects off trading card games or actually any other games. They're mostly developed in this interational manner. We have already spoke about it in our previous sections. So first you set certain goal. For example, you want to make this rope, actually work and countdown time until the end of your turn. Then you write all the cold that you need and you work in this task and then you evaluate whether or not you have completed this. And if you've succeeded with this task, you can move on to something else. And this interational approach is actually the way to go. But it gets more and more complex if you start bringing these small changes into multiple scripts and everything is interconnected. So I wish that we could afford to use the same it creative approach in this course, and I could show you all the steps off actually developing this application. But this will probably take us forever and with all these explanations, it will just take too long. So it was a conscious decision to bring all these scripts into our project at once, and then we'll just look at them. Experiment with them, will discuss how they work exactly in our project. And we'll also build our game from these scripts in the hierarchy and will attach them to our actual game objects. And eventually we will get our game to work 100%. And I wanted to show you just one more thing. In this lecture, we can go to our Project tab and find one off our logic scripts. For example, let's open our folder with logic and let's open this script that is called Player. This player class is very important in our game because it will handle all the behavior off our single player, like drawing cards and making turns and everything else So I'll zoom in on the code just a little. And the thing that I wanted to show you in this lecture was I've actually enabled this feature off folding or unfolding our code in mono develop, and I think that it might help you view these glasses when you don't know fully how this glassworks, it might be much simpler to view it this way when it's folded or when the methods are folded. So too turn on this folding in mono develop. I had to go to tools than I have to select options. And here I have to select this stab that is called Editor General. And in general, I had to place this check mark that is called enable code folding. And actually don't like this feature off folding comments by default. So I'll uncheck that and I'll hit okay, And then this feature off folding code actually allows us to view this class without viewing the implementation off all our methods so we can just focus on the methods that will have in this class or maybe the properties. But we don't have to see how they actually function. So for all the methods that are present in this class, I'll just fold them right quick, and then we'll take our look at, um so here is the list off all the methods that will have in our player class. The list is not that long, but we've gotten rid off all the implementation. So now we get to see just the methods and, for example, for our players who have awake awake is usually used to establish some connections and get references to the fields that are private. Then we have these methods that are called unturned start and intern, and so they symbolize what should happen to our player when the turn starts or when the turn ends. Then we have this method that is called get bonus manner, and it is used practically nowhere except for one situation when we get to play our coin spell. So in harsh Stone, if you've played harsh stone, there is this special spell. So the player who makes a second turn, he doesn't have an unfair disadvantage. He gets this coin spell that will give him one point off man adjust for one turn. So to implement this in our game as well, I have this method get bonus manner that will give my player a certain amount of mana just for one turn. Then we have these methods that are called Draw a card. The second method is called Get a card, not from deck, and it's very similar to draw a card, except it will be used to draw some token cards like, for example, our Korean card. Our Korean card doesn't come from the deck, but our player has to get it somehow. And there might be some other situations. When you get so called token cards, for example, you play card, and it gives you this effect that you get some other cards, not from your deck. Then we'll have to methods for bling spells, and they will be both gold play a spell from hand. This is actually called overloading when you have two methods, their cold exactly the same. But they take in different arguments. The first method takes in integer ID's and we'll talk About I DS in a couple of lectures, and the second method takes in the parameters that are present in our logic. So it's first parameter is our card logic and I character, which is an interface that will have for our characters. They might be creatures or players then, just like with our spells, will have two methods to play creatures, and they're also overloaded. So the 1st 1 will take integers as parameters, and the 2nd 1 will take an integer and a reference to our card logic. Then we'll have a method that is called Die. So when our player dies, our game is over and we have to make something else. Maybe starting you game, maybe show some messages that you want or you lost, so it's very important method. Then we'll have a method that is somehow connected to our visual part. It is cold highlight playable cards, and it will both send the information about which guards can. We play right now to our visual part, and it will also enable all those guard glows and creature glows. If we can attack with our creatures or if we can play our cards this method, it will send this information from our game logic to the visual part off our game. Of course, there's a method to use hero power, and I actually think that it is more suitable to have it here on top of far method that shows highlights and I can just fold it. And then we have these two methods that are cold when in you game starts, one of these methods sends info about our character to the visual part, so that this info could be loaded from our character asset and the correct pore trait and hero power will be shown in our battle scene, and the second method is here to figure out who's going to control this player. So in our game that will be just to situations. The players, they can be controlled by us or they can be controlled by our computers. Ai if the player should be controlled by our computers. A. I think we should not be allowed to drag his guards or Blais his spells or do anything like that. So this method is here to ensure that we cannot drag any cards off our opponents if he's controlled by computers. Ai anything that that's it for this lecture. So we have imported all the scripts that we needed into our project, and we get no Evers in our next lectures who configure everything in unity editors so that we get our game or some of the features off our game to function. I would like to thank you very much for watching the scores and I will see you in our next lecture 2. 503 – Assembling Game Logic and Deck Shuffling: Hello, everyone, and welcome back to the scores. In our previous lecture, we have imported literally all the scripts that will have in this project. And now it is time to start building the logical part off our game, adding the scripts to our game objects, establishing all the connections in the Inspector. So that's what we're going to do in a couple of our next lectures. I already have this game object that is called logic in my hierarchy and this layout for our players scripts. So for this game, object player one low. I'd like to attach the script that is called Player to this game Object, and we'll get to instantly see all off our player scripts public fields, for example. We have this player I D. That is Justin into Chur, and we'll talk about ideas a bit later. And we have our character acid that will determine which class does this player have. So it might be an elf, a monk. You might introduce some other classes into your game. Then we have this script that is called Player area and player Area is a visual script, and it's a means off connecting our player in the logic with all the visual stuff that he owns, so we'll have it later in our game as well. Used hero power this turn. I think that I should actually make this one of property so we don't need to operate it in our inspector ever. That's why a property will be more useful solution for that. And then we have references to these other scripts deck, hand and table. And not by coincidence, we have these game objects hand deck and table as a child off our player game objects in the hierarchies. So let's grab our hand script. Or maybe let's select hand game object and attach our hand script to our hand game object and let's do the same thing with our deck. So here, let's find the deck and the table, and you don't have to be overwhelmed or scared by the number off scripts that we have in this project. Because our deck is a very simple script. For example, hand is even simpler, so we can actually view this script here in the Inspector. It has just one public field that is the least off type card logic that is gold cards in hand, so it's not difficult at all. And our deck. It also has the least this time. It is of type card acid that is gold cards. And the only difference from our hand class is that in awake, all the cards are shuffled and we are able to shuffle these cards. So it seems like the method shuffle belongs to our list off guard asset. And this is not true by default. This class that is called least doesn't have this method shuffle, but we have added another script to our project. It is in the root off our Scripts folder that is gold shuffling extension. It is not my code have just found it on the Internet, and I've provided a link in the comment in this file. And this class, that is, gold shuffling extension adds in you extension method to our lists that will enable this shuffling behavior another thing. Let's now select this deck. It has least of guards, and we can actually populate this list of cards. So let's make the size of this cards array or guards list to be 10 elements right now and actually, because in this course we're just making this battle scene. The only way for us to tell our players which guards do they have in their decks is populating these erase manually. So let's try that. I'll look the inspector and I'll go to my Essence folder. I should go to S O assets and open any off our folders. For example, let's have a couple of creatures. I'll select beasts. Necromancer source. Sir. Another card. Let's have a couple of thes and then let's add a couple of spells as well. And if you don't like feeling in these rays manually, there's another interesting solution. For example, our array now has the length off 10 and we have only field seven elements. So I can first make the size of this re seven and then I can change it back to 10 which just Gloans, My last element. And it was a card that he's cold swamp lor, and it makes old and you elements in this ray have the same reference to our swamp lurk art , and we can throw in a couple of spells so I can open my folder with spells. We have our refire bulls or we have this spell that is called Eclipse, so I can just add a couple of fireballs into our deck. And now, because of this functionality off our deck class, if we enter play mode and let's try and do that. So if I hit play, then the contents off our deck, they will be shuffled. So all these cards, they now appear in a different order. And if we try that again, so each time will get a different order off these cards. And in the game when we'll draw cards from this deck will always get this zeroth element off our deck. So, for example, if we had to draw our cards right now, then we'll get this Swamp lor card first. The second card that we drove would be this necromancer and so on and so forth. For now, I'll exit, play mood and for our lower player, I'll unlock the inspector. We have to establish all these connections with his deck, so I'll drag this deck onto his deck reference with his hand as well, and with his stable, will have to select some character asset for this character. We have only two of them, so I can navigate to assets and select Monk for our character and our opponent then should probably be off other class and will not touch all the other public fields in our players script for announced of this player. I d will discuss what ideas do in our app later, this player area will be apart off our visual set up and we don't have it yet. And this use here a power this turn, it actually can be made into property. And I think I can make the same thing to this player I d. S. So I might actually introduced some changes into these scripts even while we work in this course. And if you want to have the fullest and the latest version, you should check out here Downloads saved projects folder. And there you will find the version of this application that will be saved in the very end off this course. And it should be the best and the most optimized version off them all. Now I'll do the same thing for our Blair, too. This game object should also have a player script attached, dude. And he should also have all these connections established. And then I will get back to you. So I'm back, and I have done all the same actions for our second player, that is, this one on top and then to continue setting up are a logical part. Let's also check out this folder inside our scripts Logic folder in our project that is called turns and a Guy. So I'll open that. And here we have several scripts. The script that is Cold Turn Maker is a based class for these two other scripts. So the idea behind the script is we want to distinguish between our player so us, the player that will actually operate the mouse, drag all the cards and make decisions in the game from another type of player that will be controlled by the computer. So the computer, instead of dragging his cards with his mouth, he could make decisions in a different way because he already knows what's happening in the logic off our game. He can call the methods directly in the logic off our game, and this way he can influence what happens in the game. I'll open our turn maker and we'll take a quick look at this glass so this glass is public and abstract, so we cannot create an instance off our turn maker, and it inherits from Mona behavior. Another interesting thing that I forgot about is that all these classes the player glass and the hand, the deck and the table there all morning behaviors, and it should not necessarily be like this. But I've made them this way so that we can edit them in the Inspector. We can see all these values if we need. We can right quick and debug them in the Inspector, and we'll get to see more like man Left health and all these other values for our players and for our decks. We get to see the contents off our decks. So I've decided to make them wanna behavior so that it's easier to explain maybe debug or show you something that happens behind the scenes inside of these scripts. So if we returned back to our turn maker class, it has a reference to our player, which is protected and protected means that it can be accessed from this glass and from any class that derives from our turn maker class. So if you make another glass that inherits from turn maker it will have access to this field. Blair be in awake, refined our player So this glass off type turn maker which to be attached to the same game object that our player is attached to and the only method in this class is called on turn start. So the only thing that this glass cares about is what happens when we start the turn. And for our human players that who control the game by dragging cards with their mouths, not so much should happen later. These players, they determined themselves what should happen. So if I open our other class that is called Blair turn maker, you can see that there is not so much code in this class and unturned start. We're calling the base on turn start, which in turn calls our method player on turn start. I think that it's a bad thing that all these methods are called the same. But let's just roll with it. I think that it will be all right. So inside of this player turned maker, we call the base on turn start. Then we display a message that it's our players turn and here is the first time when we encounter this gold for working with our command system. So inside of our project, we have a separate class that is a command and it is called show message command. And here we're seeing you. So we are creating a new instance of this class and its constructor. So the constructor off our class show message command, it takes into arguments. The first argument is the string for our message. And the second argument is the duration that we want to show our message. And then this bid is very important because I constantly forget about this instead of just creating in you command and putting a semicolon. In the end, we should also put a dot here and call its method at two Q. So when we create new commands, we instantly want to add them to our command que so that our system knows that this command is standing in line and it's waiting to be executed. And then we call our players method that is called draw A card. So when the turn starts, we just have to draw a card and that's it. And for our ai turn maker, this glass looks a lot different. So it also has this method. On turn start, we show another message that says Enemy Stern. This time our player that is our enemy probably draws a card, and after all this is done, we start in you courting that is called Make A I turn. So this class that is called a I turn maker for our human players it doesn't do much. But for our AI, which sense for artificial intelligence, it also takes care off all the strategy and, oh, the decision making in the middle off our game. So the score 18 actually insurers for that. So it's elects a random strategy, and then it starts calling all the methods that will make moves for our AI services. A pretty lengthy script. And I think that who take a look at how the script works exactly in the end. For, of course, when we actually get to play with our AI. For now, I'd like to close Monta develop, and I think that once we just start playing this game and maybe we'll get some errors. It is better decision to make both off our players controlled by ourselves, and we can do that by selecting our lower player and attaching the script that is cold. Blair turn maker to our player. So this way we make this player controlled by ourselves and the same should be true for our top player. So if we attach this player turn maker to both off our players, it means that will control both of them. I think that we have already done enough in this lecture. So we have started working on our game logic. We have attached some of the scripts to the game objects that we represent logic in our game and in our next lecture will continue bringing in more scripts from our assets folder into our project and establishing all these connections until our game actually starts showing some signs oflife. And we actually get to test some interesting features. I would like to thank you for watching this course, and I will see you in the next video 3. 504 – Dragging Scripts and GlobalSettings: Hello, everyone. And thanks for watching this course. In our previous lecture, we worked on this little set up that is called logic, and we have attached some of the scripts to our players to their hands, decks and tables, and also have discussed this concept off having a script that it's cold turn maker that will be different for our human players and for our AI players. And in this lecture, I wanted to return and discuss some of the new scripts that are meant for the visual part off our game, for example, we have a whole new bunch off our dragging scripts that will be used in the game. So instead off having these scripts that we have right now, for example, on our targeted spell card, we have our drag on target tests and our script. Dragonball tests with actions. So in start off all these scripts, we would like to attach actual scripts that will be used in our game. So I'll remove these components from our targeted spell card, and I'll attach first our drag, a ble script and second script drag spell on target. This public property of type targets will be set in code, so we don't need to touch it in the Inspector. And even if we do, it will be reset for each individual cart. And after I have done this, I can just hit apply for our ordinary spell card that is not meant to be dragged onto any target. In particular, we did not have any dragging scripts at all, so I'll just minimize all these components and I'll attach Dragon Ble script straight to this card. So I'll grab distractible script and drag spell no target, and then I'll apply these changes to our prefab for our creature cards. We also have a special script that will control dragging behavior for these cards. So we should also have our Dragonball component and you have another script that is called drag creature on table. And all these scripts drag spell, no target drug spell on target. They're those scripts of type dragging actions that control whether or not we can drag this game object right now. What happens when we stop dragging this game object? Was this dragging action successful so they figure out all these things for us for each individual game object in our game and finally, for are creatures. We also have the targeting system here, and we want to have our new costume dragging scripts. So I will remove these old dragging components will have Dragonball here, and we'll have drag creature attack script off type dragging actions here. Now, once we have attached these scripts to a pre fabs. If we try to drag these game objects in test mode, something might not work because they actually rely on some other scripts they will want to figure out was our direct successful, and now we don't have all those logical elements, but they will all work in the final version off the game. So if you want to have fun and test your dragging features, then you should revert back to those previous settings. Or you can have another prefab with our dragging test strips and for creatures. We should also apply these changes to our prefab. I'll disable this pre fabs game object, and we had some new scripts in our visual folder as well, so let's have a quick run through all those new scripts. We already had this better card irritation script and this script that is called Creature Attack visual should be attached to our creatures, and it contains all the code that will be responsible for showing our creatures attack. So actually moving our creature towards the target and then moving in back to its original position. So let's enable our pre fabs back. Let's find our creature and let's attach this creature attack visual to our creature. There's another important script that is here in the bottom off our visual folder and its code. Where is the card or creature? And it's important because it will track current location and state off our cards and creatures in the game so this spiritually attached to each off our prefects, it has this interesting in, um, that is called visual states, and the options for this Inam our transition lower hand, top hand, lower table, top table or dragging So the script tracks. If our game object is currently in one of these states, and also for the cards in our hand, it is responsible for their sorting order. So when our cards are in the hand, they will actually overly and we have to figure out the correct sorting order for them. So I think it's safe to just select all of these game objects and drag our Where is the card or creature scripts onto all these game objects, All the cards and all our creatures. I'll hit. Apply then let's continue moving from the top in our visual folder. We have already talked about the script that is called Global Settings, and there were some references to the stripped in our previous scripts. So let's finally have it in our project. And I am not completely sure about the nature of the script. Global settings. As I already said, I used the strip just as a dump off different references that haven't made it to any off our other scripts. But we need to have them somewhere in our project, so I think I'll create a separate game object for our global settings. I'll go to create, create an empty game, object alcohol, this one Lobo settings, and I'll attach the script to our global settings game object. I think probably it's more visual in nature because one off its purposes is to establish the connection with our players. So it is something that should be on our device or on our game client, and it will let our device know that my owner is this lower player and the top player is the opposite player or something like that. So it has references to our players, so we can just establish them right away. I'll drag our lower player here and our top player here. Then we can not bother with Oh, these fields that are cold colors for now. Or we should have something green in this card glow caller. I think that we can figure this out later. And the Alfa you don't have to forget to bring the alphas all the way up. Otherwise your colors will be transparent. And you, my thing, that something else has happened. And while you were trying in figuring out this error in your code, the solution is much simpler. You just didn't said the collar correctly. So l said this glow color so that we don't make any mistakes. And these collars card body standard collar and card ribbons, standard collar. They were used to save the colors off our standard beige card. For example, I could have edited the collars on my spell card prefab, and then when it was instant, she ated no matter what colors I had there. The game will apply the standard color to that prefab. But in our game we've made a different decision. Will just make a convention that we are not editing the colors on our pre fabs and new colors will be applied on Lee. If this card belongs to a particular player, we have another header here that says numbers and values. And we have our card review tire, which is now one second card transition time. It is also one second card preview time, fast and car transition time fast. So let me explain a little about all these values. So when we take the card from our deck normally first the card will get previewed somewhere over here, and we'll have a special spot for our previews later so the card will be flipped over, previewed and then it will travel into your hand. So these times, for a preview and transition time, they're all set to one second, and these times that I called Previ time fast or transition time fast. They are used in the beginning off your game when players get their first guards not to make this process very long you want to speed this up a little. So the players, they will also drove the cards from their deck. But this whole process, it will happen much faster. So the preview time will be just 0.2 seconds and the transition time will be half a second . And then our global settings is also responsible for holding the references to all our pre fabs. So when will create in you card or a damage effect or something else who just access this glass that is called global settings and we'll ask for a prefab that we currently need. So I'll lock the inspector on our global settings. I'll go to assets pre fabs, and I will establish the connection with all these brief up. So no target spell card in our game is just called a spell card. Then we have our targeted spell card, our creature card, our creature and then we have this damage effect. We have actually changed some cold in our damage effects, but previously it was referring to this global settings glass to create damage effects as well, and we might want to change it later, So let's drag our damage effect prefab onto this field, and we also have our explosion. Prefab. We have not created explosion in our project. Yet explosion is used only once in our game bran. One of our players loses than this little animation for explosion gets shown on top off our player, and then a new game has started or something else happens. So it's just a visual effect very similar to our damage effect, for example, and we'll make it later. And here in these other references, there is a reference for our and turn button to change its color. So let's find that I'll go to visual our rope and enter and bottom game object, and I'll drag this button onto this intern, Bottenfield, then for our queen card. There is a reference to this coin card s it, but I think that I've changed. It's location, and it will later be located in another class room. Make a card asset for our coin card later. So now let's not bother with this reference. And then there is a reference to something that is called Game Over Canvas. I think that we haven't gone that far yet, so you establish a connection with it later. I think that on this point we should end this lecture in our next lecture will continue doing pretty much the same thing. So we'll go through all than use scripts that we have and will try to attach them to our game objects to make them functional in the game. And later we'll do some troubleshooting, some testing, and we'll actually discuss how old this system works. I would like to thank you very much for watching the scores and I will see you in our next lecture. 4. 505 – Turn Manager Script: hello and thank you for watching the scores in this lecture. I wanted to pay some attention to the script. That is probably the most important script in our game, sort of because it is responsible for tracking whose turn is right now. So which player gets to make his move? And another function off the script is to actually initiate our game and start the game. So this script is called turn manager, and I've already created another game object as a child off our logic Game object actually could have attached our turn manager straight to this logic game object. But I think that having separate game object that is called turn Manager is a batter solution in terms of managing our hierarchy. So let's grab our turn manager and attach it to this new game object. As I said, I wanted to change the place for the scoring card reference. So it is also present on our turn manager script and will probably delete it from our global setting script. So now let me open this stern manager in Mona Develop. Now, I am not sure that who discuss fully how the script works in this lecture But let's make an attempt and try to figure out at least some things about this script. So you have already seen this reference, which is public card asset going card. Then we have a public static, field off type turned manager that is called instance. So for our turn manager, we are also using this singleton pattern. There will be only one turn manager in our scene and all the other game objects taken reference this stern manager bay saying Turn manager dot instance and then call some methods or access some public fields and properties on our turn manager. There's another script that should be attached to this same game object that is off type rope timer, and it will control both the duration off our returns. And it will also be responsible for showing the burn rates off our rope. And actually, I don't like this concept of having this script take care off these two things at the same time. So there is a bit off so called coupling going on when the script is responsible for a couple of things and there is no clear division whether this script rope timer is a logical stripped or visuals creep Now, because we're running this game this way, and everything is in this one unity project, it's all right. But if you want to make an actual server and run logic and the visual part off your game separately, then it's probably not the best solution. For now, I think we can go back to unity and attach our script that is, cold rope timer that is also in our logic folder to this same game object and right away we can establish some connections for this rope timer. It asks us for this rope game object to be able to turn the rope on and off, so I'll expand our rope and an turn button game object for our rope game. Object will select this slider for our rope. Slider will select the slider as well. So this is a reference to the game object, and this is reference to our slider script. Time for one turn is a length off our single turn in seconds. For example, when I was debugging this application, I've set the time for one turn to be 30 seconds and for this rope burn time, so this value should be smaller than our time for one turn. And the meaning of this variable is for how many seconds during our turn, we are going to see our rope. I can said this value to 20 for example. So the turn starts. We have 30 seconds after 10 seconds have passed. Our rope is starting to burn and it will burn for a total off 20 seconds. And when the rope burns down completely, the turn is started for our next player. And we also have a reference to our timer text, which is called Just texts on this game. Object. So I'll drag it onto here and established this connection. And here we use the same unities built in events that are used, for example, with our bottoms so we can work with them in the same way. There is an event that is called time expired, and when the time is expired, we get to do something, and the beauty off this approach is that when this rope timer gets to zero, then you can call any method on any off your game objects. You can call several methods when the timer is expired, so it's very convenient to establish connection with your other game objects and achieve many things at once. This way, we're not using it to its fullest potential, but we want to make a least something happened when our timer is expired. So I'll hit in this little plus sign here, and we get to establish a connection with some for game objects. And I think that we can just try and drag this turn manager here so we'll tell the methods on one of the scripts that is attached to this game object. And here in this drop down menu, we get to select our turn manager script and among all the other methods, it has a method, very scold and turn. And by the way, through this menu, you can call not on Lee the functions off your turn manager. But you can, for example, access the standard methods off your game object, and you can set different game objects active or a negative, and later see how you can add these events to your scripts in cold. So I'll go to my turn manager, and I'll slag this method that is called an turn. Now, once we have this configured, I'd like to save my scene, not to lose anything. And finally, let's go back to our Monta develop and check out our turn manager script. One of the most important properties in this script is a property of type player that is called whose turn. So we're going to track which off our players is making his turn right now, and when the turn is ended, will set another value to this variable whose turn and when the turn is changed. This set, a portion of this property will execute, and here it will be older code that's responsible for changing our turn. Here we're getting a reference to our turn maker that is attached to each off our players than we're calling the method On turn start on our turn maker that for our player will lead to just drawing a card. And then we get to decide what to do and for our AI opponent. It will also start his turn actions so he will actually start, act and play his cards or play. He spells or attack us with his creatures, and I'll minimize this property for now than in awake. We're setting the instance to be equal to this, which is a part off our singleton pattern. Then we are getting a reference to our rope timer that will be called Timer in this script . And then here in start, who called this method on game start? And I wanted to warn you that if we'll launch our game right now, most likely something will not work because this own game start method has to be called when our scene is completely set up. So when all the scripts are already attached to our players and their hands and their decks and so on and so forth and this method void start, it will be called rights after our awake function is cold so automatically when we enter our scene, this method on game start will be called eventually, and it will cause a lot off errors in our game for now. So to prove you this point, I can just minimize Mona develop, and once we have this script in our game, I can launch the game and it will instantly sane all reference exception object reference not set to an instance of an object, and it's caused by our update Mana Crystals Command, which is also command. So we have not established all the references in our scene, and it is too early to start our game this way. So for now I'd like to exit blame owed. Go to our turn manager script, and I would like to comment out this line that says on game starts. So I'll add a common here and l type in to do here. So this method on game start is actually an entry point to the whole functionality off our game. If this method on game start is not cold, then when we enter our scene, absolutely nothing will happen. So if we establish all those connections, if we don't call on game start, the game will not actually be initialized and will not be able to play. So I can go to file for now and hit, save, minimize, mono develop. And now, if I launch the game will not get any errors. But also nothing will happen in the game, so I'll exit play mode and let's return to our turn manager script and just take a look at our own game start method and maybe some other methods that this street has. So when our game starts. We first clear the lists off cards and creatures that are created this game. So we want to ensure that these lists are empty and they don't contain any elements. And we haven't looked at our classes, card logic and creature logic. Yet then, for each player p in players, we have to make some actions for our players. When a new game starts, we said their values off man A to be equal to zero. Then we call those methods that are called in this start of the game, and we have talked about them when we were viewing our player class. So we load character info from Asset, and then we also make our game know about the nature off our player. So this method transmits the info about whether this player is a human player or it is an AI player. Then we passed something to our visual deck and we'll get there eventually. And then we also change the transform position off. Our player later will show this small animation off our players portrait appearing in the center off our screen, and then it travels towards the place where it will sit for the rest off our game. Then we have this complex syntax for showing this animation off our player Porter. It's moving, and it is done through our duty being engine. And when you want to make several things one after another or several things simultaneously , you can create a so called do between sequence and we'll discuss between sequences later and sequences or Tweens. They also have this useful method that is gold on complete, so we can specify what happens when the execution off the sequence is complete. So when we have completed moving our player portrait's to their spots, then we generate a random number, and this way we determine which off our players will take the first turn. Then we just temporarily store our first and second player in these variables who goes first and who goes second because setting these values instantly to our property will initiate a turn for one of our players, and we don't want to do that yet. We want to hold back just a little, and then we have this loop and old thes statements, and the mission of these statements is to give our players their initial hands. So inside of this four loop, we give both off our players four cards. Then we give the player goes second, another card, and then we also give him another card. That is our coin card. So this mechanic is actually completely taken from harsh stone. And it's exactly what happens in Har Stone. The second player he gets, one more card dealt to him and also he gets this bonus going card and then finally the turn starts through this, you start to turn command. And in the end, we say at two Q so these methods draw a card for both of our players. They will actually generate their own commands. And this new start to turn command, it will be added to the end off our command. Cute. So we will wait until all these guards will be dealt to our players and only then will start in u turn if we scroll a bit lower. There is also this update function just for debug purposes and not to wait for my opponent to and hiss turn sometimes my a I didn't work, so I've included this code. If we just breast space and our keyword, it ends the turn for the current player, so it might work for us as well as for our opponents. If we want, we can press space and will interrupt any turn and the turn will be given to the next player. And then there's this method, public void and turn that we're calling. When we press on that and turn button and when the turn ends, we want to first access our timer and stop the timer. We'll check out our timer script in one of our next lectures. Then we want to call the method off our current player, who Stern is right now that is called on turn and and then we generate this new start to turn command and we pass in the player whose turn is going to go next. And of course, in this case it is our player who Stern is right now until we haven't changed the turn to our other player dot other player. So there are just two players and through each and every one of them, we can access another player through a point and saying other player and we also add this command to queue. So everything that we do in this game that who affect our visual presentation. It happens through the system off commands to ensure that everything happens in a certain order. And then there's this method public void. Stop the timer and it says, just timer dot stop timer. And the only reason for having it here is because our turn manager script is easily accessible from other scripts. So we can always say turn manager dot instance and then say, for example, stop the timer if we need to. And now we already know that if we call our method on game start in this start function, it will just break down our project. And it will not work because we have not established all the connections yet. But in the next couple of Fletcher's, I think that we can bring our project to a state that we can actually include something into this start function, and we'll get to test our rope timer will see our rope actually burned down in the scene. And when we press our intern button than the turn will be just passed on to our other character. So this will be one of our goals for our next couple. If lectures. For now, I'd like to thank you very much for watching the scores and I will see you in our next lecture.