spacetime_tiled
Load Tiled maps into SpacetimeDB for multiplayer games.
This library parses TMX files and stores them in SpacetimeDB tables. Since SpacetimeDB modules run in WASM without filesystem access, it includes an in-memory XML parser that works with embedded map data or content sent from clients.
Quick Start
Add to your server module's Cargo.toml:
[]
= "1.4.0"
= "0.1"
[]
= ["cdylib"]
In your server code:
use ;
pub use *;
Then call the reducer after publishing:
Why This Exists
SpacetimeDB modules run in WASM sandboxes with no filesystem access. You can't just std::fs::read_to_string("map.tmx") in a reducer - it'll fail with "operation not supported on this platform".
This library provides two solutions:
load_tmx_map_from_str()- Parses TMX XML in-memory usingquick-xml. Works in WASM. Use this.load_tmx_map()- Uses thetiledcrate's file loader. Doesn't work in WASM. Only useful for testing outside SpacetimeDB.
What Gets Stored
The library defines six tables:
- tiled_map - Map dimensions, tile size, orientation
- tiled_layer - Layer names, types, visibility, opacity
- tiled_tile - Individual tiles with position, GID, and flip flags
- tiled_tileset - Tileset metadata (names, dimensions, tile counts)
- tiled_object - Objects from object layers (positions, sizes, shapes)
- tiled_property - Custom properties on any element
All tables are indexed for querying by map_id or layer_id.
Usage Patterns
Server-Side Map Loading
Option 1: Embedded maps (recommended)
Option 2: Client-uploaded maps
Then from your client: spacetime call my-game upload_map '{"name": "custom", "tmx": "<?xml version..."}'
Querying Map Data
use ;
Client Setup
The Rust client SDK requires an event loop to process messages. Without it, subscriptions never apply and you won't get any data.
use ;
use *;
Important: You must import the table accessor traits:
use ;
Examples
Check out examples/simple_game/ for a complete working example with:
- Server module that loads an embedded demo map
- Interactive Rust client that queries map data
- Sample TMX file with layers, tiles, and objects
To run it:
Common Issues
"No maps loaded" in client
You forgot to call conn.run_threaded(). The SDK doesn't process messages automatically - you have to start an event loop.
"operation not supported on this platform"
You're trying to use load_tmx_map() in a WASM module. Use load_tmx_map_from_str() instead with include_str!().
"unresolved import" or "method not found" errors in client
Missing trait imports. The generated bindings define traits like TiledMapTableAccess that you need to import to use .tiled_map() methods.
use TiledMapTableAccess;
Build fails with "clang: program not found"
The zstd-sys dependency needs LLVM/clang for WASM compilation. Install LLVM and add it to your PATH, or use WSL/Linux.
Supported Tiled Features
- ✅ Orthogonal, isometric, staggered, and hexagonal maps
- ✅ Tile layers (finite and infinite)
- ✅ Object layers with rectangles, ellipses, and points
- ✅ Custom properties (string, int, float, bool, color, file)
- ✅ Tile flipping (horizontal, vertical, diagonal)
- ✅ Multiple tilesets per map
- ✅ CSV tile data encoding
- ❌ Base64/gzip tile encoding (not yet)
- ❌ Polygon/polyline vertices (shape type only)
- ❌ Tile animations
- ❌ Wang sets
License
Licensed under either of:
- Apache License, Version 2.0 (LICENSE-APACHE)
- MIT license (LICENSE-MIT)
at your option.
Contributing
Contributions are welcome! See CONTRIBUTING.md for guidelines.
Areas that need help:
- Base64/gzip tile encoding support
- Polygon/polyline vertex storage
- Tile animation support
- More examples