<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Sandeep Chauhan</title>
    <description>The latest articles on DEV Community by Sandeep Chauhan (@sandeep_chauhan_f5476f312).</description>
    <link>https://dev.to/sandeep_chauhan_f5476f312</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2658869%2F637afeaa-d432-4dc8-ba27-cbf06d6d0f72.gif</url>
      <title>DEV Community: Sandeep Chauhan</title>
      <link>https://dev.to/sandeep_chauhan_f5476f312</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sandeep_chauhan_f5476f312"/>
    <language>en</language>
    <item>
      <title>Deploy using Slot</title>
      <dc:creator>Sandeep Chauhan</dc:creator>
      <pubDate>Thu, 04 Sep 2025 02:35:08 +0000</pubDate>
      <link>https://dev.to/sandeep_chauhan_f5476f312/deploy-using-slot-3gjf</link>
      <guid>https://dev.to/sandeep_chauhan_f5476f312/deploy-using-slot-3gjf</guid>
      <description>&lt;p&gt;Slot is a managed service that provides hosted Katana instances and Torii indexers for Dojo games. This guide covers the complete deployment workflow from authentication to production deployment.&lt;/p&gt;

&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;p&gt;Ensure you have the latest Dojo version installed:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;dojoup
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Authentication Setup
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Initial Login
&lt;/h3&gt;

&lt;p&gt;First, authenticate with the Slot service:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;slot auth login
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will open your browser for authentication. Follow the prompts to complete the login process.&lt;/p&gt;

&lt;h3&gt;
  
  
  Troubleshooting Authentication
&lt;/h3&gt;

&lt;p&gt;If you encounter issues with old credentials, clear them and try again:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;rm&lt;/span&gt; ~/Library/Application&lt;span class="se"&gt;\ &lt;/span&gt;Support/slot/credentials.json
slot auth login
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Backend Deployment (Katana)
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Initialize Your Project
&lt;/h3&gt;

&lt;p&gt;Start by creating a new Dojo project or navigate to your existing project:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;sozo init dojo-starter &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nb"&gt;cd &lt;/span&gt;dojo-starter
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Create Katana Deployment
&lt;/h3&gt;

&lt;p&gt;Deploy a Katana instance using Slot:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;slot deployments create my-dojo-game katana
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Replace &lt;code&gt;my-dojo-game&lt;/code&gt; with your preferred deployment name. This command will:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create a new Katana instance&lt;/li&gt;
&lt;li&gt;Return an RPC endpoint URL&lt;/li&gt;
&lt;li&gt;Provide unique account credentials&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Configure Your Project
&lt;/h3&gt;

&lt;p&gt;After deployment, you'll receive an RPC URL. Update your &lt;code&gt;Scarb.toml&lt;/code&gt; configuration:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight toml"&gt;&lt;code&gt;&lt;span class="nn"&gt;[tool.dojo.env]&lt;/span&gt;
&lt;span class="py"&gt;rpc_url&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"https://api.cartridge.gg/x/your-deployment-id/katana"&lt;/span&gt;
&lt;span class="py"&gt;account_address&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"0x..."&lt;/span&gt;  &lt;span class="c"&gt;# From Katana deployment logs&lt;/span&gt;
&lt;span class="py"&gt;private_key&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"0x..."&lt;/span&gt;      &lt;span class="c"&gt;# From Katana deployment logs&lt;/span&gt;
&lt;span class="py"&gt;world_address&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"0x..."&lt;/span&gt;    &lt;span class="c"&gt;# Will be set after migration&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; Each Katana slot generates unique account seeds, so you'll need to update these values for each deployment.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Monitor Katana Logs
&lt;/h3&gt;

&lt;p&gt;Stream logs from your Katana deployment in a separate terminal:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;slot deployments logs my-dojo-game katana &lt;span class="nt"&gt;-f&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will show real-time logs and display the account information you need for configuration.&lt;/p&gt;

&lt;h3&gt;
  
  
  Build and Migrate
&lt;/h3&gt;

