-
Notifications
You must be signed in to change notification settings - Fork 22
/
basic.rs
45 lines (39 loc) · 1.23 KB
/
basic.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
use bevy::{log::LogPlugin, prelude::*};
use bevy_pkv::PkvStore;
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)]
struct User {
name: String,
}
fn setup(mut pkv: ResMut<PkvStore>) {
// strings
if let Ok(username) = pkv.get::<String>("username") {
info!("Welcome back {username}");
} else {
pkv.set_string("username", "alice")
.expect("failed to store username");
// alternatively, using the slightly less efficient generic api:
pkv.set("username", &"alice".to_string())
.expect("failed to store username");
}
// serde types
if let Ok(user) = pkv.get::<User>("user") {
info!("Welcome back {}", user.name);
} else {
let user = User {
name: "bob".to_string(),
};
pkv.set("user", &user).expect("failed to store User struct");
}
}
fn main() {
// When building for WASM, print panics to the browser console
#[cfg(target_arch = "wasm32")]
console_error_panic_hook::set_once();
App::new()
.insert_resource(PkvStore::new("BevyPkv", "BasicExample"))
.add_plugins(MinimalPlugins)
.add_plugins(LogPlugin::default())
.add_systems(Startup, setup)
.run();
}