- Home /
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();
}
}
Answer by andrew-lukasik · Mar 16, 2021 at 08:03 PM
Too much
if/else
makes program incomprehensible. Get rid 90% of those.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).
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.
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;
}
@andrew-lukasik Thank you so much for your time and advice, it is working perfectly.
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);
}
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.
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
Your answer

Follow this Question
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