From baf604837704b4e62d5eef17b3417d222a5bdec7 Mon Sep 17 00:00:00 2001 From: Chandler Cox Date: Fri, 3 Feb 2023 11:22:41 -0600 Subject: [PATCH 1/4] Added Play, pause, and stop functions to particle effect for more manual control if needed. --- Source/Engine/Particles/ParticleEffect.cpp | 45 ++++++++++++++++++++++ Source/Engine/Particles/ParticleEffect.h | 32 ++++++++++++++- 2 files changed, 76 insertions(+), 1 deletion(-) diff --git a/Source/Engine/Particles/ParticleEffect.cpp b/Source/Engine/Particles/ParticleEffect.cpp index 4b7923439..6b7346de3 100644 --- a/Source/Engine/Particles/ParticleEffect.cpp +++ b/Source/Engine/Particles/ParticleEffect.cpp @@ -275,6 +275,30 @@ void ParticleEffect::UpdateSimulation(bool singleFrame) Particles::UpdateEffect(this); } +void ParticleEffect::Play(bool reset) +{ + _play = true; + IsPlaying = true; + + if (reset) + ResetSimulation(); + else + UpdateSimulation(true); +} + +void ParticleEffect::Pause() +{ + _play = false; + IsPlaying = false; +} + +void ParticleEffect::Stop() +{ + _play = false; + IsPlaying = false; + ResetSimulation(); +} + void ParticleEffect::UpdateBounds() { BoundingBox bounds = BoundingBox::Empty; @@ -395,6 +419,9 @@ void ParticleEffect::SetParametersOverrides(const Array& valu void ParticleEffect::Update() { + if (!_play) + return; + // Skip if off-screen if (!UpdateWhenOffscreen && _lastMinDstSqr >= MAX_Real) return; @@ -417,7 +444,14 @@ void ParticleEffect::Update() void ParticleEffect::UpdateExecuteInEditor() { if (!Editor::IsPlayMode) + { + // Always Play in editor while not playing. + // Could be useful to have a GUI to change this state + if (!_play) + _play = true; + Update(); + } } #endif @@ -576,6 +610,7 @@ void ParticleEffect::Serialize(SerializeStream& stream, const void* otherObj) SERIALIZE(SimulationSpeed); SERIALIZE(UseTimeScale); SERIALIZE(IsLooping); + SERIALIZE(PlayOnStart); SERIALIZE(UpdateWhenOffscreen); SERIALIZE(DrawModes); SERIALIZE(SortOrder); @@ -675,6 +710,7 @@ void ParticleEffect::Deserialize(DeserializeStream& stream, ISerializeModifier* DESERIALIZE(SimulationSpeed); DESERIALIZE(UseTimeScale); DESERIALIZE(IsLooping); + DESERIALIZE(PlayOnStart); DESERIALIZE(UpdateWhenOffscreen); DESERIALIZE(DrawModes); DESERIALIZE(SortOrder); @@ -685,6 +721,12 @@ void ParticleEffect::Deserialize(DeserializeStream& stream, ISerializeModifier* } } +void ParticleEffect::BeginPlay(SceneBeginData* data) +{ + + Actor::BeginPlay(data); +} + void ParticleEffect::EndPlay() { CacheModifiedParameters(); @@ -706,6 +748,9 @@ void ParticleEffect::OnEnable() GetScene()->Ticking.Update.AddTickExecuteInEditor(this); #endif + if (PlayOnStart) + Play(); + // Base Actor::OnEnable(); } diff --git a/Source/Engine/Particles/ParticleEffect.h b/Source/Engine/Particles/ParticleEffect.h index 3487a35f4..a88628ccd 100644 --- a/Source/Engine/Particles/ParticleEffect.h +++ b/Source/Engine/Particles/ParticleEffect.h @@ -184,6 +184,7 @@ private: uint32 _parametersVersion = 0; // Version number for _parameters to be in sync with Instance.ParametersVersion Array _parameters; // Cached for scripting API Array _parametersOverrides; // Cached parameter modifications to be applied to the parameters + bool _play = false; public: /// @@ -235,9 +236,21 @@ public: bool IsLooping = true; /// - /// If true, the particle simulation will be updated even when an actor cannot be seen by any camera. Otherwise, the simulation will stop running when the actor is off-screen. + /// Determines whether the particle effect should play on start. /// API_FIELD(Attributes="EditorDisplay(\"Particle Effect\"), DefaultValue(true), EditorOrder(60)") + bool PlayOnStart = true; + + /// + /// If true, the particle effect is playing. + /// + API_FIELD() + bool IsPlaying = false; + + /// + /// If true, the particle simulation will be updated even when an actor cannot be seen by any camera. Otherwise, the simulation will stop running when the actor is off-screen. + /// + API_FIELD(Attributes="EditorDisplay(\"Particle Effect\"), DefaultValue(true), EditorOrder(70)") bool UpdateWhenOffscreen = true; /// @@ -337,6 +350,22 @@ public: /// True if update animation by a single frame only (time time since last engine update), otherwise will update simulation with delta time since last update. API_FUNCTION() void UpdateSimulation(bool singleFrame = false); + /// + /// Plays the simulation. + /// + /// /// If true, the simulation will be reset + API_FUNCTION() void Play(bool reset = true); + + /// + /// Pauses the simulation. + /// + API_FUNCTION() void Pause(); + + /// + /// Stops and resets the simulation. + /// + API_FUNCTION() void Stop(); + /// /// Updates the actor bounds. /// @@ -389,6 +418,7 @@ public: protected: // [Actor] + void BeginPlay(SceneBeginData* data) override; void EndPlay() override; void OnEnable() override; void OnDisable() override; From 2d98a46d94b2af98d4c389d72a52d5f09400540d Mon Sep 17 00:00:00 2001 From: Chandler Cox Date: Fri, 3 Feb 2023 11:27:11 -0600 Subject: [PATCH 2/4] Clean up code --- Source/Engine/Particles/ParticleEffect.cpp | 6 ------ Source/Engine/Particles/ParticleEffect.h | 1 - 2 files changed, 7 deletions(-) diff --git a/Source/Engine/Particles/ParticleEffect.cpp b/Source/Engine/Particles/ParticleEffect.cpp index 6b7346de3..2e76ea65b 100644 --- a/Source/Engine/Particles/ParticleEffect.cpp +++ b/Source/Engine/Particles/ParticleEffect.cpp @@ -721,12 +721,6 @@ void ParticleEffect::Deserialize(DeserializeStream& stream, ISerializeModifier* } } -void ParticleEffect::BeginPlay(SceneBeginData* data) -{ - - Actor::BeginPlay(data); -} - void ParticleEffect::EndPlay() { CacheModifiedParameters(); diff --git a/Source/Engine/Particles/ParticleEffect.h b/Source/Engine/Particles/ParticleEffect.h index a88628ccd..832327935 100644 --- a/Source/Engine/Particles/ParticleEffect.h +++ b/Source/Engine/Particles/ParticleEffect.h @@ -418,7 +418,6 @@ public: protected: // [Actor] - void BeginPlay(SceneBeginData* data) override; void EndPlay() override; void OnEnable() override; void OnDisable() override; From dbee9f681652f059640a02dc1ac9bab9ef82a5df Mon Sep 17 00:00:00 2001 From: Chandler Cox Date: Fri, 3 Feb 2023 12:11:18 -0600 Subject: [PATCH 3/4] Small fix --- Source/Engine/Particles/ParticleEffect.cpp | 11 ++++++++--- Source/Engine/Particles/ParticleEffect.h | 12 ++++++------ 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/Source/Engine/Particles/ParticleEffect.cpp b/Source/Engine/Particles/ParticleEffect.cpp index 2e76ea65b..04fd6eedb 100644 --- a/Source/Engine/Particles/ParticleEffect.cpp +++ b/Source/Engine/Particles/ParticleEffect.cpp @@ -253,6 +253,11 @@ int32 ParticleEffect::GetParticlesCount() const return Instance.GetParticlesCount(); } +bool ParticleEffect::GetIsPlaying() const +{ + return _isPlaying; +} + void ParticleEffect::ResetSimulation() { Instance.ClearState(); @@ -278,7 +283,7 @@ void ParticleEffect::UpdateSimulation(bool singleFrame) void ParticleEffect::Play(bool reset) { _play = true; - IsPlaying = true; + _isPlaying = true; if (reset) ResetSimulation(); @@ -289,13 +294,13 @@ void ParticleEffect::Play(bool reset) void ParticleEffect::Pause() { _play = false; - IsPlaying = false; + _isPlaying = false; } void ParticleEffect::Stop() { _play = false; - IsPlaying = false; + _isPlaying = false; ResetSimulation(); } diff --git a/Source/Engine/Particles/ParticleEffect.h b/Source/Engine/Particles/ParticleEffect.h index 832327935..5a4bae771 100644 --- a/Source/Engine/Particles/ParticleEffect.h +++ b/Source/Engine/Particles/ParticleEffect.h @@ -185,6 +185,7 @@ private: Array _parameters; // Cached for scripting API Array _parametersOverrides; // Cached parameter modifications to be applied to the parameters bool _play = false; + bool _isPlaying = false; public: /// @@ -241,12 +242,6 @@ public: API_FIELD(Attributes="EditorDisplay(\"Particle Effect\"), DefaultValue(true), EditorOrder(60)") bool PlayOnStart = true; - /// - /// If true, the particle effect is playing. - /// - API_FIELD() - bool IsPlaying = false; - /// /// If true, the particle simulation will be updated even when an actor cannot be seen by any camera. Otherwise, the simulation will stop running when the actor is off-screen. /// @@ -339,6 +334,11 @@ public: /// API_PROPERTY() int32 GetParticlesCount() const; + /// + /// Gets whether or not the particle effect is playing. + /// + API_PROPERTY(Attributes="NoSerialize, HideInEditor") bool GetIsPlaying() const; + /// /// Resets the particles simulation state (clears the instance state data but preserves the instance parameters values). /// From f3b2011feff8c82742d2041f7a5055c9f330fc9f Mon Sep 17 00:00:00 2001 From: Chandler Cox Date: Sat, 4 Feb 2023 08:25:07 -0600 Subject: [PATCH 4/4] Small fix --- Source/Engine/Particles/ParticleEffect.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Source/Engine/Particles/ParticleEffect.cpp b/Source/Engine/Particles/ParticleEffect.cpp index 04fd6eedb..651ff719b 100644 --- a/Source/Engine/Particles/ParticleEffect.cpp +++ b/Source/Engine/Particles/ParticleEffect.cpp @@ -453,7 +453,10 @@ void ParticleEffect::UpdateExecuteInEditor() // Always Play in editor while not playing. // Could be useful to have a GUI to change this state if (!_play) - _play = true; + { + _play = true; + _isPlaying = true; + } Update(); }