ALL TEMPLATES

Spacetimedb Bevy Template

Community
Built With
Tags
spacetime init --template spacetimedb-bevy-template

Get a SpacetimeDB Bevy app running in under 5 minutes.

Prerequisites


Create your project

Run the spacetime dev command to create a new project with a SpacetimeDB module and Bevy client.

spacetime dev --template bevy-rust 

Run your app

Once spacetime dev has published the module and generated bindings, you can run the client:

cargo run -p client 

Explore the project structure

Your project contains both server and client code.

Edit crates/server/src/lib.rs to add tables and reducers. Edit crates/client/src/main.rs to build your game logic.

my-bevy-app/
├── crates/
│   ├── server/            # Your SpacetimeDB module
│   │   └── src/
│   │       └── lib.rs     # Server-side logic
│   └── client/            # Bevy game client
│       └── src/
│           ├── main.rs    # Game logic and systems
│           └── stdb.rs    # SpacetimeDB connection setup
└── Cargo.toml 

Understand tables and reducers

Open crates/server/src/lib.rs to see the module code. The template includes a Player table and a move_player reducer.

Tables store your data. Reducers are functions that modify data — they're the only way to write to the database.

#[spacetimedb::table(accessor = player, public)]
pub struct Player {
    #[primary_key]
    pub identity: Identity,
    pub x: f32,
    pub y: f32,
}

#[reducer]
pub fn move_player(ctx: &ReducerContext, x: f32, y: f32) {
    if let Some(mut player) = ctx.db.player().identity().find(ctx.sender()) {
        player.x = x;
        player.y = y;
        ctx.db.player().identity().update(player);
    }
} 

Test with the CLI

Open a new terminal and navigate to your project directory. Then use the SpacetimeDB CLI to call reducers and query your data directly.

# Call the move_player reducer
spacetime call move_player 100.0 200.0

# Query the player table
spacetime sql "SELECT * FROM player" 

Next steps