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
1
Question by Daliev18 · Mar 16, 2021 at 03:39 PM · script.logic

blacksmith building logic issue

Hi guys, I am pretty new in Unity.

I wanted to create blacksmith building to reset the equipment, but I am facing some issues.

Equipment has to have 3 attributes but all different in 3 line, however my logic sometimes is giving the same attribute in 2 lines which is the behavior I do not want.

Is there any ways of fixing the issue or any way to simply the logic flow? Here is my code:

 public void Awake()
 {        
     troopsAtrribute.Add("Infantary Attack");
     troopsAtrribute.Add("Infantary Hp");
     troopsAtrribute.Add("infantary Defense");
     troopsAtrribute.Add("Archer Attack");
     troopsAtrribute.Add("Archer Hp");
     troopsAtrribute.Add("Archer Defense");
     troopsAtrribute.Add("Cavalary Attack");
     troopsAtrribute.Add("Cavalary Hp");
     troopsAtrribute.Add("Cavalary Defense");

 }

 public void EquipmentReset()
 {
     attributesFirst = Random.Range(8f, 15f);
     attributesFirst = (float)System.Math.Round(attributesFirst, 2);
     attributesSecond = Random.Range(8f, 15f);
     attributesSecond = (float)System.Math.Round(attributesSecond, 2);
     attributesThird = Random.Range(8f, 15f);
     attributesThird = (float)System.Math.Round(attributesThird, 2);

     string firstLineAttributes = troopsAtrribute[Random.Range(0, troopsAtrribute.Count)];
     string secondLineAttributes = troopsAtrribute[Random.Range(0, troopsAtrribute.Count)];
     string thirdLineAttributes = troopsAtrribute[Random.Range(0, troopsAtrribute.Count)];
     
     Resetting = true;

     if(Resetting == true)
     {
         if(attributesFirst <= 10)
         {
             firstLine.color = grey;
             firstLine.text = firstLineAttributes + " " + attributesFirst + "%";                
         }
         if(attributesFirst > 10 && attributesFirst <= 12)
         {
             firstLine.color = blue;
             firstLine.text = firstLineAttributes + " " + attributesFirst + "%";
         }
         if(attributesFirst > 12)
         {
             firstLine.color = red;
             firstLine.text = firstLineAttributes + " " + attributesFirst + "%";
         }

         if(firstLineAttributes != secondLineAttributes)
         {
             if (attributesSecond <= 10)
             {
                 secondLine.color = grey;
                 secondLine.text = secondLineAttributes + " " + attributesSecond + "%";                    
             }
             if (attributesSecond > 10 && attributesSecond <= 12)
             {
                 secondLine.color = blue;
                 secondLine.text = secondLineAttributes + " " + attributesSecond + "%";
             }
             if (attributesSecond > 12)
             {
                 secondLine.color = red;
                 secondLine.text = secondLineAttributes + " " + attributesSecond + "%";
             }

         }
         else
         {
             EquipmentReset();
         }
         if (firstLineAttributes != thirdLineAttributes && secondLineAttributes !=thirdLineAttributes )
         {
             if (attributesThird <= 10)
             {
                 thirdLine.color = grey;
                 thirdLine.text = thirdLineAttributes + " " + attributesThird + "%";                    
             }
             if (attributesThird > 10 && attributesThird <= 12)
             {
                 thirdLine.color = blue;
                 thirdLine.text = thirdLineAttributes + " " + attributesThird + "%";
             }
             if (attributesThird > 12)
             {
                 thirdLine.color = red;
                 thirdLine.text = thirdLineAttributes + " " + attributesThird + "%";
             }

         }
         else
         {
            EquipmentReset();
         }
     }
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

1 Reply

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

Answer by andrew-lukasik · Mar 16, 2021 at 08:03 PM


  1. Too much if/else makes program incomprehensible. Get rid 90% of those.

  2. Try to stay away from writing methods that can ever call themselves. This will lead to program crashing sooner or later (freeze due to infinite loop or exceptions because of stack overflow).

  3. If you want to ensure that some semi-random permutations never repeat it's sometime best to create list of all (reasonably) possible permutations and just remove randomly pick entries from it as you go.

  4. Don't worry, making sure random entries do not repeat is not a trivial task.


Consider this:

 [SerializeField] TroopsAttribute _first;
 [SerializeField] Text _firstLine;
 
 [SerializeField] TroopsAttribute _second;
 [SerializeField] Text _secondLine;
 
 [SerializeField] TroopsAttribute _third;
 [SerializeField] Text _thirdLine;
 
 void Awake ()
 {
     EquipmentReset();
 }
 
 public void EquipmentReset ()
 {
     var pool = CreateNewPool();
 
     _first = CreateRandomAttribute( pool );
     _second = CreateRandomAttribute( pool );
     _third = CreateRandomAttribute( pool );
 
     UdpateText( _firstLine , _first );
     UdpateText( _secondLine , _second );
     UdpateText( _thirdLine , _third );
 }
 
 static TroopsAttribute CreateRandomAttribute ( List<(TroopsType,StatType)> pool )
 {
     UnityEngine.Assertions.Assert.IsTrue( pool.Count!=0 , "pool is empty!" );
     var result = new TroopsAttribute();
     {
         result.Value = Random.Range(8f,15f);
     
         int i = Random.Range( 0 , pool.Count );
         var entry = pool[i];
         pool.RemoveAt(i);
 
         result.Troops = entry.Item1;
         result.Stat = entry.Item2;
     }
     return result;
 }
 
 static void UdpateText ( UnityEngine.UI.Text text , TroopsAttribute attribute )
 {
     text.text = $"{attribute.Troops} {attribute.Stat} {attribute.Value:0.00}%";
 
     if( attribute.Value<10f )
         text.color = Color.grey;
     else if( attribute.Value<12f)
         text.color = Color.blue;
     else// >= 12f
         text.color = Color.red;
 }
 
 static List<(TroopsType,StatType)> CreateNewPool ()
 {
     var pool = new List<(TroopsType,StatType)>();
     List<TroopsType> everyType = new List<TroopsType>( (TroopsType[]) System.Enum.GetValues(typeof(TroopsType)) );
     List<StatType> everyStat = new List<StatType>( (StatType[]) System.Enum.GetValues(typeof(StatType)) );
     for( int indexType=0 ; indexType<everyType.Count ; indexType++ )
         for( int indexStats=0 ; indexStats<everyStat.Count ; indexStats++ )
             pool.Add( ( everyType[indexType] , everyStat[indexStats] ) );
     return pool;
 }
 
 public enum TroopsType { Infantary , Archer , Cavalary }
 public enum StatType { Attack , Hp , Defense }
 
 [System.Serializable]
 public class TroopsAttribute
 {
     public TroopsType Troops;
     public StatType Stat;
     public float Value;
 }
Comment
Add comment · Show 5 · 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 Daliev18 · Mar 16, 2021 at 08:45 PM 0
Share

@andrew-lukasik Thank you so much for your time and advice, it is working perfectly.

avatar image AbandonedCrypt · Mar 17, 2021 at 08:58 AM 0
Share

in general it's pretty easy to make random entries not repeat: ("Pseudo-Code" as I'm writing this on my phone on the train lol)

 List<ListItem> collection, drawn;
 
 ListItem DrawUniqueRandom()
 {
     var randomIndex = Random.Range(0, collection.Count);
     var returnItem = collection[randomIndex];
     collection.RemoveAt(randomIndex);
     drawn.Add(returnItem);    
     return returnItem;
 }
 
 void ResetCollection()
 {
     collection.AddRange(drawn);
 }

avatar image andrew-lukasik AbandonedCrypt · Mar 17, 2021 at 11:55 AM 0
Share

I suggest you try to rewrite that for mutable input elements and making sure no heap allocation happens. Only then tell me all about how easy it was.

avatar image AbandonedCrypt andrew-lukasik · Mar 17, 2021 at 12:13 PM 0
Share

i really got the impression from your initial comment but right now I'm almost sure that you're throwing around terms for the sake of appearing smart. it's very typical for those people to add prerequisites in hindsight in order to strengthen their narrative. no heap allocation! (so what use nativelist and move vars out of method scope) and i dont think you understand mutables

Show more comments

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

144 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

Related Questions

Drill-like system 0 Answers

How to make sounds louder if I go faster 1 Answer

Making an AirPlane follow mouse 1 Answer

How do I access the contents of a script from another script? 2 Answers

How can I write this code, from C# to JavaScript? 2 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