&lt;p&gt;Build your Dojo project:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;sozo build
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Migrate your contracts to the deployed Katana instance:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;sozo migrate
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Upon successful migration, you'll see output similar to:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;✅ Successfully migrated World at address: 0x1234567890abcdef...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Save this &lt;code&gt;WORLD_ADDRESS&lt;/code&gt; as you'll need it for Torii deployment.&lt;/p&gt;

&lt;h2&gt;
  
  
  Torii Indexer Deployment
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Deploy Torii Instance
&lt;/h3&gt;

&lt;p&gt;Deploy a Torii indexer for your world using the world address from the previous step:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;slot deployments create my-dojo-game torii &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="nt"&gt;--world&lt;/span&gt; 0x1234567890abcdef... &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="nt"&gt;--rpc&lt;/span&gt; https://api.cartridge.gg/x/your-deployment-id/katana &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="nt"&gt;--start-block&lt;/span&gt; 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Parameter explanations:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;--world&lt;/code&gt;: The world contract address from your migration&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;--rpc&lt;/code&gt;: Your Katana RPC endpoint&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;--start-block&lt;/code&gt;: Block number to start indexing from (use 1 for complete history)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;After successful deployment, you'll receive:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;GraphQL endpoint for queries&lt;/li&gt;
&lt;li&gt;gRPC endpoint for real-time subscriptions&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Monitor Torii Logs
&lt;/h3&gt;

&lt;p&gt;Stream logs from your Torii deployment:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;slot deployments logs my-dojo-game torii &lt;span class="nt"&gt;-f&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This helps verify that the indexer is properly syncing with your world state.&lt;/p&gt;

&lt;h2&gt;
  
  
  Verification and Testing
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Test Your Deployment
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Verify Katana is running&lt;/strong&gt;: Check that your RPC endpoint responds to requests&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Confirm world deployment&lt;/strong&gt;: Ensure your contracts are properly migrated&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Test Torii indexing&lt;/strong&gt;: Verify that GraphQL queries return expected data&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Access Your Services
&lt;/h3&gt;

&lt;p&gt;Your deployed services will be available at:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Katana RPC&lt;/strong&gt;: &lt;code&gt;https://api.cartridge.gg/x/your-deployment-id/katana&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Torii GraphQL&lt;/strong&gt;: &lt;code&gt;https://api.cartridge.gg/x/your-deployment-id/torii/graphql&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Torii gRPC&lt;/strong&gt;: &lt;code&gt;https://api.cartridge.gg/x/your-deployment-id/torii&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Managing Deployments
&lt;/h2&gt;

&lt;h3&gt;
  
  
  List Deployments
&lt;/h3&gt;

&lt;p&gt;View all your active deployments:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;slot deployments list
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Delete Deployments
&lt;/h3&gt;

&lt;p&gt;Remove deployments when no longer needed:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;slot deployments delete my-dojo-game katana
slot deployments delete my-dojo-game torii
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Best Practices
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Consistent Naming&lt;/strong&gt;: Use the same &lt;code&gt;DEPLOYMENT_NAME&lt;/code&gt; for both Katana and Torii instances&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Monitor Logs&lt;/strong&gt;: Keep log streams open during initial deployment to catch issues early&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Update Configuration&lt;/strong&gt;: Always update your local configuration files with new endpoints&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Version Control&lt;/strong&gt;: Don't commit private keys or sensitive configuration to version control&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Troubleshooting
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Common Issues
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Authentication failures&lt;/strong&gt;: Clear credentials and re-authenticate&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Migration errors&lt;/strong&gt;: Verify RPC URL and account configuration&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Torii sync issues&lt;/strong&gt;: Check that world address and RPC URL are correct&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Getting Help
&lt;/h3&gt;

&lt;p&gt;If you encounter issues:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Check deployment logs for error messages&lt;/li&gt;
&lt;li&gt;Verify all configuration values are correct&lt;/li&gt;
&lt;li&gt;Ensure you're using the latest Dojo version&lt;/li&gt;
&lt;/ol&gt;




&lt;p&gt;🎉 &lt;strong&gt;Congratulations!&lt;/strong&gt; You now have a fully deployed Dojo game with both Katana sequencer and Torii indexer running on Slot's managed infrastructure. Your game is ready for development and testing with persistent, hosted services.&lt;/p&gt;

</description>
      <category>programming</category>
      <category>web3</category>
      <category>gamedev</category>
      <category>cairo</category>
    </item>
    <item>
      <title>Dojo Models Fundamentals</title>
      <dc:creator>Sandeep Chauhan</dc:creator>
      <pubDate>Sat, 30 Aug 2025 15:42:29 +0000</pubDate>
      <link>https://dev.to/sandeep_chauhan_f5476f312/dojo-models-fundamentals-4nkd</link>
      <guid>https://dev.to/sandeep_chauhan_f5476f312/dojo-models-fundamentals-4nkd</guid>
      <description>&lt;p&gt;Welcome to the &lt;strong&gt;Models Fundamentals&lt;/strong&gt; guide for Dojo Engine! This document serves as your entry point to understanding models and the Entity Component System (ECS) architecture in Dojo. Whether you're new to game development or blockchain programming, this guide will build your foundation for creating on-chain games with Dojo.&lt;/p&gt;

&lt;h2&gt;
  
  
  Table of Contents
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Introduction to Models and ECS&lt;/li&gt;
&lt;li&gt;Basic Model Anatomy&lt;/li&gt;
&lt;li&gt;Keys and Entity Identification&lt;/li&gt;
&lt;li&gt;Field Types and Metadata&lt;/li&gt;
&lt;li&gt;Custom Types Implementation&lt;/li&gt;
&lt;li&gt;Next Steps&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Introduction to Models and ECS
&lt;/h2&gt;

&lt;h3&gt;
  
  
  What are Models in Dojo Engine?
&lt;/h3&gt;

&lt;p&gt;In the Dojo Engine, a &lt;strong&gt;model&lt;/strong&gt; is a fundamental data structure that defines the schema for storing game state on-chain. Think of models as blueprints for the attributes of game objects - like a player's stats, a beast's characteristics, or an item's properties. Models serve as the "data layer" in Dojo's ECS pattern, enabling efficient storage, querying, and updating of game state in a decentralized environment.&lt;/p&gt;

&lt;h3&gt;
  
  
  Understanding the ECS Pattern
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;ECS (Entity Component System)&lt;/strong&gt; is a design pattern that organizes game architecture into three core concepts:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Entities&lt;/strong&gt;: Unique identifiers that represent game objects (like a specific player or beast)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Components&lt;/strong&gt;: Data containers that store attributes (in Dojo, these are &lt;strong&gt;models&lt;/strong&gt;)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Systems&lt;/strong&gt;: Logic modules that process and manipulate the data&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Why ECS Matters for Onchain Games
&lt;/h3&gt;

&lt;p&gt;The ECS approach provides several key benefits for blockchain game development:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Modularity&lt;/strong&gt;: Separate data (models) from logic (systems) for cleaner code&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Scalability&lt;/strong&gt;: Efficiently manage complex game states with many entities&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Flexibility&lt;/strong&gt;: Easily add new features by creating new models and systems&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Performance&lt;/strong&gt;: Optimized for Starknet's execution environment&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Relationship Between Entities, Components, and Systems
&lt;/h3&gt;

&lt;p&gt;In Dojo's ECS implementation:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Entities&lt;/strong&gt; are identified by their key values (like a &lt;code&gt;ContractAddress&lt;/code&gt; or composite key)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Models&lt;/strong&gt; act as components, storing specific data about entities&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Systems&lt;/strong&gt; contain game logic that reads and updates models&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Example relationship:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Entity: Player (ContractAddress: 0x123...)
├── Player model (battles_won, battles_lost, creation_day)
├── Beast model (level, experience, beast_type)
└── Inventory model (items, quantities)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Basic Model Anatomy
&lt;/h2&gt;

&lt;p&gt;Every Dojo model follows a consistent structure using Cairo syntax and specific attributes.&lt;/p&gt;

