Resource Generators

👍

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 resource generators?

When playing city-building games or battle strategy games you'll always have some sort of economy. In the case of city-builders it could be resources like steel, wood, stone, or money or in the case of battle strategy games like Clash of Clans it could be gold and liquids. These are in-game currencies your players try to collect either through raiding other player villages or building resource generators.

Two types of in-game resources: Gold and Liquids

In the City Building Game Kit we provide two types of resource generators, gold mines and liquid mana collectors. The names may be unique in this example but the concept is simple: in-game resource #1 and in-game resource #2. (In addition there's a rare in-game currency that can be purchased with real-world money known as crystal gems, but that's a topic under the In-App Purchases section on the left)

Shop Menu

Here's a screenshot of the shop menu for the resources. There are three different types of items on this page, resource generators (like the Gold Mine Forge), resource storage (like the Gold Vault) and the builder huts (Dobbit Toolhouse - how to get additional city builders)

636

Click to view larger.

743

Click to view larger.

Where to find the resource store menu in Unity?

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

403

Dynamic Container for every Store item. Click to view larger.

Store Item

All of the store details seen in the below screenshot are taken from the Assets/CityBuildingKit/Resources/Assets/BuildingsCategory.asset file and loaded by Scripts/Creators/BaseCreator.cs (details below on what part of the script does this)

483

Breakdown of the information shown to players in the store.

From Image AboveDescription
Item NameThis is the Name variable
Time to BuildThe TimeToBuild variable (in minutes). How long the structure will take to to be created.
# Build / Level LimitThe number of this structure already created versus the number limited by their level. See Gameplay > XP and Levels documentation for details)
Currency CostThe Price variable.
Currency TypeThe Currency variable (Either Gold, Mana, or Gems) You could change this to whatever currency you will have in your game and also add additional currencies.
View DescriptionShows the Description text variable.
Prefab ImageMore details on how to change this under the Buildings > Part 2: Customize Menus page of the documentation

Resource Generators

The first type of resource building are generators. These create in-game currencies for players over time. See the Buildings > Resource Generators documentation page for more details about these structures.

294

Resource Generators

Resource Storage

The second type of resource buildings are storage buildings. These store in-game currency and add capacity to the player's stats. For example, 250,000 total gold capacity.

150

Resource Storage Example

Additional Builders

The last type of resource buildings are a special type known as builder huts, or Dobbit Toolhouses in the demo. These allow the player to have additional builders and construct more than one building at a time. They're considered a builder resource, since it's limited.

141

Builder hut

By default, players get one builder and can purchase more in the store here. The screenshot below shows the HUD Dobbit builder stat for the player's one builder which is active. Purchasing additional builders in the store increases the total by one for a price of 25 gems (or whatever value you set in the BuildingsCategory.asset -- see more details below)

900

One builder active. Click to view larger.

Watch about how builders work in the game as a limited resource:

Resource BuildingsCategory.asset Example

Here's an example of how to do a resource generator, in this case a gold mine, in the ScriptableObject (SO). For resource generators, the single difference most noticeable is that ProdType (production type) is set to either Gold or Mana, instead of None.

625

ScriptableObject (BuildingsCategory.asset) parameters for a resource storage (Gold Mine example)

If you look closely, you'll see the gold forge costs the opposite currency (in this case mana) to purchase and produce 100 gold per hour with 500 internal gold storage. This internal storage is not a part of the player's gold capacity and when production reaches this amount the gold will no longer product until the player taps the building to save it to their distributed gold storage. (See details below on the Gold Vault XML example)

512

Meanwhile the Mana generator is the exact opposite in costs to the Gold Mine. The mana generator costs 100 gold, produces 100 mana per hour and internally stores 500 mana. This is done to balance the game so that players have to earn both currencies to be able to level up their base equally.

512

ScriptableObject (BuildingsCategory.asset) parameters for a resource storage (Gold Vault example)

Following the example from above, a Gold Vault has no resource production (ProdType is set to None and ProdPerHour set to 0) but the storage is now set to Distributed which means it adds to the player's total Gold capacity unlike Internal which is capped and stops production when reached unless the player retrieves the resources.

512

ScriptableObject (BuildingsCategory.asset) - want to store both resources in one building?

This is possible with the kit. Select the Dual option for StoreResource to add the value in StoreCap to the player's total storage capacity for both in-game resources. The Summoning Circle item is an example of this in the demo. Here's an excerpt from it's parameters:

563

BaseCreator.cs

In Scripts/Creators/BaseCreator.cs the building data from your SO files (BuildingsCategory.asset & MilitaryCategory.asset) is loaded using a common system for the resource buildings, defense buildings, walls, decorations, and other buildings. Here's an excerpt from BaseCreator.cs of the SO data load:

//reads structures XML
protected void GetBuildingsXML()//reads structures XML
	{
		List<BuildingCategoryLevels> buildingCategoryLevels = ShopData.Instance.BuildingsCategoryData.category;
		List<BuildingsCategory> buildingsCategoryData = buildingCategoryLevels.SelectMany(level => level.levels).Where(c => c.level == _structuressWithLevel).ToList();
		
		List<MilitaryCategoryLevels> militaryCategoryLevels = ShopData.Instance.MilitaryCategoryData.category;
		List<MilitaryCategory> militaryCategoryData = militaryCategoryLevels.SelectMany(level => level.levels).Where(c => c.level == _structuressWithLevel).ToList();

		FillBuildingData(buildingsCategoryData);
		FillBuildingData(militaryCategoryData);
}

When the data is loaded, the store panels are updated with the appropriate information.

XP levels and building maximums

