Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/main' into NoSTDBevyTasksV2
Browse files Browse the repository at this point in the history
  • Loading branch information
bushrat011899 committed Sep 30, 2024
2 parents f44c9a6 + 5289e18 commit 55d11e0
Show file tree
Hide file tree
Showing 84 changed files with 3,602 additions and 1,624 deletions.
2 changes: 1 addition & 1 deletion .github/ISSUE_TEMPLATE/docs_improvement.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ assignees: ''

Provide a link to the documentation and describe how it could be improved. In what ways is it incomplete, incorrect, or misleading?

If you have suggestions on exactly what the new docs should say, feel free to include them here. Or alternatively, make the changes yourself and [create a pull request](https://bevyengine.org/learn/book/contributing/code/) instead.
If you have suggestions on exactly what the new docs should say, feel free to include them here. Alternatively, make the changes yourself and [create a pull request](https://bevyengine.org/learn/book/contributing/code/) instead.
2 changes: 1 addition & 1 deletion .github/ISSUE_TEMPLATE/feature_request.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name: Feature Request
about: Propose a new feature!
title: ''
labels: C-Enhancement, S-Needs-Triage
labels: C-Feature, S-Needs-Triage
assignees: ''
---

Expand Down
13 changes: 12 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -1146,7 +1146,7 @@ setup = [
"curl",
"-o",
"assets/models/bunny.meshlet_mesh",
"https://raw.githubusercontent.com/JMS55/bevy_meshlet_asset/e3da1533b4c69fb967f233c817e9b0921134d317/bunny.meshlet_mesh",
"https://raw.githubusercontent.com/JMS55/bevy_meshlet_asset/854eb98353ad94aea1104f355fc24dbe4fda679d/bunny.meshlet_mesh",
],
]

Expand Down Expand Up @@ -1892,6 +1892,17 @@ description = "Run systems only when one or multiple conditions are met"
category = "ECS (Entity Component System)"
wasm = false

[[example]]
name = "fallible_params"
path = "examples/ecs/fallible_params.rs"
doc-scrape-examples = true

[package.metadata.example.fallible_params]
name = "Fallible System Parameters"
description = "Systems are skipped if their parameters cannot be acquired"
category = "ECS (Entity Component System)"
wasm = false

[[example]]
name = "startup_system"
path = "examples/ecs/startup_system.rs"
Expand Down
46 changes: 16 additions & 30 deletions crates/bevy_animation/src/keyframes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,17 @@ use core::{
fmt::{self, Debug, Formatter},
};

use bevy_asset::Handle;
use bevy_derive::{Deref, DerefMut};
use bevy_ecs::{
component::Component,
world::{EntityMutExcept, Mut},
};
use bevy_ecs::{component::Component, world::Mut};
use bevy_math::{Quat, Vec3};
use bevy_reflect::{FromReflect, GetTypeRegistration, Reflect, TypePath, Typed};
use bevy_reflect::{FromReflect, Reflect, Reflectable, TypePath};
use bevy_render::mesh::morph::MorphWeights;
use bevy_transform::prelude::Transform;

use crate::{
animatable,
graph::AnimationGraph,
prelude::{Animatable, GetKeyframe},
AnimationEvaluationError, AnimationPlayer, Interpolation,
AnimationEntityMut, AnimationEvaluationError, Interpolation,
};

/// A value on a component that Bevy can animate.
Expand Down Expand Up @@ -74,16 +69,7 @@ pub trait AnimatableProperty: Reflect + TypePath + 'static {
type Component: Component;

/// The type of the property to be animated.
type Property: Animatable
+ FromReflect
+ GetTypeRegistration
+ Reflect
+ TypePath
+ Typed
+ Clone
+ Sync
+ Debug
+ 'static;
type Property: Animatable + FromReflect + Reflectable + Clone + Sync + Debug + 'static;

/// Given a reference to the component, returns a reference to the property.
///
Expand Down Expand Up @@ -154,7 +140,7 @@ pub trait Keyframes: Reflect + Debug + Send + Sync {
fn apply_single_keyframe<'a>(
&self,
transform: Option<Mut<'a, Transform>>,
entity: EntityMutExcept<'a, (Transform, AnimationPlayer, Handle<AnimationGraph>)>,
entity: AnimationEntityMut<'a>,
weight: f32,
) -> Result<(), AnimationEvaluationError>;

Expand Down Expand Up @@ -185,7 +171,7 @@ pub trait Keyframes: Reflect + Debug + Send + Sync {
fn apply_tweened_keyframes<'a>(
&self,
transform: Option<Mut<'a, Transform>>,
entity: EntityMutExcept<'a, (Transform, AnimationPlayer, Handle<AnimationGraph>)>,
entity: AnimationEntityMut<'a>,
interpolation: Interpolation,
step_start: usize,
time: f32,
Expand Down Expand Up @@ -285,7 +271,7 @@ impl Keyframes for TranslationKeyframes {
fn apply_single_keyframe<'a>(
&self,
transform: Option<Mut<'a, Transform>>,
_: EntityMutExcept<'a, (Transform, AnimationPlayer, Handle<AnimationGraph>)>,
_: AnimationEntityMut<'a>,
weight: f32,
) -> Result<(), AnimationEvaluationError> {
let mut component = transform.ok_or_else(|| {
Expand All @@ -301,7 +287,7 @@ impl Keyframes for TranslationKeyframes {
fn apply_tweened_keyframes<'a>(
&self,
transform: Option<Mut<'a, Transform>>,
_: EntityMutExcept<'a, (Transform, AnimationPlayer, Handle<AnimationGraph>)>,
_: AnimationEntityMut<'a>,
interpolation: Interpolation,
step_start: usize,
time: f32,
Expand Down Expand Up @@ -340,7 +326,7 @@ impl Keyframes for ScaleKeyframes {
fn apply_single_keyframe<'a>(
&self,
transform: Option<Mut<'a, Transform>>,
_: EntityMutExcept<'a, (Transform, AnimationPlayer, Handle<AnimationGraph>)>,
_: AnimationEntityMut<'a>,
weight: f32,
) -> Result<(), AnimationEvaluationError> {
let mut component = transform.ok_or_else(|| {
Expand All @@ -356,7 +342,7 @@ impl Keyframes for ScaleKeyframes {
fn apply_tweened_keyframes<'a>(
&self,
transform: Option<Mut<'a, Transform>>,
_: EntityMutExcept<'a, (Transform, AnimationPlayer, Handle<AnimationGraph>)>,
_: AnimationEntityMut<'a>,
interpolation: Interpolation,
step_start: usize,
time: f32,
Expand Down Expand Up @@ -395,7 +381,7 @@ impl Keyframes for RotationKeyframes {
fn apply_single_keyframe<'a>(
&self,
transform: Option<Mut<'a, Transform>>,
_: EntityMutExcept<'a, (Transform, AnimationPlayer, Handle<AnimationGraph>)>,
_: AnimationEntityMut<'a>,
weight: f32,
) -> Result<(), AnimationEvaluationError> {
let mut component = transform.ok_or_else(|| {
Expand All @@ -411,7 +397,7 @@ impl Keyframes for RotationKeyframes {
fn apply_tweened_keyframes<'a>(
&self,
transform: Option<Mut<'a, Transform>>,
_: EntityMutExcept<'a, (Transform, AnimationPlayer, Handle<AnimationGraph>)>,
_: AnimationEntityMut<'a>,
interpolation: Interpolation,
step_start: usize,
time: f32,
Expand Down Expand Up @@ -454,7 +440,7 @@ where
fn apply_single_keyframe<'a>(
&self,
_: Option<Mut<'a, Transform>>,
mut entity: EntityMutExcept<'a, (Transform, AnimationPlayer, Handle<AnimationGraph>)>,
mut entity: AnimationEntityMut<'a>,
weight: f32,
) -> Result<(), AnimationEvaluationError> {
let mut component = entity.get_mut::<P::Component>().ok_or_else(|| {
Expand All @@ -472,7 +458,7 @@ where
fn apply_tweened_keyframes<'a>(
&self,
_: Option<Mut<'a, Transform>>,
mut entity: EntityMutExcept<'a, (Transform, AnimationPlayer, Handle<AnimationGraph>)>,
mut entity: AnimationEntityMut<'a>,
interpolation: Interpolation,
step_start: usize,
time: f32,
Expand Down Expand Up @@ -536,7 +522,7 @@ impl Keyframes for MorphWeightsKeyframes {
fn apply_single_keyframe<'a>(
&self,
_: Option<Mut<'a, Transform>>,
mut entity: EntityMutExcept<'a, (Transform, AnimationPlayer, Handle<AnimationGraph>)>,
mut entity: AnimationEntityMut<'a>,
weight: f32,
) -> Result<(), AnimationEvaluationError> {
let mut dest = entity.get_mut::<MorphWeights>().ok_or_else(|| {
Expand All @@ -555,7 +541,7 @@ impl Keyframes for MorphWeightsKeyframes {
fn apply_tweened_keyframes<'a>(
&self,
_: Option<Mut<'a, Transform>>,
mut entity: EntityMutExcept<'a, (Transform, AnimationPlayer, Handle<AnimationGraph>)>,
mut entity: AnimationEntityMut<'a>,
interpolation: Interpolation,
step_start: usize,
time: f32,
Expand Down
25 changes: 15 additions & 10 deletions crates/bevy_animation/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1094,28 +1094,33 @@ pub fn advance_animations(
});
}

/// A type alias for [`EntityMutExcept`] as used in animation.
pub type AnimationEntityMut<'w> = EntityMutExcept<
'w,
(
AnimationTarget,
Transform,
AnimationPlayer,
Handle<AnimationGraph>,
),
>;

/// A system that modifies animation targets (e.g. bones in a skinned mesh)
/// according to the currently-playing animations.
pub fn animate_targets(
clips: Res<Assets<AnimationClip>>,
graphs: Res<Assets<AnimationGraph>>,
players: Query<(&AnimationPlayer, &Handle<AnimationGraph>)>,
mut targets: Query<(
Option<&mut Transform>,
EntityMutExcept<(Transform, AnimationPlayer, Handle<AnimationGraph>)>,
)>,
mut targets: Query<(&AnimationTarget, Option<&mut Transform>, AnimationEntityMut)>,
) {
// Evaluate all animation targets in parallel.
targets
.par_iter_mut()
.for_each(|(mut transform, mut entity_mut)| {
let Some(&AnimationTarget {
.for_each(|(target, mut transform, mut entity_mut)| {
let &AnimationTarget {
id: target_id,
player: player_id,
}) = entity_mut.get::<AnimationTarget>()
else {
return;
};
} = target;

let (animation_player, animation_graph_id) =
if let Ok((player, graph_handle)) = players.get(player_id) {
Expand Down
21 changes: 9 additions & 12 deletions crates/bevy_ecs/src/change_detection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -543,11 +543,10 @@ impl<'w> From<TicksMut<'w>> for Ticks<'w> {
///
/// If you need a unique mutable borrow, use [`ResMut`] instead.
///
/// # Panics
/// This [`SystemParam`](crate::system::SystemParam) fails validation if resource doesn't exist.
/// This will cause systems that use this parameter to be skipped.
///
/// Panics when used as a [`SystemParameter`](crate::system::SystemParam) if the resource does not exist.
///
/// Use `Option<Res<T>>` instead if the resource might not always exist.
/// Use [`Option<Res<T>>`] instead if the resource might not always exist.
pub struct Res<'w, T: ?Sized + Resource> {
pub(crate) value: &'w T,
pub(crate) ticks: Ticks<'w>,
Expand Down Expand Up @@ -622,11 +621,10 @@ impl_debug!(Res<'w, T>, Resource);
///
/// If you need a shared borrow, use [`Res`] instead.
///
/// # Panics
///
/// Panics when used as a [`SystemParam`](crate::system::SystemParam) if the resource does not exist.
/// This [`SystemParam`](crate::system::SystemParam) fails validation if resource doesn't exist.
/// This will cause systems that use this parameter to be skipped.
///
/// Use `Option<ResMut<T>>` instead if the resource might not always exist.
/// Use [`Option<ResMut<T>>`] instead if the resource might not always exist.
pub struct ResMut<'w, T: ?Sized + Resource> {
pub(crate) value: &'w mut T,
pub(crate) ticks: TicksMut<'w>,
Expand Down Expand Up @@ -684,11 +682,10 @@ impl<'w, T: Resource> From<ResMut<'w, T>> for Mut<'w, T> {
/// the scheduler to instead run the system on the main thread so that it doesn't send the resource
/// over to another thread.
///
/// # Panics
///
/// Panics when used as a `SystemParameter` if the resource does not exist.
/// This [`SystemParam`](crate::system::SystemParam) fails validation if non-send resource doesn't exist.
/// This will cause systems that use this parameter to be skipped.
///
/// Use `Option<NonSendMut<T>>` instead if the resource might not always exist.
/// Use [`Option<NonSendMut<T>>`] instead if the resource might not always exist.
pub struct NonSendMut<'w, T: ?Sized + 'static> {
pub(crate) value: &'w mut T,
pub(crate) ticks: TicksMut<'w>,
Expand Down
3 changes: 2 additions & 1 deletion crates/bevy_ecs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ pub mod prelude {
system::{
Commands, Deferred, EntityCommand, EntityCommands, In, InMut, InRef, IntoSystem, Local,
NonSend, NonSendMut, ParallelCommands, ParamSet, Query, ReadOnlySystem, Res, ResMut,
Resource, System, SystemIn, SystemInput, SystemParamBuilder, SystemParamFunction,
Resource, Single, System, SystemIn, SystemInput, SystemParamBuilder,
SystemParamFunction,
},
world::{
Command, EntityMut, EntityRef, EntityWorldMut, FromWorld, OnAdd, OnInsert, OnRemove,
Expand Down
21 changes: 21 additions & 0 deletions crates/bevy_ecs/src/observer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1190,4 +1190,25 @@ mod tests {
// after the observer's spawn_empty.
world.despawn(ent);
}

#[test]
fn observer_invalid_params() {
#[derive(Event)]
struct EventA;

#[derive(Resource)]
struct ResA;

#[derive(Resource)]
struct ResB;

let mut world = World::new();
// This fails because `ResA` is not present in the world
world.observe(|_: Trigger<EventA>, _: Res<ResA>, mut commands: Commands| {
commands.insert_resource(ResB);
});
world.trigger(EventA);

assert!(world.get_resource::<ResB>().is_none());
}
}
6 changes: 4 additions & 2 deletions crates/bevy_ecs/src/observer/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,8 +374,10 @@ fn observer_system_runner<E: Event, B: Bundle, S: ObserverSystem<E, B>>(
// - system is the same type erased system from above
unsafe {
(*system).update_archetype_component_access(world);
(*system).run_unsafe(trigger, world);
(*system).queue_deferred(world.into_deferred());
if (*system).validate_param_unsafe(world) {
(*system).run_unsafe(trigger, world);
(*system).queue_deferred(world.into_deferred());
}
}
}

Expand Down
Loading

0 comments on commit 55d11e0

Please sign in to comment.