Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 jag45 · Dec 23, 2017 at 07:11 AM · gameobjectliststrategybattle-systemturn based

A list of enemies and players speed to determine turn order

I have some scripts for these players (4 of them, practically identical) and an enemy script. I want to make a list of who is fastest and then have them take their turns down the list. But my enemies and players aren't all game objects. so how should I get them all together?

This is a game where the battle system happens in the blink of an eye. so I don't want to create any unnecessary objects that will be gone in a split second. unless that would fix it.

 public void PBattle(List<GameObject> _party, List<TheEnemy> _enemies)
 {
         foreach(GameObject player in _party)
         {
                 //Get speed
         }
  }

should I make a general class and have them all inherit from it and all it does is have a method in there that gets their speed and returns it? Or should I just have a method that checks everyone's speed every time but I set a counter after each time I use the method. So once it's used and the first person goes it adds 1 to the counter the next time around it sees there's 1 person who went and so it goes to the next one who has the fastest speed.

One last way could be, that it checks the first party members speed, then checks it against all the enemies, then if the player is faster he will have his turn, then we go to the next player and check his speed against all the enemies, say the enemy's speed is higher, so it will have its turn. then we go back to that same player who didn't go yet and have him check to see if he's faster than the rest of the enemies until he can go and so on with the other players. the only problem would be keeping track of who has had their turn and what code would be able to detect that. (including enemies)

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
0

Answer by Alanisaac · Dec 23, 2017 at 05:43 PM

I wouldn't use a base class here, but rather use an interface. This would allow you to have both player and enemy objects implement the interface to check speed without additional objects. For this example, I'm assuming that your speed is represented by a simple integer. Below, I'm only showing the enemy classes implementing the interface, but the player/party objects can do the same thing. You can also keep the PBattle(...) method using two lists, but all the objects should be ICanBattle which will let you check speed. You can add other things to ICanBattle that are common between your players and enemies that are important to your battle system. I don't know what your game is like, but a simple example would be something like health/HP.

 public interface ICanBattle
 {
     int Speed { get; }
 }

 // ---------------------------

 public class TheEnemy : ICanBattle
 {
    public int Speed { get; set; }
 
    // the rest of the class ...
 }

 // ---------------------------

  public void PBattle(List<ICanBattle> _partyAndEnemiesInOneList)
  {
          foreach(ICanBattle battler in _partyAndEnemiesInOneList)
          {
                  var speed = battler.Speed;
          }
   }


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
0

Answer by jag45 · Dec 25, 2017 at 08:23 PM

The problem I have now is I don't know how to add my party exactly to the list. I have a game object for each class script, then I have just one script for enemies that are not attached to any game object. Since all my class scripts are going to be the character themselves, should i just have 1 game object with 4 components of each one of the classes? one object for the class scripts and one object for the enemy scripts. Here is some code i have

     public interface ICanBattle
     { 
         int Speed { get; }
     }

one of the classes

 public class TheAttacker : MonoBehaviour, ICanBattle
 {
     public string _name = "Bob";
     public List<RPGStat> stats = new List<RPGStat>();
 
     public TheAttacker()
     {
         stats.Add(new RPGStat("Level", 1));
         stats.Add(new RPGStat("XP", 0));
         SetStatTempValue("XP", 20);
         stats.Add(new RPGStat("HP", 90));
         stats.Add(new RPGStat("MP", 15));
         stats.Add(new RPGStat("Strength", 10));
         stats.Add(new RPGStat("Intellect", 4));
         stats.Add(new RPGStat("Defense", 6));
         stats.Add(new RPGStat("Speed", 9));
 
         stats.Add(new RPGStat("HPMod", 10));
         stats.Add(new RPGStat("MPMod", 5));
         stats.Add(new RPGStat("StrMod", 1));
         stats.Add(new RPGStat("IntelMod", 1));
         stats.Add(new RPGStat("DefMod", 1));
         stats.Add(new RPGStat("SpeMod", 1));
     }
 
     public int Speed
     {
         get
         {
             return GetStatBaseValue("Speed");
         }
     }
 
     public int GetStatBaseValue(string name)
     {
         foreach(RPGStat stat in stats)
         {
             if(stat.GetName() == name)
             {
                 return stat.GetBaseValue();
             }
         }
         return 0;
     }

the controller that initiates everything

 public class Controller : MonoBehaviour
 {
     public List<GameObject> party = new List<GameObject>();
     public GameObject attacker, tank, mage, healer;
     public GameObject enemyCont;
     public int money = 0;
     // Use this for initialization
     void Start ()
     {
         enemyCont = GameObject.Find("EnemyContainer");
         attacker = GameObject.Find("Attacker");
         tank = GameObject.Find("Tank");
         mage = GameObject.Find("Mage");
         healer = GameObject.Find("Healer");
 
         attacker.AddComponent<TheAttacker>();
         tank.AddComponent<TheTank>();
         mage.AddComponent<TheMage>();
         healer.AddComponent<TheHealer>();
 
         party.Add(attacker);
         //add this code where you aquire these characters
         party.Add(tank);
         party.Add(mage);
         party.Add(healer);
 
         //tank.SetActive(false);
         //mage.SetActive(false);
         //healer.SetActive(false);
     }
 
     public void StartBattle()
     {
         this.GetComponent<BattleController>().SBattle(party);
     }
 }

the battle controller. to set up the enemies and set the attack order (attacker order could be determined in the battle class)

 public class BattleController : MonoBehaviour
 {
     public List<ICanBattle> battleOrder = new List<ICanBattle>();
     private TheAttacker attacker;
     private int numEnemies;
 
     public void SBattle(List<GameObject> party)
     {
         numEnemies = Random.Range(1,4);
         for(int i = 0; i < numEnemies; i++)
         {
             // ADD ENEMIES HERE TO GAME OBJECT OR LIST
             //_enemyCont.AddComponent<TheEnemy>();
             //battleOrder.Add(TheEnemy enemy2 = new TheEnemy());
         }
 
         foreach (GameObject player in party)
         {
             if (player.name == "Attacker")
             {
                 attacker = player.GetComponent<TheAttacker>();
                 Debug.Log("inside");
                 battleOrder.Add(attacker);
             }
             /*else if (playerClass.name == "Tank")
             {
                 battleOrder.Add(playerClass.GetComponent<TheTank>());
             }
             else if (playerClass.name == "Mage")
             {
                 battleOrder.Add(playerClass.GetComponent<TheMage>());
             }
             else if (playerClass.name == "Healer")
             {
                 battleOrder.Add(playerClass.GetComponent<TheHealer>());
             }*/
         }
         Debug.Log(battleOrder.Count);
         //battleOrder.Sort();
         //Debug.Log(battleOrder.ToString());
 
         /*foreach (ICanBattle battler in battleOrder)
         {
             var speed = battler.Speed;
         }*/
 
         gameObject.AddComponent<Battle>();
         //gameObject.GetComponent<Battle>().PBattle(party);
     }
 }

ive been trying to make this work for a couple hours and i still cant get it haha

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

104 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

Related Questions

A node in a childnode? 1 Answer

Add GameObject to List using Timer 1 Answer

Update list on mouse click 1 Answer

Adding a prefab to gameObject at a certain position in runtime 0 Answers

Calling GameObject's transform from list 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