forked from johanhelsing/bevy_pkv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
enumkeys.rs
52 lines (45 loc) · 1.35 KB
/
enumkeys.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
46
47
48
49
50
51
52
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>(PkvKeys::UserName) {
info!("Welcome back {username}");
} else {
pkv.set_string(PkvKeys::UserName, "alice")
.expect("failed to store username");
// alternatively, using the slightly less efficient generic api:
pkv.set(PkvKeys::UserName, &"alice".to_string())
.expect("failed to store username");
}
// serde types
if let Ok(user) = pkv.get::<User>(PkvKeys::User) {
info!("Welcome back {}", user.name);
} else {
let user = User {
name: "bob".to_string(),
};
pkv.set(PkvKeys::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", "EnumExample"))
.add_plugins(MinimalPlugins)
.add_plugins(LogPlugin::default())
.add_systems(Startup, setup)
.run();
}
#[derive(strum_macros::AsRefStr)]
enum PkvKeys {
User,
UserName,
}