Custom Build Conditions
Last updated
Last updated
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!
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.
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.
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.
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.
Now let's check the first event method, CheckBuildConditionBuildableGridObject() from Line 90 to Line 114.
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.
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.
Now let's check the second event method, CompleteBuildConditionBuildableGridObject() from Line 118 to Line 131.
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.
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.
Now let's check the first event method, CheckBuildConditionBuildableGridObject() from Line 136 to Line 160.
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.
Now let's check the second event method, CompleteBuildConditionBuildableFreeObject() from Line 164 to Line 177.
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.
In above integration methods,
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.