> For the complete documentation index, see [llms.txt](https://mindcodeinteractive.gitbook.io/easy-build-system/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://mindcodeinteractive.gitbook.io/easy-build-system/reference/building-area/building-rule.md).

# Building Rule

Validates whether a specific building action is allowed inside a [Building Area](/easy-build-system/reference/building-area.md).\
Each rule returns a pass or fail result based on its own criteria.

***

### How Rules Work

Rules are modular restrictions attached to a [Building Area](/easy-build-system/reference/building-area.md).\
When an action occurs inside an area, rules are evaluated in order.\
The first failure blocks the action with a descriptive error message.\
This lets you combine different restrictions on the same area.

Each rule receives the [Building Part](/easy-build-system/reference/building-part.md) and the active [Building Mode](/easy-build-system/reference/building-controller.md#building-modes).\
It returns a ConditionResult with a valid/invalid flag and an optional reason string.

***

### Built-in Rules

#### Height Restriction Rule

Limits building actions to a defined height range on the Y axis.\
Configure independent height ranges for placement, destruction, and adjustment.\
If the part's Y position is outside the range for the current mode, the rule fails.

Useful for limiting how high or low players can build within an area.

#### Category Restriction Rule

Limits building actions to parts whose category matches an allowed list.\
Configure independent allowed category lists for placement, destruction, and adjustment.\
If the part's category is not in the list for the current mode, the rule fails.

Useful for restricts which parts can be placed in specific zones (e.g., "Walls" and "Floors").

***

### Adding Rules to an Area

1. Select a [Building Area](/easy-build-system/reference/building-area.md) in your scene.
2. In the inspector, find the [Building Rules](/easy-build-system/reference/building-area/building-rule.md) list.
3. Click **Add Rule** and choose the rule type.
4. Configure the rule's parameters.

Rules are components that live on the same GameObject as the [Building Area](/easy-build-system/reference/building-area.md).\
You can add multiple rules to the same area.

***

### Creating Custom Rules

Inherit from `BuildingRule` and implement the `Validate` method:

```csharp
using MindCodeInteractive.EasyBuildSystem.Framework.Code.Runtime.Systems.Rules.Abstracts;
using MindCodeInteractive.EasyBuildSystem.Framework.Code.Runtime.Systems.Rules.Attributes;
using MindCodeInteractive.EasyBuildSystem.Framework.Code.Runtime.Systems.Parts;
using MindCodeInteractive.EasyBuildSystem.Framework.Code.Runtime.Systems.Parts.Implementations.Conditions.Abstracts;
using MindCodeInteractive.EasyBuildSystem.Framework.Code.Runtime.Systems.Controllers.States;

[BuildingRule("Max Parts Rule", "Limits the number of parts allowed in this area.")]
public class MaxPartsBuildingRule : BuildingRule
{
    [SerializeField] private int m_maxParts = 50;

    public override ConditionResult Validate(BuildingPart part, BuildingMode mode)
    {
        if (mode != BuildingMode.Placement)
            return new ConditionResult(true);

        BuildingArea area = GetComponent<BuildingArea>();
        if (area.RegisteredParts.Count >= m_maxParts)
            return new ConditionResult(false, "Area has reached the maximum of " + m_maxParts + " parts.");

        return new ConditionResult(true);
    }
}
```

The `BuildingRule` attribute defines the name and description shown in the inspector.\
Your rule will automatically appear in the Add Rule dropdown.

***

### Tips

Put cheap checks (category filtering) first and expensive checks (spatial queries) last.\
Rules are evaluated in order, so early failures skip the rest.

Use the `Enabled` toggle on each rule to quickly test with or without specific restrictions.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://mindcodeinteractive.gitbook.io/easy-build-system/reference/building-area/building-rule.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
