-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Description
Background
Background
At the time of writing the Bevy version is 0.6.1.
Here is the implementation of HandleId::random()
:
bevy/crates/bevy_asset/src/handle.rs
Lines 53 to 55 in 544b6df
pub fn random<T: Asset>() -> Self { | |
HandleId::Id(T::TYPE_UUID, rand::random()) | |
} |
Here is where the method is used in bevy_asset
:
bevy/crates/bevy_asset/src/assets.rs
Lines 77 to 88 in 544b6df
/// Adds an asset to the collection, returning a Strong handle to that asset. | |
/// | |
/// # Events | |
/// * [`AssetEvent::Created`] | |
pub fn add(&mut self, asset: T) -> Handle<T> { | |
let id = HandleId::random::<T>(); | |
self.assets.insert(id, asset); | |
self.events.send(AssetEvent::Created { | |
handle: Handle::weak(id), | |
}); | |
self.get_handle(id) | |
} |
I use this method manually to immediately return a handle to an asset that I will generate asynchronously, and then later use Assets::set_untracked
to place the generated asset into the collection.
What problem does this solve or what need does it fill?
Problem: It's possible (although unlikely) for HandleId::random()
to produce a HandleId
that is already in use by an existing asset in the Assets
collection. This would result in overwriting the existing asset rather than adding a new one. This would be confusing for the user and very difficult to root-cause (and nearly impossible to reproduce).
What solution would you like?
Require users to provide a &Assets<T>
reference so that collisions can be detected and the dice can be re-rolled:
fn random<T: TypeUuid>(assets: &Assets<T>) -> Self {
loop {
let handle_id = HandleId::Id(T::TYPE_UUID, rand::random());
if !assets.contains(handle_id) {
return handle_id;
}
}
}
What alternative(s) have you considered?
Implement the suggestion above, but do so as an additional method and call it something like random_unique()
.
Additional context
We may well decide that the likelihood of this is so low that it's not worth bothering.
See also a short Discord conversation here: https://discord.com/channels/691052431525675048/749332104487108618/944733810577264720