# Custom Build Conditions

This guide will quickly walk through how to add Custom Build Conditions to your build system easily. With this system, you can connect different systems with Easy Grid Builder Pro for adding conditions without a any direct dependency and only with internal events.

{% hint style="success" %}
You can use this system to not only just add cost conditions with inventories, but can also use for timed placements, check stats, check terrain conditions, check other objects, check collision, and so much more. The sky is the limit!
{% endhint %}

## 01. Setting Up Build Conditions

You need to follow few steps to set up build conditions to your buildable objects. First step is to add the integration to your system. This can be a Inventory system or anything you want. Then you need to modify the [**Build Conditions SO**](../scriptable-objects/build-conditions-so) scriptable object to fit with your system and conditions.&#x20;

After that you can create your own [**Build Conditions SO**](../scriptable-objects/build-conditions-so) assets with the data to your likings (_You can find example **BuildConditionSO** assets located in **EasyGridBuilder Pro > Scriptable Objects >**_**&#x20;Build Condition SO**). Then enable **Enable Build Condition** property from the **Buildable Grid / Free Object Type SO** assets and add the created **Build Conditions SO** asset.

## 02. Integrate Your System

Easy Grid Builder Pro has an example integration for a simple inventory system. We can explore from there. **Open Demo 3D All In One** scene located in **EasyGridBuilder Pro > Demo Scenes** and select the child object **Sample BuildCondition Inventory** inside **EGB Pro > Demo Sample Script Objects** object and you can see the object has a component **Example Inventory Build Condition.**

This is a simple inventory system script. We don't need to know how the inventory is set up, We only need to know how the **Build Conditions** are integrated. So simply open the component script by right-clicking on it and selecting Edit Script. Similar to the image below

<figure><img src="https://4076857463-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FzgUHYggWkJIGXQGAerRm%2Fuploads%2FoOmjT6kPTHz6g5pblkis%2FScreenshot_117.png?alt=media&#x26;token=ff7c456e-2143-477c-8c81-fb85fcc5ee42" alt=""><figcaption></figcaption></figure>

_**NOTE:** In this case, we are gonna add integration to a simple inventory system, instead this can be your own inventory script, a stats system, a day-night cycle, a weather system, or any system you want._

Now after opening the script let's go from top to bottom to understand what it does and what should be implemented.&#x20;

```csharp
using SoulGames.EasyGridBuilderPro;
```

At the top in **Line 3** add the namespace. This should be added to your script. From the start to **Line 61** is all inventory code. So let's skip that.

_**NOTE: Line 10, 11, and 12** you can see this inventory script has 3 items (Food, Metal, and Wood) in this example when we check for conditions we are check for these items and also consume these items when build is complete._

From **Line 70** to **Line 76** in **OnEnable()** method we subscribe to four static events. _When you are integrating Build Conditions into your system you need to add this function as it is._

These events are called from the [**Multi Grid Build Condition Manager**](../primary-components/multi-grid-build-condition-manager) component.

```csharp
private void OnEnable()
{
   MultiGridBuildConditionManager.OnBuildConditionCheckBuildableGridObject += CheckBuildConditionBuildableGridObject;
   MultiGridBuildConditionManager.OnBuildConditionCompleteBuildableGridObject += CompleteBuildConditionBuildableGridObject;
   MultiGridBuildConditionManager.OnBuildConditionCheckBuildableFreeObject += CheckBuildConditionBuildableFreeObject;
   MultiGridBuildConditionManager.OnBuildConditionCompleteBuildableFreeObject += CompleteBuildConditionBuildableFreeObject;
}
```

Also we are unsubscribing from the subscribed events in **OnDisable()** method to prevent any unnecessary memory leak issues from **Line 79** to **Line 85.** _When you are integrating Build Conditions into your system you need to add this function as it is._

<pre class="language-csharp"><code class="lang-csharp">private void OnDisable()
{
   MultiGridBuildConditionManager.OnBuildConditionCheckBuildableGridObject -= CheckBuildConditionBuildableGridObject;
<strong>   MultiGridBuildConditionManager.OnBuildConditionCompleteBuildableGridObject -= CompleteBuildConditionBuildableGridObject;
</strong>   MultiGridBuildConditionManager.OnBuildConditionCheckBuildableFreeObject -= CheckBuildConditionBuildableFreeObject;
   MultiGridBuildConditionManager.OnBuildConditionCompleteBuildableFreeObject -= CompleteBuildConditionBuildableFreeObject;
}
</code></pre>

## 03. First Event Method

Now let's check the first event method, **CheckBuildConditionBuildableGridObject()** from **Line 90** to **Line 114.**

<pre class="language-csharp"><code class="lang-csharp">private void CheckBuildConditionBuildableGridObject(BuildableGridObjectTypeSO buildableGridObjectTypeSO)
{
    foreach (BuildableGridObjectTypeSO item in MultiGridBuildConditionManager.BuildableGridObjectTypeSOList)
    {
        if (item == buildableGridObjectTypeSO &#x26;&#x26; item.enableBuildCondition)
        {
            //You can simply replace this if statement with your own conditions
<strong>            if (buildableGridObjectTypeSO.buildConditionSO.foodAmount &#x3C;= currentFoodInInventory &#x26;&#x26;
</strong><strong>                buildableGridObjectTypeSO.buildConditionSO.metalAmount &#x3C;= currentMetalInInventory &#x26;&#x26;
</strong><strong>                buildableGridObjectTypeSO.buildConditionSO.woodAmount &#x3C;= currentWoodInInventory)
</strong>            //And you can leave rest of the code from here as it is
            {
                MultiGridBuildConditionManager.BuidConditionResponseBuildableGridObject = true;
                return;
            }
            else
            {
                MultiGridBuildConditionManager.BuidConditionResponseBuildableGridObject = false;
                return;
            }
        }
    }
    MultiGridBuildConditionManager.BuidConditionResponseBuildableGridObject = false;
    return;
}
</code></pre>

This method is called when a **Buildable Grid Object** that has **Build Conditions** enabled is about to be built on the grid.

As you can see this method comes with a parameter **BuildableGridObjectTypeSO** and this is the **Buildable Grid Object** that is about to be built on the grid.

_When you are integrating Build Conditions into your system you need to add this function as it is._ And only change the highlighted part. It's a simple **if statement** with some conditions. You can replace these conditions with your own in your system. If this statement is true the object will get built. Else it will not build due to the Build Conditions not being met.

In these highlighted conditions you can see we are checking some values in the [**BuildConditionSO**](../scriptable-objects/build-conditions-so) assets. To understand this let's open the [**BuildConditionSO**](../scriptable-objects/build-conditions-so) script located in **EasyGridBuilder Pro > Scripts > Core Grid Scripts > Grid Scriptable Objects** and let's focus on the highlighted parts below.

<pre class="language-csharp"><code class="lang-csharp">using UnityEngine;

namespace SoulGames.EasyGridBuilderPro
{
    [CreateAssetMenu(menuName = "SoulGames/Easy Grid Builder Pro/BuildConditionSO", order = 200)] //Scriptable object asset path
    public class BuildConditionSO : ScriptableObject //This is a scriptable object class
    {
        [Tooltip("Should wood consumed after build is complete. If not only check weather wood amount is in inventory and do not consume.")] [TextArea(4,5)]
        public string tooltipContent;
        //You can modify this to use with your own materials and use conditions. This is just an example
        [Tooltip("Food amount should be in inventory to build.")]
<strong>        public int foodAmount;
</strong>        [Tooltip("Should food consumed after build is complete. If not only check weather food amount is in inventory and do not consume.")]
        public bool consumeFoodOnBuild;
        [Tooltip("Metal amount should be in inventory to build.")]
<strong>        public int metalAmount;
</strong>        [Tooltip("Should metal consumed after build is complete. If not only check weather metal amount is in inventory and do not consume.")]
        public bool consumeMetalOnBuild;
        [Tooltip("Wood amount should be in inventory to build.")]
<strong>        public int woodAmount;
</strong>        [Tooltip("Should wood consumed after build is complete. If not only check weather wood amount is in inventory and do not consume.")]
        public bool consumeWoodOnBuild;
    }
}
</code></pre>

In these highlighted parts you can see we have 3 variables for Food, Metal, and Wood. These are the values that we are checking in the **CheckBuildConditionBuildableGridObject()** method.

## 04. Second Event Method

Now let's check the second event method, **CompleteBuildConditionBuildableGridObject()** from **Line 118** to **Line 131.**

<pre class="language-csharp"><code class="lang-csharp">private void CompleteBuildConditionBuildableGridObject(BuildableGridObjectTypeSO buildableGridObjectTypeSO)
{
    foreach (BuildableGridObjectTypeSO item in MultiGridBuildConditionManager.BuildableGridObjectTypeSOList)
    {
        if (item == buildableGridObjectTypeSO &#x26;&#x26; item.enableBuildCondition)
        {
            //You can simply replace this if statement with your own conditions
<strong>            if (buildableGridObjectTypeSO.buildConditionSO.consumeFoodOnBuild) currentFoodInInventory = currentFoodInInventory - buildableGridObjectTypeSO.buildConditionSO.foodAmount;
</strong><strong>            if (buildableGridObjectTypeSO.buildConditionSO.consumeMetalOnBuild) currentMetalInInventory = currentMetalInInventory - buildableGridObjectTypeSO.buildConditionSO.metalAmount;
</strong><strong>            if (buildableGridObjectTypeSO.buildConditionSO.consumeWoodOnBuild) currentWoodInInventory = currentWoodInInventory - buildableGridObjectTypeSO.buildConditionSO.woodAmount;
</strong>            //And you can leave rest of the code from here as it is
        }
    }
}
</code></pre>

This method is called right after a **Buildable Grid Object** that has **Build Conditions** enabled, is built on the grid (which means this event is only getting called after the previous method was called and its conditions met true)

As you can see this method also comes with a parameter **BuildableGridObjectTypeSO** and this is the **Buildable Grid Object** that is built on the grid.

So when you are implementing **Build Conditions** you can use the previous **CheckBuildConditionBuildableGridObject()** method to check if the conditions are met, and this **CompleteBuildConditionBuildableGridObject()** method to do anything after that **Buildable Grid Object** is built on the grid.

_When you are integrating Build Conditions into your system you need to add this function as it is._ And only change the highlighted part. In this example, we are removing items from the inventory. (as a build cost)

In these highlighted conditions you can see we are checking some values in the [**BuildConditionSO**](../scriptable-objects/build-conditions-so) assets. Same as before to understand this let's open the [**BuildConditionSO**](../scriptable-objects/build-conditions-so) script again, located in **EasyGridBuilder Pro > Scripts > Core Grid Scripts > Grid Scriptable Objects** and let's focus on the highlighted parts below.

<pre class="language-csharp"><code class="lang-csharp">namespace SoulGames.EasyGridBuilderPro
{
    [CreateAssetMenu(menuName = "SoulGames/Easy Grid Builder Pro/BuildConditionSO", order = 200)] //Scriptable object asset path
    public class BuildConditionSO : ScriptableObject //This is a scriptable object class
    {
        [Tooltip("Should wood consumed after build is complete. If not only check weather wood amount is in inventory and do not consume.")] [TextArea(4,5)]
        public string tooltipContent;
        //You can modify this to use with your own materials and use conditions. This is just an example
        [Tooltip("Food amount should be in inventory to build.")]
        public int foodAmount;
        [Tooltip("Should food consumed after build is complete. If not only check weather food amount is in inventory and do not consume.")]
<strong>        public bool consumeFoodOnBuild;
</strong>        [Tooltip("Metal amount should be in inventory to build.")]
        public int metalAmount;
        [Tooltip("Should metal consumed after build is complete. If not only check weather metal amount is in inventory and do not consume.")]
<strong>        public bool consumeMetalOnBuild;
</strong>        [Tooltip("Wood amount should be in inventory to build.")]
        public int woodAmount;
        [Tooltip("Should wood consumed after build is complete. If not only check weather wood amount is in inventory and do not consume.")]
<strong>        public bool consumeWoodOnBuild;
</strong>    }
}
</code></pre>

In these highlighted parts you can see we have 3 Boolean variables for enable consuming Food, Metal, and Wood. These are the values that we are checking in the **CompleteBuildConditionBuildableGridObject()** method. And if one or more is set to true, the ones that are set to true will consume the amounts of those items.

## 05. Third Event Method

Now let's check the first event method, **CheckBuildConditionBuildableGridObject()** from **Line 136** to **Line 160.**

```csharp
private void CheckBuildConditionBuildableFreeObject(BuildableFreeObjectTypeSO buildableFreeObjectTypeSO)
{
    foreach (BuildableFreeObjectTypeSO item in MultiGridBuildConditionManager.BuildableFreeObjectTypeSOList)
    {
        if (item == buildableFreeObjectTypeSO && item.enableBuildCondition)
        {
            //You can simply replace this if statement with your own conditions
            if (buildableFreeObjectTypeSO.buildConditionSO.foodAmount <= currentFoodInInventory &&
                buildableFreeObjectTypeSO.buildConditionSO.metalAmount <= currentMetalInInventory &&
                buildableFreeObjectTypeSO.buildConditionSO.woodAmount <= currentWoodInInventory)
            //And you can leave rest of the code from here as it is
            {
            MultiGridBuildConditionManager.BuidConditionResponseBuildableFreeObject = true;
                return;
            }
            else
            {
                MultiGridBuildConditionManager.BuidConditionResponseBuildableFreeObject = false;
                return;
            }
        }
    }
    MultiGridBuildConditionManager.BuidConditionResponseBuildableFreeObject = false;
    return;
}
```

This method is very much identical to the _earlier_ **CheckBuildConditionBuildableGridObject()** method except this method is for Buildable Free Objects. As you can see this method comes with a parameter **BuildableFreeObjectTypeSO** and this is the **Buildable Free Object** that is about to be built on the grid.

This method is called when a **Buildable Free Object** that has **Build Conditions** enabled is about to be built on the grid.

_When you are integrating Build Conditions into your system you need to add this function as it is._ And only change the highlighted part. Same as the first method.

_**NOTE:** Since everything else is identical to the earlier_ **CheckBuildConditionBuildableGridObject()** method _we are not gonna repeat it in the guide._

## 06. Fourth Event Method

Now let's check the second event method, **CompleteBuildConditionBuildableFreeObject()** from **Line 164** to **Line 177.**

```csharp
private void CompleteBuildConditionBuildableFreeObject(BuildableFreeObjectTypeSO buildableFreeObjectTypeSO)
{
    foreach (BuildableFreeObjectTypeSO item in MultiGridBuildConditionManager.BuildableFreeObjectTypeSOList)
    {
        //You can simply replace this if statement with your own conditions
        if (item == buildableFreeObjectTypeSO && item.enableBuildCondition)
        {
            if (buildableFreeObjectTypeSO.buildConditionSO.consumeFoodOnBuild) currentFoodInInventory = currentFoodInInventory - buildableFreeObjectTypeSO.buildConditionSO.foodAmount;
            if (buildableFreeObjectTypeSO.buildConditionSO.consumeMetalOnBuild) currentMetalInInventory = currentMetalInInventory - buildableFreeObjectTypeSO.buildConditionSO.metalAmount;
            if (buildableFreeObjectTypeSO.buildConditionSO.consumeWoodOnBuild) currentWoodInInventory = currentWoodInInventory - buildableFreeObjectTypeSO.buildConditionSO.woodAmount;
        }
        //And you can leave rest of the code from here as it is
    }
}
```

This method is very much identical to the _earlier_ **CompleteBuildConditionBuildableGridObject()** method except this method is for Buildable Free Objects. As you can see this method also comes with a parameter **BuildableFreeObjectTypeSO** and this is the **Buildable Free Object** that is built on the grid.

This method is called when a **Buildable Free Object** that has **Build Conditions** enabled is built on the grid (which means this event is only getting called after the previous method was called and its conditions met true)

So when you are implementing **Build Conditions** you can use the previous **CheckBuildConditionBuildableFreeObject()** method to check if the conditions are met, and this **CompleteBuildConditionBuildableFreeObject()** method to do anything after that **Buildable Free Object** is built on the grid.

_When you are integrating Build Conditions into your system you need to add this function as it is._ And only change the highlighted part. In this example, we are removing items from the inventory. (as a build cost)

_**NOTE:** Since everything else is identical to the earlier_ **CompleteBuildConditionBuildableGridObject()**  method _we are not gonna repeat it in the guide._

## 06. Summery

In above integration methods,

```csharp
<param name="buildableGridObjectTypeSO">Holds the Buildable Grid Object Type Scriptable Object</param>
<param name="buildableFreeObjectTypeSO">Holds the Buildable Free Object Type Scriptable Object</param>
<param name="buildConditionSO">Holds the Build Condition Scriptable Object That attached to one of above Scriptable Objects</param>
<param name="foodAmount, metalAmount, woodAmount, consumeFoodOnBuild, currentMetalInInventory, currentWoodInInventory">These data from Build Condition Scriptable Object</param>
```

## 06. Build Conditions SO Asset

Let's have a quick look at the [**Build Condition SO**](../scriptable-objects/build-conditions-so) scriptable object. This is the object that goes to the **Build Condition** property in **Buildable Grid / Free Object Type SO** assets. Since we covered this in previous sections we are only gonna summarize it.

After setting up your **Custom Build Conditions** integration open the **BuildConditionSO** script again, located in **EasyGridBuilder Pro > Scripts > Core Grid Scripts > Grid Scriptable Objects** then add variables to fit your custom conditions. And save it.&#x20;

_**NOTE:**_ _**Line 9** in **BuildConditionSO** script there is a string variable **toolTipContent**. Do not change it and leave it as it is. After you create **Build Conditions SO** assets you can use this property to add tooltips to your buildable objects. (Require **Easy Grid Builder Pro UI** set up)_

Now you can create your own [**Build Condition SO**](../scriptable-objects/build-conditions-so) assets and add them to **Buildable Grid Object Type SO** and **Buildable Free Object Type SO** assets to add those build conditions.&#x20;
