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.
[BridgeAction]
public void StartGame()
{
CurrentScreen = GameScreen.Hud;
}
await bridge.startGame();
Parameters
Actions can take any number of parameters:
[BridgeAction]
public void SetVolume(float value)
{
Volume = value;
}
[BridgeAction]
public void MovePlayer(int x, int y, float speed)
{
// ...
}
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:
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:
[BridgeAction]
public int GetCoins()
{
return Coins;
}
The UI receives a promise:
const coins = await bridge.getCoins();
Async work
Use async actions when the game operation is asynchronous:
[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:
public sealed class BootState
{
public float Progress { get; set; }
[BridgeAction]
public void SetProgress(float value) => Progress = value;
}
await bridge.boot.setProgress(0.5);
Errors
If an action throws, the UI promise rejects. Handle expected failures in the UI:
try {
await bridge.buyItem({ sku, quantity: 1 });
} catch (error) {
setError(String(error));
}