Skip to content

Conversation

@trethaller
Copy link
Contributor

@trethaller trethaller commented Jan 6, 2026

The checked mode is useful when you want to check on the client whether an RPC will succeed before executing it. It generates two additional functions:

  • check{Name}(...) : Runs the RPC logic without executing @:do blocks, returning the result
  • can{Name}(...) : Like check, but returns a Bool indicating success

Use @:do { } blocks to mark code that should only run when the RPC succeeds:

@:rpc(checked)
function buyItem(itemId:Int) : PurchaseResult {
    var item = inventory.get(itemId);
    if (item == null) return NotFound;
    if (player.gold < item.price) return NotEnoughGold;
    
    @:do {
        // This only executes when called via buyItem(), not checkBuyItem()
        player.gold -= item.price;
        player.addItem(item);
    }
    return Success;
}

// Usage:
if (canBuyItem(itemId)) {
    buyItem(itemId);
}

// Or check the specific result:
var result = checkBuyItem(itemId);
if (result != Success)
    showError(result);

The can{Name} function determines success by:

  • If return type is Bool, returns the value directly
  • If return type is an enum with @:rpcSuccess(Value), returns result == Value
  • Otherwise, returns result != null (result considered an error code)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant