9 releases
new 0.3.4 | Dec 9, 2024 |
---|---|
0.3.3 | Dec 9, 2024 |
0.3.2 | Jul 9, 2024 |
0.3.1 | May 27, 2024 |
0.1.2 | May 25, 2024 |
#563 in Game dev
359 downloads per month
Used in 2 crates
(via lightyear)
16KB
200 lines
bevy_web_keepalive
Library of bevy plugins to keep a bevy app running in the browser despite despite not being visible
WebKeepalivePlugin
The WebKeepalivePlugin
plugin creates a web worker that runs the main schedule to keep bevy running in the background (eg. when the user is on another browser tab).
Usage:
// To add the worker, use add_plugins
app.add_plugins(WebKeepalivePlugin::default())
// Configure the worker like this:
app.add_plugins(WebKeepalivePlugin {
initial_wake_delay: 1000.0, // 1 sec delay
})
// To change the wake_delay at run-time, access the `KeepaliveSettings` resource in a system
fn system_a(mut settings: ResMut<KeepaliveSettings>) {
settings.wake_delay = 16.667; // 60Hz updates
}
// To terminate the web worker remove the `KeepaliveSettings` resource
fn system_b(world: &mut World) {
world.remove_resource::<KeepaliveSettings>();
}
Reasoning: bevy_winit
runs it's event loop via requestAnimationFrame. This works well for apps that don't need to run if they are in the background. However there are situations where this is unwanted such as multiplayer games that require a constant connection and cannot rely on reconnecting.
VisibilityChangeListenerPlugin
The VisibilityChangeListenerPlugin
plugin registers a listener that fires whenever the app's visibility has changed and updates the WindowVisibility
resource while also allowing the Main
schedule to run a last time after the app is hidden.
Usage:
// To add the listener, use add_plugins
app.add_plugins(VisibilityChangeListenerPlugin::default())
// To actually run the main schedule, configure the plugin like this:
app.add_plugins(VisibilityChangeListenerPlugin { run_main_schedule_on_hide: true })
// To use the `WindowVisibility` resource, access it in a system
fn system_a(window_visibility: Res<WindowVisibility>) {
if !window_visibility.0 {
// Do something that you want to do whenever the window is hidden
}
}
Reasoning: This may be used to notify internal or external services of user inactivity.
Feature Requirements: listener
BackgroundTimerPlugin
The BackgroundTimerPlugin
plugin adds a utility resource which contains a timer that keeps track of time spent in the background. This plugin needs to be paired with the WebKeepalivePlugin
to function properly (frame delta time is capped at 250ms in bevy_time by default)
Usage:
// To add the listener, use add_plugins, please note that the WebKeepalivePlugin.initial_wake_delay should be < 250.0 so that we can ensure that the frame delta time won't be capped at 250ms
app.add_plugins((WebKeepalivePlugin::default(), BackgroundTimerPlugin))
// To use the `BackgroundTimer` resource, access it in a system
fn system_a(timer: Res<BackgroundTimer>) {
if !timer.0.elapsed_secs() > 60.0 {
// Clientside-timeout the user or something similar
}
}
Reasoning: Can be used to collect analytics about user behavior regarding window visibility or (clientside) timeout a player in multiplayer games after a certain time of inactivity.
Feature Requirements: timer
Dependencies
~17–27MB
~397K SLoC