Search Unity

Turn-Based Combat Using Switch Case

Discussion in 'Scripting' started by DRRosen3, Dec 18, 2014.

  1. DRRosen3

    DRRosen3

    Joined:
    Jan 30, 2014
    Posts:
    683
    Well, as the title implies I'm wondering a few things about this methodology of creating turn-based combat. I was following a YouTube tutorial series about this, but since then I've been implementing aspects unique to my game, and I'm hitting a few snags. For instance, animations. There's a state where attacks, using items, casting spells, and other various animations have to be done. The problem is that the tutorial had me set up the Switch statement in the Update function. With that being the case, the animations are called over and over and over and over (you get the point) until the case switches. Is there a work-around for this, or is using the Update function to power turn-based combat using switch cases a bad idea? (It worked perfectly all the way up until adding animations.)
     
  2. Tomnnn

    Tomnnn

    Joined:
    May 23, 2013
    Posts:
    4,148
    You can make a new state after an animation finishes playing, or introduce an idle animation and add that to your switch.
     
  3. DRRosen3

    DRRosen3

    Joined:
    Jan 30, 2014
    Posts:
    683
    Well...posting the entire code would be WAY too extensive, but here's the part that's giving me trouble...

    Code (CSharp):
    1. case (BattleStates.CAPTUREATTEMPT):
    2.             StartCoroutine(gameObject.GetComponent<Movement>().CapturePokemon(enemy));
    3.             break;
    4.         case (BattleStates.SPEEDCHECK):
    5.             ChooseWhoGoesFirst();
    6.             break;
    7.         case (BattleStates.PLAYERSATK):
    What happens is the coroutine starts (and in the coroutine is where the animation is triggered). However, since I'm starting the coroutine inside the Update function, it just starts over and over and over again, when I only want it to start once.
     
  4. Tomnnn

    Tomnnn

    Joined:
    May 23, 2013
    Posts:
    4,148
    What signifies the end of what's going on in the coroutine? You can have a bool for success or failure of those conditions. Like set it to true when it first starts, and set it to false when it ends (maybe where the case changes).

    Code (CSharp):
    1. case (whatever):
    2.     if(!started_coroute)
    3.     {
    4.         started_coroute = true;
    5.         StartCoroutine(stuff);
    6.     }
    7.  
    8. ...
    9.  
    10. void stuff()
    11. {
    12.     //your coroutine stuff
    13.     if(the condition that signifies the end of this coroutine)
    14.     {
    15.         //the code you normally execute
    16.         started_coroute = false;
    17.     }
    18.     if(the condition that signifies failure for this coroutine)
    19.     {
    20.         //failure code execution
    21.         started_coroute = false;
    22.     }
    23. }