Military Structures

👍

Reminder - Keep Daily Backups

When working on a strategy game with a kit as big as the Complete Kit - always keep a working daily backup! Save yourself the trouble of rolling-back changes and losing work.

What are military structures?

The city building kit comes with examples for normal structures like the Wizard Academy or the Classroom - but we're going to talk about another special building category type on this documentation page: military buildings.

These buildings can store units, and increase the total unit cap for players to build bigger armies. The example included in the City Building Kit is called the Tatami item as seen below.

297

Examples of the unit storage structure in the store.

Where to find the military store menu in Unity?

Open the game scene hierarchy and locate the ScrollView in Canvas > ModalsWindows > ShopWindows > ShopGroup > StorePanel > BottomGroup > Content > Content. Look at the below screenshot.

238

UIAnchor. Click to view larger.

Where are the prefabs?

All of the game object prefabs are set in the ScriptableObjects at the path Assets/CityBuildingKit/Resources/Assets/
For example take a look at the MilitaryCategory.asset file below :

662

Game scene Game Manager > SaveLoadMap

In addition, you will want to look at GameManager > Creators > BuildingCreator (prefabs are also there) This is discussed in the Buildings > Part 2: Customize Menus section of the documentation. Prefabs are used by the creator for construction and by SaveLoadBattle for loading during battle.

GameManager > Creators > BuildingCreator

In the GameManager > Creators > BuildingCreator we have the list of game object prefabs used by the creator in addition to certain build settings used by the Scripts/Creators/BaseCreator.cs script. Let's describe these below. But first, here's how to open the BuildingCreator Inspector

273

Game scene Game Manager

In the Inspector you'll notice a few arrays, here's a description of each one. It's the same for every type of game object you can build in the game except for the RemovableCreator (removables are not for players to construct but rather remove -- see Player Removable Objects doc for details)

Array TitleDescription
Structure PfThe structure prefab.
Grass Pf1x1 to 5x5 dark green grass collider grid prefabs list. These you don't have to edit, they are shared by all structures in the game based on their grid size.
Construction PfUnder construction prefab list. There's 3 different sizes of this for 1x1 to 3x3 grid size construction projects.
Grass TypesSize of this array matches the the Structure Pf array size. We tell which Grass Pf element to use with each structure. Depends on structure size whether they take 1x1 (Grass Pf element 0) like a wall segment or 3x3 (Grass Pf element 2) like the large gold vault. Tatami is the only object that takes grass collider 5x5 ( Grass Pf Element 4).
Construction TypesArray size matches Structure Pf. Like grass types, we match the construction prefab grid size too. Since there's 3 different sizes of construction prefabs, this ranges from values 0 to 2. By default, we set most structures to use the 3x size.
Pivot CorrectionsArray size matches Structure Pf. Odd size grid structures (1x1 and 3x3) need a pivot correction so they don't appear accidentally off-center, taking more grid space than necessary. Pivot Correction does this.
Is ArrayArray size matches Structure Pf. If set to 1, uses the row making field builder for construction instead of the usual construction. The only objects that use this are the wall segments that let you pick a start and end point and automatically constructs a repeating row of the structure between the points.
Is InstantArray size matches Structure Pf. For items you set TimeToBuild to 0 (instant build) in the SO, you also want to set the elements Is Instant boolean to 1 in this array. The only objects that use this in the demo are the wall segments.

Inspector: Is Instant booleans set to false

For all buildings - we use the time delay construction. The Is Instant array here matches the prefab list and is set to false for all constructions (except for wall objects).

However for instant construction projects (see the Buildings > Walls documentation project), you want to mark the Is Instant boolean true in the Inspector, for the element in this array matching the same prefab element above. Also, the TimeToBuild element of your SO item must also be equal to zero. Look at the Walls documentation for in-game examples.

348

Inspector: Is Instant booleans

Unit Storage Example (Tatami Structure)

Like many free-to-play strategy games, there's a building that can store units which increases the total unit capacity for the player's army. This allows players to level up with bigger army sizes. The Tatami structure is the example we've included in the City Building Kit

386

Tatami unit storage building. Click to view larger.

