Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Schaezar · Oct 30, 2017 at 11:22 PM · c#turn based

Active Turn-Based Battle System Help

So I have this turn-based battle system. So far, it's working pretty well and each "hero" can choose an action, this action gets added to a list and the first one in that list gets processed. However, one of the requirement of the game is that each action has a certain delay before it's processed, meaning that if playerA choose to take actionX with a delay of 12 seconds and playerB choose to take actionY with a delay of 4 seconds, if actionX still has more than 4 seconds to go, actionY will become the next action to be processed. Also, right now, actionX has to be completed (and 12 seconds need to pass) before actionY "timer" starts... which is not good as each "timer" should update in parallel...
.

I have an idea about how to reorder the actors list when I add a new one to it, based on the time left before each action. But I have absolutely no clue how to process everything in parallel and make it so that all the "timers" update at the same time instead of one after the other...

 public class BattleStateMachine : MonoBehaviour {

     public enum BattleStates {
         INITIALIZE,
         WAITING,
         PROCESS_ACTION,
         IDLE,
         CHECK_IF_ALIVE,
         WON,
         LOST
     }

     public BattleStates currState;
     
     public List<Actor> actors = new List<Actor>();      //List of actors, which contains the details of the action taken by the battler

     // Update is called once per frame
     void Update() {
     
         switch(currState) {
             case BattleStates.INITIALIZE: {
                     currState = BattleStates.WAITING;
                     break;
                 }

             case BattleStates.WAITING: {
                     if(actors.Count > 0)
                         currState = BattleStates.PROCESS_ACTION;
                     break;
                 }

             case BattleStates.PROCESS_ACTION: {
                     HeroStateMachine hsm = actors[0].Origin.GetComponent<HeroStateMachine>();
                     hsm.target = actors[0].Target;
                     hsm.currState = HeroStateMachine.TurnStates.ACTION;
                     break;
                 }
         }
     }
 }

 public class Actor : MonoBehaviour
 {
     public string ActorType;

     public GameObject Origin;
     public string OriginName;
     public GameObject Target;

     public string ActionType;
     public BaseAction ChosenAction;
 }

 public class HeroStateMachine : MonoBehaviour {

     public BattleStateMachine bsm;

     public enum TurnStates {
         WAITING,
         ADDTOLIST,
         IDLE,
         ACTION,
         DEAD
     }
     
     public TurnStates currState;
     
     void Update () {

         switch(currState) {
             case TurnStates.WAITING: {
                     UpdateATBGauge();
                     break;
                 }

             case TurnStates.ADDTOLIST: {
                     bsm.heroesToManage.Add(this.gameObject);
                     currState = TurnStates.IDLE;

                     break;
                 }

             case TurnStates.IDLE: {
                     break;
                 }

             case TurnStates.ACTION: {
                     StartCoroutine(TimeForAction());
                     break;
                 }
         }
     }
     
     private IEnumerator TimeForAction() {
         if(actionStarted)
             yield break;

         actionStarted = true;

         yield return new WaitForSeconds(bsm.actors[0].ChosenAction.ActionDelay);

         DoDamage();

         bsm.actors.RemoveAt(0);
         bsm.currState = BattleStateMachine.BattleStates.WAITING;
         actionStarted = false;

         statsPanel.progressGauge.value = 0f;
         currState = TurnStates.WAITING;
     }
 }

Oh masters of Unity (Or C# in general), if you know of a way, please help me because I've been stuck on this issue for a lot longer that I would admit...

Thanks,

A lot of the code about how the Actor instance is built doesn't appear here but since the BattleStateMachine is pushing 600 lines, I didn't include anything that wasn't related to the question and had no effect on the turn order.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by ItsaMeTuni · Oct 30, 2017 at 11:47 PM

Create a counter for each of the actions (a float, not an int). Then on Update add Time.deltaTime to all counters ( counter += Time.deltaTime) and if a counter value exceeds the time limit (which would be 12 seconds or 4 seconds, if(counter >= timeLimit)) it means that the action is finished. I didn't read your code because I don't have time to do it right now (sorry), but I think that you can adapt what I said to your needs.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

Answer by Schaezar · Oct 31, 2017 at 11:02 AM

Someone answered my question on reddit but it's very similar to what you said here. Add ActionTime to the BaseAction class and a function to calculate it's value whenever it's added to the actors list:

 public class BaseAction
 {
     public string Name;
     public float ActionDelay;
     public float ActionTime;
 
     public void CalculateActionTime()
     {
         ActionTime = Time.time + ActionDelay;
     }
 }

Modify Actor like so :

 public class Actor
 {
     public string ActorType;
 
     public GameObject Origin;
     public string OriginName;
     public GameObject Target;
 
     public string ActionType;
     public BaseAction ChosenAction;
 
     public BattleStateMachine BSM;
 
     public void AddToActionQueue()
     {
         ChosenAction.CalculateActionTime();
         BSM.actors.Add(this);
     }
 
     public void ExecuteAndRemoveFromQueue()
     {
         HeroStateMachine hsm = Origin.GetComponent<HeroStateMachine>();
         hsm.currState = HeroStateMachine.TurnStates.ACTION;
         hsm.chosenAction = ChosenAction;
         hsm.target = Target;
         BSM.actors.Remove(this);
     }
 }

Call the AddToActionQueue() function when ready to Add the actor to the actors list and then check with this loop :

 case BattleStates.PROCESS_ACTION:
     {
     foreach(Actor actor in actors)
     {
         if(actor.ChosenAction.ActionTime <= Time.time)
         {
             actor.ExecuteAndRemoveFromQueue();
             break;
         }
     }
     break;
     }

If someone is ready to take their turn. There's no need to sort the list in this case. This also frees the Coroutine in the HeroStateMachine to wait some time for an animation to finish when I add it in the future.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

415 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Making a bubble level (not a game but work tool) 1 Answer

An OS design issue: File types associated with their appropriate programs 1 Answer

Stuck when want to make the AI in Turn-Based Strategy Game 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges