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.

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!

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 object to fit with your system and conditions.

After that you can create your own Build Conditions SO assets with the data to your likings (You can find example BuildConditionSO assets located in EasyGridBuilder Pro > Scriptable Objects > 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

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.

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 component.

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.

private void OnDisable()
{
   MultiGridBuildConditionManager.OnBuildConditionCheckBuildableGridObject -= CheckBuildConditionBuildableGridObject;
   MultiGridBuildConditionManager.OnBuildConditionCompleteBuildableGridObject -= CompleteBuildConditionBuildableGridObject;
   MultiGridBuildConditionManager.OnBuildConditionCheckBuildableFreeObject -= CheckBuildConditionBuildableFreeObject;
   MultiGridBuildConditionManager.OnBuildConditionCompleteBuildableFreeObject -= CompleteBuildConditionBuildableFreeObject;
}

03. First Event Method

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

private void CheckBuildConditionBuildableGridObject(BuildableGridObjectTypeSO buildableGridObjectTypeSO)
{
    foreach (BuildableGridObjectTypeSO item in MultiGridBuildConditionManager.BuildableGridObjectTypeSOList)
    {
        if (item == buildableGridObjectTypeSO && item.enableBuildCondition)
        {
            //You can simply replace this if statement with your own conditions
            if (buildableGridObjectTypeSO.buildConditionSO.foodAmount <= currentFoodInInventory &&
                buildableGridObjectTypeSO.buildConditionSO.metalAmount <= currentMetalInInventory &&
                buildableGridObjectTypeSO.buildConditionSO.woodAmount <= currentWoodInInventory)
            //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;
}

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 assets. To understand this let's open the BuildConditionSO script located in EasyGridBuilder Pro > Scripts > Core Grid Scripts > Grid Scriptable Objects and let's focus on the highlighted parts below.

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.")]
        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.")]
        public bool consumeFoodOnBuild;
        [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.")]
        public bool consumeMetalOnBuild;
        [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.")]
        public bool consumeWoodOnBuild;
    }
}

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.

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

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 assets. Same as before to understand this let's open the BuildConditionSO script again, located in EasyGridBuilder Pro > Scripts > Core Grid Scripts > Grid Scriptable Objects and let's focus on the highlighted parts below.

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.")]
        public bool consumeFoodOnBuild;
        [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.")]
        public bool consumeMetalOnBuild;
        [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.")]
        public bool consumeWoodOnBuild;
    }
}

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.

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.

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,

<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 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.

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 assets and add them to Buildable Grid Object Type SO and Buildable Free Object Type SO assets to add those build conditions.

Last updated