Skip to content

Commit

Permalink
Add DummyResource
Browse files Browse the repository at this point in the history
  • Loading branch information
alice-i-cecile committed Aug 6, 2024
1 parent 89ec5ce commit 636b198
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
4 changes: 3 additions & 1 deletion src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use bevy::reflect::TypePath;
use bevy::time::run_fixed_main_schedule;
#[cfg(feature = "ui")]
use bevy::ui::UiSystem;
use updating::{CentralInputStore, InputUpdateSystem};
use updating::{CentralInputStore, DummyResource, InputUpdateSystem};

use crate::action_state::{ActionState, ButtonData};
use crate::clashing_inputs::ClashStrategy;
Expand Down Expand Up @@ -282,6 +282,8 @@ pub struct CentralInputStorePlugin;

impl Plugin for CentralInputStorePlugin {
fn build(&self, app: &mut App) {
app.init_resource::<DummyResource>();

let mut central_input_store = CentralInputStore::default();
central_input_store.register_standard_input_kinds(app);

Expand Down
17 changes: 14 additions & 3 deletions src/user_input/updating.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ impl CentralInputStore {
/// Registers a new kind of input.
///
/// This will allow the input to be updated based on the state of the world,
/// by adding the [`UpdatableUserInput::update`] system to one of the [`InputUpdateSystem`] sets,
/// by adding the [`UpdatableUserInput::compute`] system to one of the [`InputUpdateSystem`] sets,
/// and thus to [`InputManagerSystem::Update`] during [`PreUpdate`].
///
/// This method has no effect if the input kind has already been registered.
Expand All @@ -50,7 +50,7 @@ impl CentralInputStore {
}

self.registered_input_kinds.insert(TypeId::of::<I>());
app.add_systems(PreUpdate, I::update.in_set(I::SYSTEM_SET));
app.add_systems(PreUpdate, I::compute.in_set(I::SYSTEM_SET));
}

/// Registers the standard input types defined by [`bevy`] and [`leafwing_input_manager`](crate).
Expand Down Expand Up @@ -199,6 +199,13 @@ enum UpdatedValues {
pub trait UpdatableUserInput: UserInput {
/// The resource data that must be fetched from the world in order to update the user input.
///
/// If your system does not need any additional data, you can use [`DummyResource`] as a placeholder,
/// which will be initialized by [`CentralInputStorePlugin`](crate::plugin::CentralInputStorePlugin) to avoid panicking.
/// Ideally one could use `()`, but that requires a [change to Bevy itself](https://github.com/bevyengine/bevy/issues/14640).
///
/// Input types that are derived from other inputs should generally not need additional data,
/// as they can be computed directly from the [`CentralInputStore`].
///
/// # Panics
///
/// This type cannot be [`CentralInputStore`], as that would cause mutable aliasing and panic at runtime.
Expand All @@ -214,7 +221,7 @@ pub trait UpdatableUserInput: UserInput {
/// # Warning
///
/// This system should not be added manually: instead, call [`CentralInputStore::register_input_kind`].
fn update(central_input_store: ResMut<CentralInputStore>, source_data: Res<Self::SourceData>);
fn compute(central_input_store: ResMut<CentralInputStore>, source_data: Res<Self::SourceData>);
}

/// More granular system sets that belong to [`InputManagerSystem::Update`].
Expand All @@ -234,3 +241,7 @@ pub enum InputUpdateSystem {
/// Updates that combine multiple user inputs into a single value.
Chord,
}

/// A dummy resource that can be used as a placeholder for [`UpdatableUserInput::SourceData`].
#[derive(Resource, Default)]
pub struct DummyResource;

0 comments on commit 636b198

Please sign in to comment.