> For the complete documentation index, see [llms.txt](https://postica.gitbook.io/binding-system/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://postica.gitbook.io/binding-system/product/bind-class.md).

# Bind Class

The central piece of the BindingSystem is the <mark style="color:orange;">`Bind<T>`</mark> class. This class wraps the `T` type with the binding mechanism and provides the necessary functionality for serializing binding information. Additionally, it includes the `BindData` structure for storing the binding information. The <mark style="color:orange;">`Bind<T>`</mark> class allows for binding paths with different depths. For example, a float field can be bound to the path `"position.x"` of a Transform so that at runtime, it will have the value of x component of transform's position.&#x20;

The <mark style="color:orange;">`Bind<T>`</mark> has an internal data pipeline when reading the data from the bound source or writing it to  the source:

<figure><picture><source srcset="/files/P4y9jpoR0RJsTpYHWR2Q" media="(prefers-color-scheme: dark)"><img src="/files/wYNToLiY7Y5LZAtjFH7s" alt="" width="352"></picture><figcaption><p>Default Bind Pipeline</p></figcaption></figure>

There are several variations of the <mark style="color:orange;">`Bind<T>`</mark> class available:

* <mark style="color:orange;">`ReadOnlyBind<T>`</mark>: Allows read-only access to the bound value.
* <mark style="color:orange;">`WriteOnlyBind<T>`</mark>: Allows write-only access to the bound value.
* <mark style="color:orange;">`BindDataFor<T>`</mark>: Doesn't have a direct value field, must only bind to other sources.

These classes can be implicitly converted to the inner `T` type when needed.

To define a new <mark style="color:orange;">`Bind<T>`</mark> simply add a field or property as follows:

```csharp
class MyMonoBehaviour : MonoBehaviour
{
    public Bind<float> myBoundFloat;
    public Bind<float> myBoundAndInitializedFloat = 3.14f.Bind();
}
```

The above code will let users bind the properties directly in Unity's Inspector.

{% tabs %}
{% tab title="Minimal UI" %}

<figure><img src="/files/UJuXWgja5ggEDM1lvfbU" alt="" width="563"><figcaption><p>Inspector View of MyMonoBehaviour</p></figcaption></figure>
{% endtab %}

{% tab title="Verbose UI" %}

<figure><img src="/files/oZ1O8Il9vGzmmsueDvq4" alt=""><figcaption><p>Inspector View of MyMonoBehaviour</p></figcaption></figure>
{% endtab %}
{% endtabs %}

The power of <mark style="color:orange;">`Bind<T>`</mark> type is in its drawer, which allows for a quick and easy binding process with an intuitive UI.

#### Binding with code

To create a new bound value in code simply call the constructor with specified target and path:

```csharp
    void Start()
    {
        Object target = GameObject.Find("Sphere").transform;
        myBoundFloat = new Bind<float>(target, "position.x");
    }
```

When accessing `Bind<T>`'s value, simply use the `.Value` property or, in case of reading the value, ommit it altogether:

```csharp
    void Update()
    {
        float value = myBoundFloat; // Here the implicit conversion is applied
        value *= 2;
        myBoundAndInitializedFloat.Value = value;
    }
```

The `Bind<T>` class and its variations (except `WriteOnlyBind<T>`) can notify users when its value changes through its `ValueChanged` event:

```csharp
    void Start()
    {
        myBoundFloat.ValueChanged += (oldValue, newValue) => Debug.Log("New Value is: " + newValue);
    }
```

*Please Note*: `ValueChanged` is not called immediately when the underlying value is changed, due to security limitations (*an immediate version is available in experimental versions, to be released on demand*). All these events are processed at multiple points within the same frame.

For more expert users, the inner `BindData` structure can be used to create custom `Bind` classes. `BindData` stores all required information to access a bound value, as well as parameters for converters and modifiers.
