Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bevy Preferences API #13311

Open
viridia opened this issue May 10, 2024 · 5 comments
Open

Bevy Preferences API #13311

viridia opened this issue May 10, 2024 · 5 comments
Labels
A-Core Common functionality for all bevy apps C-Enhancement A new feature S-Needs-RFC This issue or PR is particularly complex, and needs an approved RFC before it can be merged X-Controversial There is active debate or serious implications around merging this PR

Comments

@viridia
Copy link
Contributor

viridia commented May 10, 2024

What problem does this solve or what need does it fill?

Games and editors need a way to store user preferences: things like video options, keyboard/controller mappings and so on.

What solution would you like?

I proposal we add an API to Bevy for saving and loading of user preferences.

This should support at least the following features:

  • It should support serde serialization of preference data in some common format.
  • It should allow plugins and libraries to be able to define their own preferences that can be stored along with the app preferences.
  • For desktop platforms and mobile, it would know the correct directory for storing the preferences in the user's home directory.
  • For browser platforms, it should be able to store preference data in browser local storage.

The solution you propose for the problem presented.

What alternative(s) have you considered?

There are a number of existing Rust crates that provide preference-saving functionality. However, these don't necessarily conform to the requirements for a Bevy-based game. For example, some crates may assume that the preferences are stored in the filesystem, which may not be true in a browser-based game.

@viridia viridia added C-Enhancement A new feature S-Needs-Triage This issue needs to be labelled labels May 10, 2024
@james7132
Copy link
Member

james7132 commented May 10, 2024

The API here needs to be carefully considered. The proposal here mirrors that of PlayerPrefs and EditorPrefs in Unity, which is backed by the Windows registry, local storage on Web, and a sqlite storage on other platforms. However, these APIs are notorious for being beginner traps for persistent state as they are always results in blocking disk IO on each call. Accidentally using these APIs in a loop can instantly tank performance, and making the APIs only periodically commit their states to disk may introduce consistency issues in the case of a process crash.

@ItsDoot ItsDoot added A-App Bevy apps and plugins and removed S-Needs-Triage This issue needs to be labelled labels May 10, 2024
@aevyrie
Copy link
Member

aevyrie commented May 10, 2024

I've put up a PR. My approach there is to provide a simple typed value store in a bevy resource, using Reflect. Users of preferences don't need to know anything about where preferences are stored, or how they got there. They just need to know the rust type of the preferences they care about, and can get/set/mut.

I'm of the opinion bevy should provide some filesystem backend for this, but make it easy for users to bring their own. We can make the simple cases easy, and the complex cases possible. A simple filesystem backend might be as straightforward as writing to the assets directory, and watching for changes. Both can be done asynchronously without blocking the main event loop.

We can also aim to build a production-ready filesystem backend, but I suspect that will be a significant undertaking.

@valaphee
Copy link
Contributor

I would keep preferences as individual reflect resources marked to be stored/restored as pref:

  • it allows for change detection, you don't need to bridge between them
  • keeps all prefs separated, as for example with the PlayerPrefs api the keyName doesn't have to be unique

And a separate backend which handles the actual storing, restoring of preferences to the local user directory.

I don't know if it would be better to mark them in code, by registering resources to be stored, or if the resource itself should be marked.

As a more long-term solution it would probably a good idea to make a storage api in general, where prefs is a part of, as similar data like save game state, cache, etc. could also be handled this way.

@alice-i-cecile
Copy link
Member

@cart has a comprehensive comment laying out requirements and design that's essential reading for anyone tackling this: #13312 (comment)

@alice-i-cecile alice-i-cecile added X-Controversial There is active debate or serious implications around merging this PR A-Core Common functionality for all bevy apps S-Needs-RFC This issue or PR is particularly complex, and needs an approved RFC before it can be merged and removed A-App Bevy apps and plugins labels May 13, 2024
@viridia
Copy link
Contributor Author

viridia commented May 22, 2024

Detailed requirements doc here: https://hackmd.io/@dreamertalin/rkhljFM7R

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Core Common functionality for all bevy apps C-Enhancement A new feature S-Needs-RFC This issue or PR is particularly complex, and needs an approved RFC before it can be merged X-Controversial There is active debate or serious implications around merging this PR
Projects
None yet
Development

No branches or pull requests

6 participants