Skip to main content

Value Binders

Wrappers that hold a bound value in code (no MonoBehaviour).


Overview

Value binders are non-MonoBehaviour classes for reading ViewModel values from code. Useful when a ViewModel value is needed programmatically, without UI.


Types

ClassModeDescription
ValueOneWayBinder<T>OneWay / OneTimeHolds the value, Changed event
ValueTwoWayBinder<T>TwoWayTwo-way, can be changed from code
ValueOneTimeBinder<T>OneTimeRead-only after the first set
ValueOneWayToSourceBinder<T>OneWayToSourcePush from code into the ViewModel

ValueOneWayBinder<T>

var healthValue = new ValueOneWayBinder<int>();

// Bind to the ViewModel
view.BindCustomBinder("Health", healthValue);

// Read the value
int current = healthValue.Value;

// Subscribe to changes
healthValue.Changed += newValue =>
{
Debug.Log($"Health changed: {newValue}");
};

// Implicit conversion
int hp = healthValue; // implicit cast to T?

ValueTwoWayBinder<T>

var nameValue = new ValueTwoWayBinder<string>();

// Bind...

// Read
string name = nameValue.Value;

// Write: notifies the ViewModel
nameValue.Value = "New Name";

Writing Value raises ValueChanged, which passes the change back to the ViewModel.


Example: use in a custom component

public class CustomComponent : MonoBehaviour
{
private ValueOneWayBinder<bool> _isActive = new();

public void Bind(IViewModel viewModel)
{
var result = viewModel.FindBindableMember(
new FindBindableMemberParameters("IsActive"));

if (result.IsFound)
_isActive.Bind(result.Adder);
}

private void Update()
{
// Use the value from the ViewModel
if (_isActive.Value)
DoSomething();
}
}

See also