Automatic building construction

I have a (turn-based) town managment element in a game, but the player is mainly concerned with main buildings. I want civilians to build things over time.

Below is the mockup of the code I’m planning on using (typed it in Notepad++, not at my PC atm), I’m interested to hear thoughts on my approach.
I’m torn on how to implement it.
Right now I’m instantiating each house individually.
Another approach would be to pre-place everything in the scene and activate/de-activate.

I could use empty GO’s to hold building locations, or I could make a grid (but that will look unnatural, terrain is 3D and far too uneven, and trying to calculate for each grid position seems like a waste of time and effort)

Lists? Dictionaries?
Finding a game object by name might not be the best method performance-wise, but since it’s a turn based game it shouldn’t be an issue. Alternatively, I could use a List of game objects instead of strings.

I’m jsut far too unsure.

public class CivilianBuilding: Monobehavior
{
    //civilian houses should be automaticly and somewhat randomly built as time goes on
   
    public int t1bcounter;  //used to give each house a unique ID
    public int t2bcounter;
    public int t3bcounter;
   
    public List<GameObject> tier1prefabs; //hold building prefabs to instantiate
    public List<GameObject> tier2prefabs;
    public List<GameObject> tier3prefabs;
   
    public List<string> tier1buildings = new List<string>();  //list of buildings of each tier, by name
    public List<string> tier2buildings = new List<string>();
    public List<string> tier3buildings = new List<string>();       
   
    public List<GameObject> damagedBuildings; //to temporarily store damaged buildings after attack   
    public int daysSinceAttack;  // counter that checks for building repair. Each house has a random chance to be repaired, depending on tier and time passed. Re-sets to 0 when damagedBuildings is empty.
   

    public List<Vector3> freeSlots = new List<Vector3>();  //list of all availalbe house positions. Fill in manually?


    public void ConstructCivilian()  //get random position and instantaite building prefab here
    {       
        //pick random building prefab and radom position
        int rng1 = Random.Range(0, tier1prefabs.Count);
        int rng2 = Random.Range(0, freeSlots.Count);       
       
        GameObject go = Instantiate(tier1prefabs[rng], freeSlots[rng2], Quaterniton.Identity); //instantite prefab at that position
        go.name = "T1building" + t1bcounter; // name it so we can find it easily
        tier1buildings.Add(go.name); //add object name to list of t1 buildings
        t1bcounter++;  //increase counter for buildings of that type (to ensure unique name)
       
        //remove the slot from freeSlots
        freeSlots.Remove(rng2);  //check syntax?

        // parent it to the town root GO for less clutter and better organization           
    }
   
   
    public void UpgradeCivlian()
    {
        //pick random civy building and upgrade
        //depends on safety, population and prosperity of town. Call this method from anteehr that checks for those
       
        int rng = Random.Range(0, tier1buildings.Count); //pick random T1 building
        var toUpgrade = gameObject.Find(tier1buildings[rng]);  // find the game object with that name (check syntax)
        //NOTE TO SELF: if all are parented, perhaps a narrower search of children of town GO?
       
        //instantiate a T2 building in that position
        int rng1 = Random.Range(0, tier2prefabs.Count);
        GameObject go = Instantiate(tier2prefabs[rng], toUpgrade.transform, Quaterniton.Identity); //instantite prefab at that position
        go.name = "T2building" + t2bcounter; // name it
        tier2buildings.Add(go.name); //add object name to list
        t2bcounter++;  //increase counter for buildings of that type

        //remove old building
        tier1buildings.Remove(toUpgrade.name) //remove from list of t1 buildings
        t1bcounter--;  //recude counter of t1 buildings
        Destroy(toUpgrade);
       
    }
   
   
    public void DamageCivlian(int damage)
    {       
        //pick x random buildings depending on battle result
       
        //replace nomrla model with damaged model
       
        //start repair counter
    }
   
   
    public void Repair()
    {
        //go trough the damaged building list. If there are none, re-set counter.
       
        //for each calculate repair chance and roll
       
        //if sucesfull, replace damaged model with normal one.
    }
   
   

}

Yes, yes, the world is wide open, you can really do anything.

It all has to do with exactly what you’re trying to do and how you’re comfortable solving it.

Every single one of the approaches you list above is 100% valid.

I recommend you obtain a coin, select heads or tails, choose a direction and begin coding.

Within an hour or so of serious work on the problem you should gain 100x more insight than anything from an answer here, because a) you will have experienced it firsthand, and b) it will be specifically related to your game context, which nobody here knows.

If you end up choosing wrong, change course and start in a new direction. That’s why it’s called SOFT-ware. It’s soft, it can change. :slight_smile:

1 Like

I know…I’m looking more for “best practices”, so I don’t have to re-write everything. My day job involves sitting at the PC, so working on the game after that is hard on the eyes and mind, so I’m trying to minimize code re-write.

Basically, I want people to look up the code and see if it look OK to them. Any major issues? Suggestions to improve?