# 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="https://2360058278-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3rw9jjjjlPycieM3U3MU%2Fuploads%2FlXoNdttIfwNshEm8d2sc%2FBS_Bind_Pipeline_Dark.png?alt=media&#x26;token=a5f4268f-2923-45d2-bea7-cf04a306e15c" media="(prefers-color-scheme: dark)"><img src="https://2360058278-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3rw9jjjjlPycieM3U3MU%2Fuploads%2FaBi6Brk0z9oGtIFtimxj%2FBS_Bind_Pipeline.png?alt=media&#x26;token=8c4d935b-738e-47f3-85ed-c403bbda82e0" 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="https://2360058278-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3rw9jjjjlPycieM3U3MU%2Fuploads%2FrOkXbcWyUCFJnQdkXr5t%2FMinimalUI_MyMonoBehaviour.png?alt=media&#x26;token=483ddee2-f8fc-4242-877d-f36a9fd07b6b" alt="" width="563"><figcaption><p>Inspector View of MyMonoBehaviour</p></figcaption></figure>
{% endtab %}

{% tab title="Verbose UI" %}

<figure><img src="https://2360058278-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3rw9jjjjlPycieM3U3MU%2Fuploads%2Fpsft7g24haLWuQDwaFuO%2FMyMonoBehaviour_Bind.png?alt=media&#x26;token=b8dd6fc2-ba79-4102-8e86-6398a018a148" 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.


---

# Agent Instructions: 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:

```
GET https://postica.gitbook.io/binding-system/product/bind-class.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
