Loading...
Loading...
Create Dojo models for storing game state with proper key definitions, trait derivations, and ECS patterns. Use when defining game entities, components, or state structures.
npx skill4agent add dojoengine/book dojo-model#[dojo::model]DropSerde#[key]"Add a model for player positions""Create a Position model with player as key and x, y coordinates"#[dojo::model]#[key]#[derive(Drop, Serde)]
#[dojo::model]
struct Moves {
#[key]
player: ContractAddress,
remaining: u8,
}DropSerdeCopy#[derive(Drop, Serde)]
#[dojo::model]
struct Position {
#[key]
player: ContractAddress,
vec: Vec2,
}
#[derive(Drop, Copy, Serde, Introspect)]
struct Vec2 {
x: u32,
y: u32,
}Introspect#[derive(Copy, Drop, Serde)]
#[dojo::model]
struct GameResource {
#[key]
player: ContractAddress,
#[key]
location: ContractAddress,
balance: u8,
}let resource: GameResource = world.read_model((player, location));const RESPAWN_DELAY: u128 = 9999999999999;
#[derive(Copy, Drop, Serde)]
#[dojo::model]
struct GameSetting {
#[key]
setting_id: u128,
setting_value: felt252,
}
// Usage
world.write_model(@GameSetting {
setting_id: RESPAWN_DELAY,
setting_value: (10 * 60).into()
});#[derive(Copy, Drop, Serde)]
#[dojo::model]
struct Position {
#[key]
id: u32,
x: u32,
y: u32,
}
#[derive(Copy, Drop, Serde)]
#[dojo::model]
struct Health {
#[key]
id: u32,
health: u8,
}
// Human has Position + Health + Potions
// Orc has Position + Health (no Potions)#[key]use dojo::model::{ModelStorage, ModelValueStorage};
let mut world = self.world(@"my_namespace");world.write_model(@Position { player, vec: Vec2 { x: 0, y: 0 } });let position: Position = world.read_model(player);let resource: GameResource = world.read_model((player, location));let entity_id = world.uuid();
world.write_model(@Health { id: entity_id, health: 100 });u8u16u32u64u128u256felt252boolContractAddressIntrospectIntrospectdojo-systemdojo-testdojo-config