Read the Gameplay > XP and Levels documentation page for more details.

What happens when a store button is clicked?

When the player clicks one of the buttons to build a building in the store, first one of the building functions is triggered, like this example below:

//receive a NGUI button message to build
	public void OnBuild0()	 { currentSelection=0; 	Verify(); }
//when a building construction menu button is pressed

Verify() is run shortly after, which initiates the conditions checking to see if the player actually can build what they want to build.

protected void Verify(Action callback = null, bool isUpgrade = false)
	{
		if (isArray[currentSelection] == 0) 
		{			
			VerifyConditions (callback, isUpgrade);
		}
		else 
		{			
			isField = true;
			drawingField = true;
			Delay ();
			VerifyConditions (callback, isUpgrade);
		}
	}

VerifyConditions()

Can the player build this structure? In Scripts/Creators/BaseCreator.cs the VerifyConditions() script takes over once the store button has been pushed to determine if the following conditions are met and canBuild is set to true.

VerifyConditions() Test #1 - Maximum structures reached?

First, we check if the player's level restricts the structure from being made. Have they exceeded the number of structures allowed by their level in XML/EvolutionBuildings.xml? (See Gameplay > XP and Levels documentation for more information on this)

//max allowed structures ok?
		if (existingStructures[id] >= maxCount)//max already reached
		{					
			canBuild = false;
			MessageController.Instance.DisplayMessage("Maximum " + maxCount + 
				" structures of type " + structureName+". " + "Upgrade Summoning Circle to build more."); //displays the hint - you can have only 3 structures of this type
		}

VerifyConditions() Test #2 - Do they have enough funds?

The second test the player data must pass is their funds - do they have enough of the currency the building requires to be constructed?

// Get the building cost
		//enough gold?
		if (currencyType == CurrencyType.Gold) //this needs gold
		{
			int existingGold = ((Stats)stats).gold + ((Stats)stats).deltaGoldPlus - ((Stats)stats).deltaGoldMinus;

			if (existingGold < priceOfStructure) 
			{
				canBuild = false;
				MessageController.Instance.DisplayMessage("Not enough gold.");//updates hint text
			}
		} 
		else  if (currencyType == CurrencyType.Mana)
		{
			int existingMana = ((Stats)stats).mana + ((Stats)stats).deltaManaPlus - ((Stats)stats).deltaManaMinus;

			if(existingMana < priceOfStructure)
			{
				canBuild = false;
				MessageController.Instance.DisplayMessage("Not enough mana.");//updates hint text
			}
		}
		else
		{
			int existingCrystals = ((Stats)stats).crystals + ((Stats)stats).deltaCrystalsPlus - ((Stats)stats).deltaCrystalsMinus;

			if(existingCrystals < priceOfStructure)
			{
				canBuild = false;
				MessageController.Instance.DisplayMessage("Not enough crystals.");//updates hint text
			}
		}

VerifyConditions() Test #3 - Do they have an available builder?

In the City Building Kit each building under construction takes one builder. Players get a certain number of builders to start with (default is 1 in the demo) and you can adjust this for your game. It costs extra rare gems to get additional builders like games such as Clash of Clans. (The builder hut is called a dobbit hut in the demo, and can be seen in the resource shop screenshot above.)

if (((Stats)stats).occupiedDobbits >= ((Stats)stats).dobbits) //dobbit available?
		{
			canBuild = false;
			MessageController.Instance.DisplayMessage("You need more dobbits.");
		}

VerifyConditions() - If all tests pass, greenlight construction.

If the player's data passes all 3 tests and canBuild still is set to true, then the script adjusts the player data awarding the XP they get from the building XML (if any) and deducting the costs involved from the building currency and price.

if (canBuild)
		{
			if (callback != null)
			{
				callback.Invoke();
			}
			
			
			((MenuMain)menuMain).constructionGreenlit = true;//ready to close menus and place the building; 
			//constructionGreenlit bool necessary because the command is sent by pressing the button anyway

			((Stats)stats).experience += xwAward; //incre	ases Stats experience  // move this to building finished 

			if(((Stats)stats).experience>((Stats)stats).maxExperience)
			{
				((Stats)stats).level++;
				((Stats)stats).experience-=((Stats)stats).maxExperience;
				((Stats)stats).maxExperience+=100;
			}

			//pays the gold/mana price to Stats
			if(currencyType == CurrencyType.Gold)
			{
				((Stats)stats).SubstractResources (priceOfStructure, 0, 0);
			}
			else if(currencyType == CurrencyType.Mana)
			{
				((Stats)stats).SubstractResources (0, priceOfStructure, 0);
			}
			else
			{
				
				((Stats)stats).SubstractResources (0, 0, priceOfStructure);

			}

			UpdateButtons ();//payments are made, update all

			((Stats)stats).UpdateUI();//tells stats to update the interface - otherwise new numbers are updated but not displayed

			if (!isField || buildingFields) 
			{
				if (needToDeleteBeforeUpgrade)
				{
					CancelObject();
					needToDeleteBeforeUpgrade = false;
				}
				UpdateExistingStructures (+1); //an array that keeps track of how many structures of each type exist
				InstantiateStructure (isUpgrade);
			}
		} 
		else 
		{
			((MenuMain)menuMain).constructionGreenlit = false;//halts construction - the button message is sent anyway, but ignored
		}

ConstructionGreenlit = true is sent the MenuMain script which then deactivates the store panel the player was just on and waits for the building to be placed.

((MenuMain)menuMain).constructionGreenlit = true;
// ready to close menus and place the building; 
// constructionGreenlit bool necessary because the command is sent by pressing the button anyway