Skip to content

Commit

Permalink
feat: add spawn and move function to update member
Browse files Browse the repository at this point in the history
  • Loading branch information
glihm committed Jan 2, 2025
1 parent 04cc6e2 commit 98600be
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
2 changes: 1 addition & 1 deletion examples/spawn-and-move/Scarb.lock
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ dependencies = [

[[package]]
name = "dojo_examples"
version = "1.0.8"
version = "1.0.9"
dependencies = [
"armory",
"bestiary",
Expand Down
17 changes: 15 additions & 2 deletions examples/spawn-and-move/manifest_dev.json
Original file line number Diff line number Diff line change
Expand Up @@ -1252,8 +1252,8 @@
},
"contracts": [
{
"address": "0x7e8a52c68b243d3a86a55c04ccec2edc760252d5952566d3af4001bd6cf38f3",
"class_hash": "0x636c2cf31b094097625cb5ada96f54ee9a3f7bc6d8cde00cc85e5ef0c622c8b",
"address": "0x6bfba78b8f4f42da469860a95291c2fad959c91ea747e2062de763ff4e62c4a",
"class_hash": "0x3e65d56b082c47e9e0952503424bcf6956ddd6d5faff40dd9900ea787d89c6",
"abi": [
{
"type": "impl",
Expand Down Expand Up @@ -1399,6 +1399,18 @@
"outputs": [],
"state_mutability": "external"
},
{
"type": "function",
"name": "update_player_config_name",
"inputs": [
{
"name": "name",
"type": "core::byte_array::ByteArray"
}
],
"outputs": [],
"state_mutability": "external"
},
{
"type": "function",
"name": "get_player_position",
Expand Down Expand Up @@ -1585,6 +1597,7 @@
"spawn",
"move",
"set_player_config",
"update_player_config_name",
"reset_player_config",
"set_player_server_profile",
"set_models",
Expand Down
14 changes: 13 additions & 1 deletion examples/spawn-and-move/src/actions.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pub trait IActions<T> {
fn spawn(ref self: T);
fn move(ref self: T, direction: Direction);
fn set_player_config(ref self: T, name: ByteArray);
fn update_player_config_name(ref self: T, name: ByteArray);
fn get_player_position(self: @T) -> Position;
fn reset_player_config(ref self: T);
fn set_player_server_profile(ref self: T, server_id: u32, name: ByteArray);
Expand All @@ -22,7 +23,7 @@ pub mod actions {
Position, Moves, MovesValue, Direction, Vec2, PlayerConfig, PlayerItem, ServerProfile,
};
use dojo_examples::utils::next_position;
use dojo::model::{ModelStorage, ModelValueStorage};
use dojo::model::{ModelStorage, ModelValueStorage, Model};
use dojo::event::EventStorage;

// Features can be used on modules, structs, trait and `use`. Not inside
Expand Down Expand Up @@ -131,6 +132,17 @@ pub mod actions {
world.write_model(@config);
}

fn update_player_config_name(ref self: ContractState, name: ByteArray) {
let mut world = self.world_default();
let player = get_caller_address();

// Don't need to read the model here, we directly overwrite the member "name".
world
.write_member(
Model::<PlayerConfig>::ptr_from_keys(player), selector!("name"), name
);
}

fn reset_player_config(ref self: ContractState) {
let player = get_caller_address();
let mut world = self.world_default();
Expand Down

0 comments on commit 98600be

Please sign in to comment.