#static #bevy #attach #type #statically #collection #handle

bevy_cell

Attach Bevy's Handles/Entities statically to Types

6 releases

0.13.3 Jan 1, 2024
0.13.2 Dec 29, 2023
0.12.0 Nov 12, 2023
0.11.1 Aug 31, 2023

#1032 in Rust patterns

Download history 14/week @ 2023-12-29 14/week @ 2024-02-16 15/week @ 2024-02-23 1/week @ 2024-03-01 66/week @ 2024-03-29 14/week @ 2024-04-05

80 downloads per month

MIT/Apache

32KB

🦊 Easily attach bevy's Handles/Entities statically to types
🐑 Get them in any system, without using Resources.
🦄 See type_cell for any other use case!

[dependencies]
bevy_cell = "0.13"
use bevy_cell::*;

I. There are 2 valid syntaxes:
🐰 {Type} [name1] [name2] [name3]
🦝 Type: [name1] [name2] [name3];

II. The syntax inside the [] will change the attached type:
🐈 Entity - Just choose a name: [camera]
🦥 Handle - Its type separated by a |: [Image|cat]
🐹 Raw - Its type separated by a :: [Image:cat]
🐒 If no type is set, the parent type is used: [|cat] [:cat]

III. Setting the collection type is also done inside []:
🦄 Single - Using the syntax as in **II.**
🐔 Vec - add a <> after the name: [cameras<>]
🐲 HashMap - add a <KeyType> after the name: [cameras<usize>]

// Macro Examples
bycell! {
    Camera: [main] [secondary];
    AudioSource: [|hit] [|shots<>];
    Player: [main] [Scene|models<u8>];
}

IV. Setting Values:
🐑 Use Type::set_..(value) ONCE on (pre-)startup
🦌 The value can be anything implementing its type!

// Setter Examples
Camera::set_main(commands.spawn(..).id());
AudioSource::set_shots([
    assets.load("shot0.ogg"),
    assets.load("shot1.ogg"),
    assets.load("shot3.ogg"),
]);
Player::set_models([
    (5, assets.load("player5.glb")),
    (7, assets.load("player7.glb")),
]);

V. Getting Values:
🐏 Different getters are provided, depending on the collection type!

// Single Getter
Camera::main();            // Cloned
Camera::main_ref();        // Static Reference
// Vec Getters
AudioSource::shots(1);     // Cloned
AudioSource::shots_ref(1); // Static Reference
AudioSource::shots_vec();  // Static Reference to Vec
// HashMap Getters
Player::models(&5);        // Cloned
Player::models_ref(&5);    // Static Reference
Player::models_map();      // Static Reference to HashMap

VI. Mutability:
🐝 You can make any of those mutable by adding a mut before the name
🦞 Only use this if you can avoid race conditions
🦧 One idea is to mutate something on state change!

// Macro Examples
bycell! {
    Camera: [mut main] [mut secondary];
    AudioSource: [|mut hit] [|mut shots<>];
    Player: [mut main] [Scene|mut models<u8>];
}

License

Licensed under either of Apache License, Version 2.0 or MIT license at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~2MB
~25K SLoC