&lt;h3&gt;
  
  
  The #[dojo::model] Attribute
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;#[dojo::model]&lt;/code&gt; attribute marks a Cairo struct as a Dojo model, registering it in the world contract for ECS integration:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#[derive(Copy, Drop, Serde, Debug, PartialEq)]
#[dojo::model]
pub struct Player {
    #[key]
    pub address: ContractAddress,
    pub current_beast_id: u16,
    pub battles_won: u16,
    pub battles_lost: u16,
    pub last_active_day: u32,
    pub creation_day: u32,
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Simple Model Structure with Single Key
&lt;/h3&gt;

&lt;p&gt;The simplest model structure includes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Required attributes and derives&lt;/li&gt;
&lt;li&gt;A single &lt;code&gt;#[key]&lt;/code&gt; field for entity identification&lt;/li&gt;
&lt;li&gt;Data fields using appropriate types
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#[derive(Drop, Serde)]
#[dojo::model]
struct GameSession {
    #[key]
    pub session_id: u64,
    pub player_count: u8,
    pub status: u8,
    pub created_at: u32,
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Basic Field Types
&lt;/h3&gt;

&lt;p&gt;Dojo models support Cairo's primitive types:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;u8&lt;/code&gt;, &lt;code&gt;u16&lt;/code&gt;, &lt;code&gt;u32&lt;/code&gt;, &lt;code&gt;u64&lt;/code&gt;&lt;/strong&gt;: Unsigned integers of different sizes&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;felt252&lt;/code&gt;&lt;/strong&gt;: Field element (primary data type in Cairo)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;ContractAddress&lt;/code&gt;&lt;/strong&gt;: Starknet contract addresses&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;bool&lt;/code&gt;&lt;/strong&gt;: Boolean values&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Choose types based on your data requirements:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use &lt;code&gt;u8&lt;/code&gt; for small values (0-255) like levels or counts&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;u32&lt;/code&gt; for larger values like timestamps&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;felt252&lt;/code&gt; for strings and identifiers&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;ContractAddress&lt;/code&gt; for player or contract references&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Essential Derives
&lt;/h3&gt;

&lt;p&gt;Every model requires specific derives for proper functionality:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;Drop&lt;/code&gt;&lt;/strong&gt;: Enables safe memory deallocation (required)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;Serde&lt;/code&gt;&lt;/strong&gt;: Enables serialization/deserialization (required)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;Copy&lt;/code&gt;&lt;/strong&gt;: Allows copying instead of moving (optional, useful for simple data)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;Debug&lt;/code&gt;&lt;/strong&gt;: Enables debug output (optional, helpful for development)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;PartialEq&lt;/code&gt;&lt;/strong&gt;: Enables equality comparisons (optional)
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#[derive(Copy, Drop, Serde, Debug, PartialEq)]
#[dojo::model]
pub struct SimpleItem {
    #[key]
    pub id: u32,
    pub name: felt252,
    pub value: u16,
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Keys and Entity Identification
&lt;/h2&gt;

&lt;p&gt;Keys are crucial for identifying and querying entities in your game world.&lt;/p&gt;

&lt;h3&gt;
  
  
  The #[key] Attribute and Its Purpose
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;#[key]&lt;/code&gt; attribute specifies which fields serve as the unique identifier for a model instance. Every model must have at least one key field, and all key fields must appear before non-key fields in the struct definition.&lt;/p&gt;

&lt;h3&gt;
  
  
  Single Key Pattern
&lt;/h3&gt;

&lt;p&gt;The most straightforward approach uses a single field as the key:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#[derive(Copy, Drop, Serde, Debug, PartialEq)]
#[dojo::model]
pub struct Player {
    #[key]
    pub address: ContractAddress,  // Single key field
    pub current_beast_id: u16,
    pub battles_won: u16,
    pub battles_lost: u16,
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Composite Key Pattern
&lt;/h3&gt;

&lt;p&gt;For more complex relationships, use multiple key fields to create composite keys:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#[derive(Copy, Drop, Serde, Debug, PartialEq)]
#[dojo::model]
pub struct Beast {
    #[key]
    pub player: ContractAddress,    // First part of composite key
    #[key]
    pub beast_id: u16,             // Second part of composite key
    pub level: u8,
    pub experience: u16,
    pub beast_type: BeastType,
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Key Ordering Requirements
&lt;/h3&gt;

&lt;p&gt;Important rules for keys:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;All &lt;code&gt;#[key]&lt;/code&gt; fields must come before non-key fields&lt;/li&gt;
&lt;li&gt;Key order in the struct defines the query order&lt;/li&gt;
&lt;li&gt;All keys must be provided when querying composite key models&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Querying Models with Different Key Structures
&lt;/h3&gt;

&lt;p&gt;How you query models depends on their key structure:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Single key query
let player = world.read_model(player_address);

// Composite key query (requires all key values)
let beast = world.read_model((player_address, beast_id));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Field Types and Metadata
&lt;/h2&gt;

&lt;p&gt;Understanding data types and metadata is essential for creating efficient models.&lt;/p&gt;

&lt;h3&gt;
  
  
  Cairo Primitive Types in Models
&lt;/h3&gt;

&lt;p&gt;Choose the appropriate type based on your data requirements:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Type&lt;/th&gt;
&lt;th&gt;Range&lt;/th&gt;
&lt;th&gt;Use Cases&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;u8&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;0-255&lt;/td&gt;
&lt;td&gt;Levels, small counters, percentages&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;u16&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;0-65,535&lt;/td&gt;
&lt;td&gt;Larger counters, experience points&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;u32&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;0-4.3B&lt;/td&gt;
&lt;td&gt;Timestamps, large quantities&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;u64&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;0-18.4 quintillion&lt;/td&gt;
&lt;td&gt;Very large values, IDs&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;felt252&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Field element&lt;/td&gt;
&lt;td&gt;Names, strings, large identifiers&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;ContractAddress&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Starknet address&lt;/td&gt;
&lt;td&gt;Player addresses, contract references&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  When to Use Each Type
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Performance considerations:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Smaller types (&lt;code&gt;u8&lt;/code&gt;, &lt;code&gt;u16&lt;/code&gt;) use less storage and gas&lt;/li&gt;
&lt;li&gt;Larger types provide more range but cost more&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;felt252&lt;/code&gt; is versatile but should be used thoughtfully&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example with appropriate type choices:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#[derive(Copy, Drop, Serde, Debug, PartialEq)]
#[dojo::model]
pub struct GameItem {
    #[key]
    pub id: u64,              // Large range for unique IDs
    pub name: felt252,        // String data
    pub level_required: u8,   // Small range (1-100)
    pub price: u32,           // Medium range for currency
    pub owner: ContractAddress, // Player reference
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Required vs Optional Derives
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Required derives:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Drop&lt;/code&gt;: Memory management (always required)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Serde&lt;/code&gt;: Data serialization (always required)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Optional but recommended derives:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Copy&lt;/code&gt;: For simple data that can be copied cheaply&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Debug&lt;/code&gt;: For development and testing&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;PartialEq&lt;/code&gt;: For comparing model instances&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Type Safety Considerations for Game Data
&lt;/h3&gt;

&lt;p&gt;Ensure your types match your game's requirements:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#[derive(Copy, Drop, Serde, Debug, PartialEq)]
#[dojo::model]
pub struct PlayerStats {
    #[key]
    pub player: ContractAddress,
    pub health: u16,        // 0-65535 HP range
    pub mana: u16,          // 0-65535 MP range  
    pub level: u8,          // 1-255 level cap
    pub experience: u32,    // Large XP values
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Custom Types Implementation
&lt;/h2&gt;

&lt;p&gt;Advanced models often require custom types like enums and structs.&lt;/p&gt;

&lt;h3&gt;
  
  
  Implementing Custom Types in Models
&lt;/h3&gt;

&lt;p&gt;Custom types must implement the &lt;code&gt;Introspect&lt;/code&gt; trait to be used in models. Here's an example with a custom enum:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#[derive(Copy, Drop, Serde, Debug, PartialEq, Introspect)]
pub enum BeastType {
    Fire,
    Water,
    Earth,
    Air,
}

#[derive(Copy, Drop, Serde, Debug, PartialEq)]
#[dojo::model]
pub struct Beast {
    #[key]
    pub player: ContractAddress,
    #[key] 
    pub beast_id: u16,
    pub level: u8,
    pub experience: u16,
    pub beast_type: BeastType,  // Custom enum type
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Introspect Trait Requirements
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;Introspect&lt;/code&gt; trait allows Dojo to understand your custom type's structure. For types you define, you can usually auto-derive it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#[derive(Drop, Serde, Introspect)]
pub struct Stats {
    pub attack: u8,
    pub defense: u8,
    pub speed: u8,
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Automatic Derivation vs Manual Implementation
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Automatic derivation&lt;/strong&gt; (preferred when possible):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#[derive(Drop, Serde, Introspect)]
pub enum Rarity {
    Common,
    Uncommon,
    Rare,
    Epic,
    Legendary,
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Manual implementation&lt;/strong&gt; (for complex cases or external types):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;impl StatsIntrospect of dojo::database::introspect::Introspect&amp;lt;Stats&amp;gt; {
    #[inline(always)]
    fn size() -&amp;gt; Option&amp;lt;usize&amp;gt; {
        Option::Some(3)  // 3 u8 fields
    }

    fn layout() -&amp;gt; dojo::database::introspect::Layout {
        // Custom layout implementation
    }

    #[inline(always)]
    fn ty() -&amp;gt; dojo::database::introspect::Ty {
        // Type definition implementation  
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  IntrospectPacked for Space Efficiency
&lt;/h3&gt;

&lt;p&gt;Use &lt;code&gt;IntrospectPacked&lt;/code&gt; when you want to optimize storage space for models with known, fixed sizes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#[derive(Drop, Serde, IntrospectPacked, Debug)]
#[dojo::model]
struct Potion {
    #[key]
    id: u64,
    name: felt252,
    effect: u8,
    rarity: Rarity,
    power: u32,
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Benefits of IntrospectPacked:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Reduced storage costs&lt;/li&gt;
&lt;li&gt;More efficient packing of data&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Limitations:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Cannot use dynamic types (&lt;code&gt;Array&lt;/code&gt;, &lt;code&gt;ByteArray&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Less flexible for future upgrades&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Custom Enums and Structs in Game Models
&lt;/h3&gt;

&lt;p&gt;Practical example combining custom types in a game context:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#[derive(Copy, Drop, Serde, Debug, PartialEq, Introspect)]
pub enum ItemType {
    Weapon,
    Armor,
    Consumable,
    Quest,
}

#[derive(Copy, Drop, Serde, Debug, PartialEq, Introspect)]
pub struct ItemStats {
    pub attack_bonus: u8,
    pub defense_bonus: u8,
    pub durability: u8,
}

#[derive(Copy, Drop, Serde, Debug, PartialEq)]
#[dojo::model]
pub struct GameItem {
    #[key]
    pub id: u64,
    #[key]
    pub owner: ContractAddress,
    pub item_type: ItemType,     // Custom enum
    pub stats: ItemStats,        // Custom struct
    pub quantity: u16,
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Next Steps
&lt;/h2&gt;

&lt;p&gt;Congratulations! You now understand the fundamentals of Dojo models and ECS architecture. You should be able to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Define basic model structures with appropriate types&lt;/li&gt;
&lt;li&gt;Use single and composite keys for entity identification
&lt;/li&gt;
&lt;li&gt;Implement custom types with the Introspect trait&lt;/li&gt;
&lt;li&gt;Choose appropriate field types for game data&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Continue Your Learning Journey
&lt;/h3&gt;

&lt;p&gt;Now that you have a solid foundation, explore these advanced topics:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;a href="///getting-started/basics/models/models-patterns.md"&gt;Models Patterns&lt;/a&gt;&lt;/strong&gt;: Learn advanced patterns like model relationships, validation, and optimization techniques&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Systems Integration&lt;/strong&gt;: Discover how systems interact with your models&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;World Management&lt;/strong&gt;: Understand how the World contract orchestrates your game state&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Query Optimization&lt;/strong&gt;: Advanced techniques for efficient model queries&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Remember:
&lt;/h3&gt;

&lt;p&gt;Start simple, test thoroughly, and gradually add complexity as you become more comfortable with the ECS pattern in Dojo!&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
