Search Unity

Need Help Coding Pokemon/Turn Based Battle System

Discussion in 'Scripting' started by MJG1123, May 29, 2015.

  1. MJG1123

    MJG1123

    Joined:
    Apr 12, 2015
    Posts:
    24
    Watch this video, I got most of the work done but I am having trouble with coding the enemy's turn in which I need text to display for an amount of time before damage is calculated and then the combat state is switched back to players choice.I tried using Invoke () as seen in the video but the code runs multiple times which is why i'm stuck again... Or if you have a better way of doing this whole menu system let me know, this is starting to become a headache.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    Most of these types of things are done with coroutines in Unity, specifically using StartCoroutine() and passing in a function to call "over time."

    You can make your coroutine accept a functor to call when it has finished, such that the rest of the game can continue. Here is a simple example of such a time-drawn-out process:

    Code (csharp):
    1.     // example of functor used to "continue" after a Coroutine finishes
    2.     IEnumerator RunOneTurn( System.Action finished)
    3.     {
    4.         Debug.Log ( "Turn begins!");
    5.         yield return new WaitForSeconds( 1.0f);
    6.         Debug.Log( "Enemy announces actions...");
    7.         yield return new WaitForSeconds( 1.0f);
    8.         Debug.Log( "Enemy performs actions...");
    9.         yield return new WaitForSeconds( 1.0f);
    10.         Debug.Log( "Results of actions are viewable...");
    11.         yield return new WaitForSeconds( 1.0f);
    12.         finished();
    13.     }
    14.  
    15.     // your UI layer would only function when this is enabled
    16.     bool MyInputIsEnabled = true;
    17.  
    18.     // when your UI is enabled, beginning the turn would call this
    19.     public void BeginTurn()
    20.     {
    21.         // turns off your input layer until the turn has played out
    22.         MyInputIsEnabled = false;
    23.         StartCoroutine( RunOneTurn ( () => { MyInputIsEnabled = true;}));
    24.     }
     
  3. MJG1123

    MJG1123

    Joined:
    Apr 12, 2015
    Posts:
    24
    that sorta of worked, when I copy your code it works fine but I can't seem to get it to work when I try and apply it to my code (video)
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    I recommend that you don't just copy and "apply" code, but rather understand the underlying construct used in my example. Review functors and coroutines to see what is going on there, and how to apply it in your precise situation.
     
    Kiwasi and Mycroft like this.
  5. MJG1123

    MJG1123

    Joined:
    Apr 12, 2015
    Posts:
    24
    NO i meant I copied and pasted to test it before I tried to apply to my code, I didn't just copy what you wrote into my code.
     
  6. SubZeroGaming

    SubZeroGaming

    Joined:
    Mar 4, 2013
    Posts:
    1,008
    Just using yield statements can accomplish what you want. IF you need to know when something has got to a certain state, you could do what kurt said and use a lambda expression for callback functionality and pass through logic.

    Maybe if you paste what you have completed so far, some can help you a little further. It's not really clear what you're asking for at this point.
     
  7. MJG1123

    MJG1123

    Joined:
    Apr 12, 2015
    Posts:
    24
    I got it to work thanks Kurt