What is unit storage?

Just like resources, you can store units in buildings too. This allows players to have a bigger army for attacking other players.

What is an example of the ScriptableObject for soldier storage?

Here's an example of how to do a unit storage building. The key parts are that StoreType is set to Distributed and StoreResources set to Soldiers.

662

If you add new Military Structures or subtract some via the MilitaryCategory.file , please don't forget to update GameUnitsSettings.asset file. Read more info at Documentation > XML files VS ScriptableObjects

How unit storage works

Unit storage structures work by increasing the distributed storage cap for the player's army. In the case of the Tatami example above, you can see it's 25 more of additional unit storage space that gets added to the total player stats (Distributed):

297

This storage addition is added to the total player stats in Scripts/Creators/ConstructionSelector.cs when ProgressBarUpdate() is triggered for the construction completion.

The following excerpt shows the special qualities for various buildings that are updated in the player stats when construction finishes. For the Tatami item, total player army storage is increased by the StoreCap SO item.

// Add unit storage to max housing stats
((Stats)stats).maxHousing += storageAdd;

Seen below:

// ProgressBarUpdate() excerpt
// from Scripts/Creators/ConstructionSelector.cs
//
// The following part of the function updates the player stats depending 
// on what sort of special features the building they create

					//the builder previously assigned becomes available
					((Stats)stats).occupiedDobbits--;			

					// What type of building is it?
					// Any special building attributes are added to the player stats
					if(structureType=="Toolhouse")													
					{
					//increases total builders since they built a builder house
						((Stats)stats).dobbits += 1;
            															
					}
					else if(structureType=="Tatami")									
					{
            // increases total unit storage in Stats	because they built
            // the Tatami example which has unit storage
						((Stats)stats).maxHousing += storageAdd;
					}
					else if(structureType=="Forge")
					{
            // if it's a production building
            // later in this fuction we'll register it for
            // notifictions above the building using
            // RegisterAsProductionBuilding
						isProductionBuilding = true;			
					}
					else if(structureType=="Generator")
					{
            // if it's a production building
            // later in this fuction we'll register it for
            // notifictions above the building using
            // RegisterAsProductionBuilding
						isProductionBuilding = true;		
					}

					else if(structureType=="Barrel")				
					{	
            //increases total mana (currenct #2) storage in Stats														
						((Stats)stats).maxMana += storageAdd;
					}
					else if(structureType=="Vault")
					{
            //increases total gold (currency #1 storage in Stats	
						((Stats)stats).maxGold += storageAdd;
					}

					// since we've probably changed values seen in the UI 
					// (e.g. extra storage with a vault) then update the stats UI
					((Stats)stats).UpdateUI();

		// .........

Example of resource storage cap increase

The following screenshot shows before and after unit army storage addition in the Stats UI. The storage cap is shown just above the progress bars for the total army size. In the below example, we start with 100 unit cap and after building a Tatami structure the player army cap increases by 25 for a new total of 125.

266

Total unity cap before add (100). Click to view larger.

258

Total unit army cap after add, now 25 larger. Click to view larger.

How are player stats updated?

Seen in the above ProgressBarUpdate() function excerpt, the last line calls a UI update which then reflects the army cap changes from 100 to after 125 with the 25 additional unit storage added from the Tatami item in the images above.

((Stats)stats).UpdateUI ();

Where is the max housing variable used?

The script Scripts/Menus/Units/MenuUnit.cs BuyStoreItem() excerpt checks if the player has enough resources and army housing space remaining to build the unit they want to build.

// Scripts/Menus/Units/MenuUnit.cs
// BuyStoreItem() Excerpt
//
		if(trainingIndexes[itemData.GetId()] == GameUnitsSettings_Handler.s.maxUnitsNo)
		{
			canBuild = false;
			MessageController.Instance.DisplayMessage(GameUnitsSettings_Handler.s.maxUnitsNo.ToString()+" units limit.");
		}

		if(Stats.Instance.occupiedHousing + itemData.size>(Stats.Instance.maxHousing))
		{
			canBuild = false;
			MessageController.Instance.DisplayMessage("Increase your soldier housing capacity.");
		}