| Crates.io | bevy_prefs_lite |
| lib.rs | bevy_prefs_lite |
| version | 0.1.1 |
| created_at | 2025-01-14 19:39:23.663868+00 |
| updated_at | 2025-01-14 20:14:45.68419+00 |
| description | A simple preferences system for Bevy |
| homepage | |
| repository | |
| max_upload_size | |
| id | 1516521 |
| size | 181,170 |
This crate provides basic preferences support for Bevy applications. The word "preferences" in this context is used to mean user settings that are (1) set while running the app, (2) persistent across restarts, and (3) implicitly saved. It is not meant to be a general config file serialization mechanism.
Preferences typically include things like:
Preferences are NOT:
~/.config or
$HOME/Library/Preferences/. That being said, the format of preference files is TOML or JSON,
which can easily be edited in a text editor.Command which immediately and synchronously
writes out the settings file.When compiling for WASM targets, preferences are stored in browser LocalStorage in serialized
JSON format.
When compiling for desktop, preferences are stored as TOML files in the standard OS locations for user preferences.
Because this library supports "simple" preferences, some things have been intentionally left out:
serde to JSON or TOML it will work.The Preferences object represents the container for preferences files. Within this container
you can create individual PreferenceFile objects, each one backed by a separate file such as
"prefs.toml" (on the web, each file is stored as a separate key item in LocalStorage).
Each preferences file contains one or more PreferenceGroups which represents a section within
the file. Groups can also contain other groups.
Finally, groups have individual properties which are accessed via get and set methods.
In the examples below, the app.toml file would have a structure like this:
[window]
size = [
800,
600
]
Normally the Preferences object is initialized during app initialization. You create a new
Preferences object, passing it a unique string which identifies your application. This string
is used to ensure that your preferences don't overwrite those of other apps.
The "reverse domain name" convention is an easy way to ensure global uniqueness:
// Configure preferences directory
let mut preferences = Preferences::new("com.mydomain.coolgame");
In desktop targets, the app name is used to establish a preferences directory in the standard OS location for preferences.
The preferences store will verify that the preferences directory exists, but won't load anything
yet. To actually load preferences, you'll need to load a PreferencesFile, which corresponds
to individual preference files in your config directory such as app.toml:
let app_prefs = preferences.get("app").unwrap();
if let Some(window_group) = app_prefs.get_group("window") {
if let Some(window_size) = window_group.get::<UVec2>("size") {
// Configure window size
}
}
So for example on Mac, the above code would look for a file in the location "$HOME/Library/Preferences/com.mydomain.coolgame/app.toml".
In WASM, it would look for a local storage key named "com.mydomain.coolgame-app".
The Preferences object is also an ECS Resource, so you can insert it into the game world. This
makes it easy for other parts of the game code to load their preference settings. For example,
startup systems can inject preferences like any other resource.
app.insert_resource(preferences);
To save preferences, you can use the mut versions of the preference methods:
let mut app_prefs = preferences.get_mut("app").unwrap();
let window_group = app_prefs.get_group_mut("window").unwrap();
window_group.set("size", UVec2::new(10, 10));
The mut methods do several things:
However, setting the value only changes the preferences setting in memory, it does not automatically
save the changes to disk. To trigger a save, you can issue a SavePreferences command:
commands.queue(SavePreferences::IfChanged);
This will cause any preference files to be saved if they are marked as changed. It's up to you to decide when to save preferences, but they should be saved before the app exits.
The AutosavePrefsPlugin implements a timer which can be used to save preferences. Once you
install this plugin, you can then start the timer by issuing a command:
commands.queue(StartAutosaveTimer);
This command sets the save timer to 1 second, which counts down and then saves any changed preference files when the timer goes off. This is useful for settings that change at high frequency (like dragging an audio volume slider), reducing the number of writes to disk.