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 GhandiJones · Nov 28, 2013 at 07:24 AM · coroutinetimerfinite-state-machine

Active Battle System implementation question.

Ok... I've looked into this quite a bit and haven't really found a solution at all.

I'm trying to achieve something along the lines of a Final Fantasy style battle system. And am not really sure how to achieve this. Here is some pseudo-code.

 Determine the amount of characters on the battle field;
 
 Start all subsequent timers -- then:
     
     if(Player's Turn)
         Display Player's options;
     
     if(Enemy's Turn)
         Run A* to determine action;
         
     Continue timers;
     
     if(All enemies or players are dead)
         End round;    
     


I know I'm going to need to get an array or list of all active characters in the scene and start their timers. Now, here is the part that gets me... do I need to implement a Finite State Machine or use coroutines to pause the other timers after one of the timers has filled up completely? If you guys wouldn't mind sharing some pseudo-code or pointing me in the right direction with some links/tutorials for broadening my knowledge on how to achieve this type of effect, I would greatly appreciate it. I just want to learn. Thanks ahead of time.

Comment
Add comment · Show 3
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 karljj1 · Nov 28, 2013 at 08:31 AM 0
Share

What about a central controller class that manages the turns. Every enemy/player could inherit from a Base abstract class with functions such as OnTurn that get called by the controller when its their turn.

avatar image GhandiJones · Nov 29, 2013 at 07:40 PM 0
Share

Something like that is already occurring, the problem I'm having is with all of the timers, and pausing the whole thing.

avatar image GhandiJones · Dec 02, 2013 at 05:29 AM 0
Share

No one has any good information at all?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Jamora · Dec 02, 2013 at 08:27 AM

I think you'd be best off creating a Battle-class that you instantiate for each battle. Alternatively you could use a singleton, but because your game is not in a constant state of battle, I do not recommend a singleton. You seem to have outlined the Battle-class pretty well in your snippet.

My code is in C#, but UnityScript has the same logic. To control the timers, you could have the Battle-class run a coroutine that loops over each player and adds to its timer. If the timer if full, give that player his turn.

The timer-code could look like this:

 /*Because the battle is its own class, you could have different kinds of battles;
 ones with slow timers, or with no timers... maybe with constant damage to all
 players. Just do the required modifications in this class.*/
 public class Battle{

     //The timers aren't a feature of the character. They only exist in the battle.
     Dictionary<Character, float> timer; 

     private IEnumerator BattleUpdate(){
 
         while(true){
             foreach (KeyValuePair<Character,float> kvp in timer){
                 //add to each timer
                 if (kvp.Value > kvp.Key.timeToAllowTurn){
                     yield return kvp.Key.DoTurn(); //wait until turn is completed
                     kvp.Value = 0f;
                 }
             }
             yield return null;
         }
     }
 }

If the battle system is done this way, each character only needs to know its own battle behavior (DoTurn) and not care about timers or other players or anything. If you need to allow more than Characters in the Battle, then make Character an abstract class, or use an interface.

Once you're done with the battle, just Destroy the object like any other.

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

20 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Making a fill take exactly n seconds to complete 2 Answers

Laptime timer using TimeSpan - force milliseconds to 00 and not 000? 1 Answer

How can I make a "Fastest Lap" scoring system for a racing game? 2 Answers

Editor script coroutine/timer/delay...? 3 Answers


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