Hi,
Firstly I am looking for a bit of a discussion here, not “a please write my code for me”.
I am working on a strategy game, and pretty much everything is working in terms of logic. However, my code is on the verge of becoming one large plate of spaghetti and I want to start pulling it back and tidying it up.
The game is a strategy game, there are Roman legions, forts, fleets and barbarian settlements. This is how I am currently creating my legions.
Each legion prefab has a script called Legatus, which (cut down) looks like this
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Legatus : MonoBehaviour {
public string lName;
public string lStrength;
public int lTalents;
}
and then there is a worldBuilding script called at the start of the game.
using UnityEngine;
public class WorldBuilder : MonoBehaviour {
public Transform Army;
// Use this for initialization
void Start () {
BuildWorld ();
}
// Update is called once per frame
void Update () {
}
void BuildWorld ()
{
//add Legions
Transform Leg1 =Instantiate (Army, new Vector3 (0, 0, 4.5f), Quaternion.identity);
Transform Leg2 =Instantiate (Army, new Vector3 (1.5f, 0, 3.75f), Quaternion.identity);
Transform Leg3 =Instantiate (Army, new Vector3 (2, 0, 3.25f), Quaternion.identity);
Transform Leg4 =Instantiate (Army, new Vector3 (2, 0, 2.25f), Quaternion.identity);
Transform Leg5 =Instantiate (Army, new Vector3 (2.75f, 0, 1), Quaternion.identity);
Transform Leg6 =Instantiate (Army, new Vector3 (3.25f, 0, 0), Quaternion.identity);
Leg1.transform.parent = GameObject.Find("LegionData").transform;
Leg2.transform.parent = GameObject.Find("LegionData").transform;
Leg3.transform.parent = GameObject.Find("LegionData").transform;
Leg4.transform.parent = GameObject.Find("LegionData").transform;
Leg5.transform.parent = GameObject.Find("LegionData").transform;
Leg6.transform.parent = GameObject.Find("LegionData").transform;
Leg1.GetComponent<Legatus> ().lName = "TUTORIAL LEGION";
Leg2.GetComponent<Legatus> ().lName = "I GERMANICA";
Leg3.GetComponent<Legatus> ().lName = "V ALAUDAE";
Leg4.GetComponent<Legatus> ().lName = "XX VALERIA VICTRIX";
Leg5.GetComponent<Legatus> ().lName = "XXI RAPAX";
Leg6.GetComponent<Legatus> ().lName = "II AUGUSTA";
Leg1.GetComponent<Legatus> ().lStrength = "STRONG";
Leg2.GetComponent<Legatus> ().lStrength = "STRONG";
Leg3.GetComponent<Legatus> ().lStrength = "NORMAL";
Leg4.GetComponent<Legatus> ().lStrength = "DECIMATED";
Leg5.GetComponent<Legatus> ().lStrength = "STRONG";
Leg6.GetComponent<Legatus> ().lStrength = "NORMAL";
Leg1.GetComponent<Legatus> ().lTalents = 100;
Leg2.GetComponent<Legatus> ().lTalents = 100;
Leg3.GetComponent<Legatus> ().lTalents = 100;
Leg4.GetComponent<Legatus> ().lTalents = 75;
Leg5.GetComponent<Legatus> ().lTalents = 50;
Leg6.GetComponent<Legatus> ().lTalents = 50;
}
}
And it works. Later on, in my game there are all kind of other scripts, that react on click events, change the properties, show data in the UI etc, etc.
Where it all is going scu-wiffy, is some of my routines are becoming massive long nested if statements. This isn’t so much a problem with my legion scripts, but my settlements have tons of information, such as population size, loyalty, number of druids, trade agreements, strengths, etc
Now , from what I have been reading up on so far,
I could potentially …
i) Use 2d Arrays
ii) Use Dictionarys
Or there are some other options, such as hash tables which seem to rely on outside libraries?
I am unsure what approach I should be pursuing, so any thoughts welcomed.
