Trait valence::ecs::prelude::Resource

pub trait Resource: Send + Sync + 'static { }
Expand description

A type that can be inserted into a World as a singleton.

You can access resource data in systems using the Res and ResMut system parameters

Only one resource of each type can be stored in a World at any given time.

Examples

#[derive(Resource)]
struct MyResource { value: u32 }

world.insert_resource(MyResource { value: 42 });

fn read_resource_system(resource: Res<MyResource>) {
    assert_eq!(resource.value, 42);
}

fn write_resource_system(mut resource: ResMut<MyResource>) {
    assert_eq!(resource.value, 42);
    resource.value = 0;
    assert_eq!(resource.value, 0);
}

!Sync Resources

A !Sync type cannot implement Resource. However, it is possible to wrap a Send but not Sync type in SyncCell or the currently unstable Exclusive to make it Sync. This forces only having mutable access (&mut T only, never &T), but makes it safe to reference across multiple threads.

This will fail to compile since RefCell is !Sync.


#[derive(Resource)]
struct NotSync {
   counter: RefCell<usize>,
}

This will compile since the RefCell is wrapped with SyncCell.

use bevy_utils::synccell::SyncCell;

#[derive(Resource)]
struct ActuallySync {
   counter: SyncCell<RefCell<usize>>,
}

Implementations on Foreign Types§

§

impl<T> Resource for ReportHierarchyIssue<T>where ReportHierarchyIssue<T>: Send + Sync + 'static,

§

impl Resource for FrameCountwhere FrameCount: Send + Sync + 'static,

source§

impl Resource for CommandRegistrywhere CommandRegistry: Send + Sync + 'static,

source§

impl Resource for CommandScopeRegistrywhere CommandScopeRegistry: Send + Sync + 'static,

source§

impl Resource for InventorySettingswhere InventorySettings: Send + Sync + 'static,

Implementors§

§

impl Resource for MainScheduleOrderwhere MainScheduleOrder: Send + Sync + 'static,

source§

impl Resource for EntityHitboxSettingswhere EntityHitboxSettings: Send + Sync + 'static,

source§

impl Resource for EntityManagerwhere EntityManager: Send + Sync + 'static,

source§

impl Resource for KeepaliveSettingswhere KeepaliveSettings: Send + Sync + 'static,

source§

impl Resource for MovementSettingswhere MovementSettings: Send + Sync + 'static,

source§

impl Resource for NetworkSettingswhere NetworkSettings: Send + Sync + 'static,

source§

impl Resource for PlayerListwhere PlayerList: Send + Sync + 'static,

source§

impl Resource for SharedNetworkStatewhere SharedNetworkState: Send + Sync + 'static,

source§

impl Resource for BiomeRegistrywhere BiomeRegistry: Send + Sync + 'static,

source§

impl Resource for DimensionTypeRegistrywhere DimensionTypeRegistry: Send + Sync + 'static,

source§

impl Resource for RegistryCodecwhere RegistryCodec: Send + Sync + 'static,

source§

impl Resource for TagsRegistrywhere TagsRegistry: Send + Sync + 'static,

source§

impl Resource for Serverwhere Server: Send + Sync + 'static,

source§

impl Resource for ServerSettingswhere ServerSettings: Send + Sync + 'static,

§

impl Resource for MainThreadExecutorwhere MainThreadExecutor: Send + Sync + 'static,

§

impl Resource for AppTypeRegistrywhere AppTypeRegistry: Send + Sync + 'static,

§

impl Resource for Scheduleswhere Schedules: Send + Sync + 'static,

§

impl<E> Resource for Events<E>where E: Event, Events<E>: Send + Sync + 'static,

§

impl<S> Resource for NextState<S>where S: States, NextState<S>: Send + Sync + 'static,

§

impl<S> Resource for State<S>where S: States, State<S>: Send + Sync + 'static,