> For the complete documentation index, see [llms.txt](https://docs.allout.game/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.allout.game/data-and-persistence/economy.md).

# Economy

* Each currency is stored **per-player**
* Balances are **automatically persisted** across sessions
* Balances can be viewed/edited in the creator portal (see [Editing/Viewing Player Data](/data-and-persistence/editing-viewing-player-data.md))

### Economy API reference

```go
Economy :: struct {
    register_currency     :: proc(currency: string, icon: Texture_Asset);
    deposit_currency      :: proc(player: Player, currency: string, amount: s64);
    get_balance           :: proc(player: Player, currency: string) -> s64;
    can_withdraw_currency :: proc(player: Player, currency: string, amount: s64) -> bool;
    withdraw_currency     :: proc(player: Player, currency: string, amount: s64);
    delete_save_data      :: proc(player: Player);
}
```

### Registering a currency (one time)

Before you use a currency name, register it with an icon.

```go
// Pick an icon from your /res folder
coin_icon := get_asset(Texture_Asset, "ui/coin.png");

Economy.register_currency("Coins", coin_icon);
Economy.register_currency("XP", coin_icon); // example (use a different icon ideally)
```

{% hint style="info" %}
Currency names are just strings. Pick a consistent name and stick to it (for example `"Coins"` vs `"coins"`).
{% endhint %}

### Reading a player's balance

```go
coins := Economy.get_balance(player, "Coins");
```

### Giving currency (rewards)

Use `deposit_currency` whenever a player earns currency.

```go
on_enemy_killed :: proc(player: Player) {
    Economy.deposit_currency(player, "Coins", 10);
    Economy.deposit_currency(player, "XP", 3);
}
```

### Spending currency (shops/upgrades)

Always check `can_withdraw_currency` before withdrawing.

```go
UPGRADE_COST :: 50;

try_buy_upgrade :: proc(player: Player) -> bool {
    if !Economy.can_withdraw_currency(player, "Coins", UPGRADE_COST) {
        Notifier.notify(player, "Not enough coins!");
        return false;
    }

    Economy.withdraw_currency(player, "Coins", UPGRADE_COST);

    // Grant the upgrade here...
    Notifier.notify(player, "Upgrade purchased!");
    return true;
}
```

### Resetting a player's economy data

If you need to wipe all economy balances for a player (for example, an admin reset button or a game mode reset), you can delete their economy save data:

```go
reset_economy :: proc(player: Player) {
    Economy.delete_save_data(player);
    Notifier.notify(player, "Your economy data was reset.");
}
```

{% hint style="warning" %}
`Economy.delete_save_data` is destructive. Use it sparingly, and consider adding confirmations/admin-only access.
{% endhint %}

### Economy vs Save

* Use **Economy** for "currencies" (coins, gems, XP, tickets) where you want auto-persistence + portal editing.
* Use **Save** for everything else (settings, quest state, unlock lists, complex progress structures). See [Save System](/data-and-persistence/save.md)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

```
GET https://docs.allout.game/data-and-persistence/economy.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
