Demonstrate how to cache asset handles effectively
- Dominant language
- Rust
- Stars
- 48.2k
- Forks
- 4.8k
- Avg merge
- 3d 16h
- Merged PRs (30d)
- 171
Description
## How can Bevy's documentation be improved?
Strong handles can be retained in system-local storage. This can be quite useful to avoid having to track handles in dedicated resources. None of our examples use this pattern; we should at least demonstrate it in a standalone example.
To show what I mean, here's a snippet of real code that I was working on today.
```rust
#[derive(Bundle)]
struct PingBundle {
ping: Ping,
#[bundle]
mesh_bundle: MaterialMesh2dBundle,
}
impl PingBundle {
fn new(
ping: Ping,
location: WorldCoordinates,
mesh_handle: Mesh2dHandle,
material_handle: Handle,
) -> PingBundle {
PingBundle {
ping,
mesh_bundle: MaterialMesh2dBundle:: {
mesh: mesh_handle,
material: material_handle,
transform: Transform::from_xyz(location.x, location.y, 0.0),
..default()
},
}
}
}
pub fn ping_at_instruction(
cached_mesh_handle: Local>,
cached_material_handle: Local>>,
mut meshes: ResMut>,
mut materials: ResMut>,
action_state: Query<&ActionState>,
cursor_query: Query<&WorldCoordinates, With>,
mut commands: Commands,
) {
if action_state
.single()
.just_pressed(PlayerAction::Instruction)
{
let mesh_handle = match cached_mesh_handle.clone() {
Some(cache) => cache,
None => meshes.add(shape::Circle::new(50.).into()).into(),
};
let material_handle = match cached_material_handle.clone() {
Some(cache) => cache,
None => materials.add(ColorMaterial::from(Color::PURPLE)),
};
let &location = cursor_query.single();
commands.spawn_bundle(PingBundle::new(
Ping::default(),
location,
mesh_handle,
material_handle,
));
}
}
```
Contributor guide
Research direction
The issue provides a ping_at_instruction snippet showing Local caches for mesh and material handles, but names no example file or test. Start by locating the standalone examples that demonstrate asset handles and system-local storage. Done means adding an example that clearly demonstrates retaining and reusing strong asset handles in system-local storage.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- documentation
- Issue type
- Documentation
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100