Skills

Bevy Economy System

Bevy ECS economy: Wallet components, a PriceTable resource, Purchase and Transfer...

Install

npx skills add thedivergentai/Game-Dev-Agentic-Skills --skill bevy-economy-system -g -a claude-code -y
Category
Engineering
Updated
19d ago
License
LGPL-3.0
Context cost
Mediumabout 2,022 tokens
Runs scripts
No, instructions only

What it does

Bevy ECS economy: Wallet components, a PriceTable resource, Purchase and Transfer messages, vendor stock. Use when a debit must see the price row and two wallets must not be written by unordered systems. Keywords: Bevy, economy, wallet, price, Purchase, get_many_mut.

Bevy economy system

When to use

Load this when wallets, prices, or vendors change numbers. Item identity stays on bevy-inventory-system.

ECS shape

  • Components: wallet amounts you define.
  • Resources: price rows live in one resource (bevy-data-resources).
  • Systems: mutate wallets on Update. Use .chain() when stock must drop before the wallet debit.
  • Messages: a purchase or transfer is a message. This file names Purchase and Transfer.

Idioms

NeedBevy shape
WalletA component of currency amounts you define.
Price rowA resource table.
TransactionA message. One system writes both wallets with get_many_mut.

Scope

  • Coins live on a wallet component. Prices live in one resource. A purchase message is the only writer of the buyer wallet.
  • Persistence of the wallet belongs on bevy-save-load.

Examples

[package]
name = "economy"
version = "0.1.0"
edition = "2021"

[dependencies]
bevy = "0.20"

Price lookup and the wallet write share one chain so the debit sees the row that was chosen.

#[derive(Component)]
struct Wallet {
    coins: u32,
}

#[derive(Clone, Copy)]
struct PriceRow {
    item: u32,
    cost: u32,
}

#[derive(Resource)]
struct PriceTable {
    rows: Vec<PriceRow>,
}

#[derive(Message)]
struct Purchase {
    buyer: Entity,
    item: u32,
}

fn buy(
    mut purchases: MessageReader<Purchase>,
    table: Res<PriceTable>,
    mut wallets: Query<&mut Wallet>,
) {
    for purchase in purchases.read() {
        let Some(row) = table.rows.iter().find(|row| row.item == purchase.item) else {
            continue;
        };
        let Ok(mut wallet) = wallets.get_mut(purchase.buyer) else {
            continue;
        };
        if wallet.coins < row.cost {
            continue;
        }
        wallet.coins -= row.cost;
    }
}

pub struct EconomyPlugin;
Read the whole SKILL.md on GitHub

More in Engineering