Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 JDCAce55 · Apr 22, 2014 at 06:29 AM · rpgturn-basedbattle-systemobject-oriented-programming

How can I pause one object's Update() until another class has performed its task?

I'm struggling making an RPG turn-based battle system (à la early Final Fantasy), and I'm really trying to take an object-oriented approach. I have a CombatManager class attached to an object that takes all the combatants, sorts them according to each combatants' Speed stat, and tells each combatant when he can act. However, I've run into a problem on that last part: How do I tell the CombatManager to stop its job for a moment so the combatant can do his job? If the combatant is the player, the program will pause and wait for input, and this could take many frames, during which the CombatManager shouldn't do anything. Telling the player to act is simple enough (just call the player's Act() method), but how do I pause the CombatManager then resume it when the player is done?

Upon request, here is my CombatManager. There is still much work to be done on it.

 public class CombatManager : MonoBehaviour
 {
     private List<Combatant> combatants;
     private Queue<Combatant> combatantsQueue;
     private bool isRunning;
 
     private void Awake()
     {
         combatants = new List<Combatant>();
         combatantsQueue = new Queue<Combatant>();
         isRunning = false;
     }
 
     private void Update()
     {
         while(isRunning)
         {
             // Run the combat.
             // Dequeue a combatant and tell it to act.
             // Add it back to the queue (at the end).
             // When it's done acting, CombatManager should be notified so it can dequeue the next combatant.
             // If a combatant is unable to act (such as when he dies), remove him from the queue.
             // Other combatants may revive him, in which case add him back to the queue.
         }
     }
 
     public void AddCombatant(Combatant combatant)
     {
         combatants.Add(combatant);
     }
 
     public void SortCombatants()
     {
         // Sort combatants by speed, highest first
         combatants = combatants.OrderByDescending(c=>c.Speed).ToList();
         foreach(var c in combatants)
         {
             // Add each combatant to the queue, starting with the fastest.
             combatantsQueue.Enqueue(c);
         }
     }
 
     public void StartCombat()
     {
         SortCombatants();
         isRunning = true;
     }
 }

PS - Any general tips on creating an RPG turn-based battle system would be greatly appreciated, as well. I'm having a tough time conceptualizing the design taking into consideration the game loop.

Comment
Add comment · Show 1
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 EvilTak · Apr 22, 2014 at 07:25 AM 0
Share

Could you post the Combat$$anonymous$$anager script so that I get an idea of the script?

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by getyour411 · Apr 22, 2014 at 07:30 AM

 public PlayerAction playerAction;
 
 Update()
 
 if(playerAction.booleanHasNotSelectedAction)
 return;
 else
 // do stuff

Do nothing until the Player has selected their action (i.e return), then do whatever. This is a simple example.

Comment
Add comment · Show 1 · 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 JDCAce55 · Apr 22, 2014 at 08:00 PM 0
Share

Thanks for the suggestion. That may work. I'll play around with it a bit.

avatar image
0

Answer by Andres-Fernandez · Apr 22, 2014 at 07:35 AM

Each turn I would create a list with combatants actions (for instance: player1, enemyA, player2, player3, enemyB). If any combatant has more than 2 actions in a turn, put it that amount of times in the list.

Combat manager calls the Act() method from the first element of the list.

Then, each Act() method within a combatant finishes by triggering an event in the CombatManager that makes the CombatManager pop that action from the list.

To make the CombatManager wait for the current action to complete, just check for the size of the list in the Update function of the CombatManager. If it is the same size as the last frame, it is because the current combatant hasn't finished its action. If size of the list is smaller than last time checked (because the function triggered by the Act() method popped the first element) then call the Act() method of the folowing combatant (which is the first of the list).

Once the size of the list reaches zero, the turn has finished for all combatants.

Rince and repeat till the battle is over.

Probably not the best way to do it, but that's how I would tackle it.

Comment
Add comment · Show 1 · 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 JDCAce55 · Apr 22, 2014 at 07:59 PM 1
Share

Yes, that may work! The 'multiple actions' suggestion may be tricky; my game uses a variable number of actions (like Fallout 1 and 2's action point system), but I'll implement that later, after I get the basics of combat.

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

22 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

Related Questions

Turn Order 2 Answers

Need help making a pokemon turn based system ! 1 Answer

Creating an Interactable Grid for Tactics Game (C#) 1 Answer

Making in turn-based rpg in which characters can take turn ahead of time 0 Answers

Classic turn-based RPG battle scene question 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