Guides Actions
Guides

Actions

Call C# methods from the UI, pass arguments, and handle results or errors.

Add [BridgeAction] to a public C# method to make it callable from the UI. Public methods without the attribute are not exposed to the UI. Every generated TypeScript action returns a promise, including actions backed by synchronous C# methods.

C#
[BridgeAction]
public void StartGame()
{
    CurrentScreen = GameScreen.Hud;
}
TSX
await bridge.startGame();

Parameters

Actions can take any number of parameters:

C#
[BridgeAction]
public void SetVolume(float value)
{
    Volume = value;
}

[BridgeAction]
public void MovePlayer(int x, int y, float speed)
{
    // ...
}
TSX
await bridge.setVolume(0.8);
await bridge.movePlayer(3, 4, 1.5);

Group related arguments in a DTO when you expect to add fields later:

C#
public sealed class BuyItemRequest
{
    public string Sku { get; set; } = "";
    public int Quantity { get; set; }
}

[BridgeAction]
public PurchaseResult BuyItem(BuyItemRequest request)
{
    // ...
}

Return values

Actions may return a value:

C#
[BridgeAction]
public int GetCoins()
{
    return Coins;
}

The UI receives a promise:

TSX
const coins = await bridge.getCoins();

Async work

Use async actions when the game operation is asynchronous:

C#
[BridgeAction]
public async Task<Profile> LoadProfile()
{
    return await ProfileService.LoadAsync();
}

For operations such as loading a profile, expose loading progress and errors as bridge state so the UI can show them while it waits.

Nested actions

A method on a nested state DTO can also use [BridgeAction]. Call it through the corresponding UI object:

C#
public sealed class BootState
{
    public float Progress { get; set; }

    [BridgeAction]
    public void SetProgress(float value) => Progress = value;
}
TSX
await bridge.boot.setProgress(0.5);

Errors

If an action throws, the UI promise rejects. Handle expected failures in the UI:

TSX
try {
  await bridge.buyItem({ sku, quantity: 1 });
} catch (error) {
  setError(String(error));
}
Loom documentation

Search docs

Esc

Type a word or phrase to search the documentation.

Type a word or phrase to search the documentation.