Added Play, pause, and stop functions to particle effect for more manual control if needed.

This commit is contained in:
Chandler Cox
2023-02-03 11:22:41 -06:00
parent d8a9b699ad
commit baf6048377
2 changed files with 76 additions and 1 deletions

View File

@@ -275,6 +275,30 @@ void ParticleEffect::UpdateSimulation(bool singleFrame)
Particles::UpdateEffect(this); 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() void ParticleEffect::UpdateBounds()
{ {
BoundingBox bounds = BoundingBox::Empty; BoundingBox bounds = BoundingBox::Empty;
@@ -395,6 +419,9 @@ void ParticleEffect::SetParametersOverrides(const Array<ParameterOverride>& valu
void ParticleEffect::Update() void ParticleEffect::Update()
{ {
if (!_play)
return;
// Skip if off-screen // Skip if off-screen
if (!UpdateWhenOffscreen && _lastMinDstSqr >= MAX_Real) if (!UpdateWhenOffscreen && _lastMinDstSqr >= MAX_Real)
return; return;
@@ -417,7 +444,14 @@ void ParticleEffect::Update()
void ParticleEffect::UpdateExecuteInEditor() void ParticleEffect::UpdateExecuteInEditor()
{ {
if (!Editor::IsPlayMode) 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(); Update();
}
} }
#endif #endif
@@ -576,6 +610,7 @@ void ParticleEffect::Serialize(SerializeStream& stream, const void* otherObj)
SERIALIZE(SimulationSpeed); SERIALIZE(SimulationSpeed);
SERIALIZE(UseTimeScale); SERIALIZE(UseTimeScale);
SERIALIZE(IsLooping); SERIALIZE(IsLooping);
SERIALIZE(PlayOnStart);
SERIALIZE(UpdateWhenOffscreen); SERIALIZE(UpdateWhenOffscreen);
SERIALIZE(DrawModes); SERIALIZE(DrawModes);
SERIALIZE(SortOrder); SERIALIZE(SortOrder);
@@ -675,6 +710,7 @@ void ParticleEffect::Deserialize(DeserializeStream& stream, ISerializeModifier*
DESERIALIZE(SimulationSpeed); DESERIALIZE(SimulationSpeed);
DESERIALIZE(UseTimeScale); DESERIALIZE(UseTimeScale);
DESERIALIZE(IsLooping); DESERIALIZE(IsLooping);
DESERIALIZE(PlayOnStart);
DESERIALIZE(UpdateWhenOffscreen); DESERIALIZE(UpdateWhenOffscreen);
DESERIALIZE(DrawModes); DESERIALIZE(DrawModes);
DESERIALIZE(SortOrder); DESERIALIZE(SortOrder);
@@ -685,6 +721,12 @@ void ParticleEffect::Deserialize(DeserializeStream& stream, ISerializeModifier*
} }
} }
void ParticleEffect::BeginPlay(SceneBeginData* data)
{
Actor::BeginPlay(data);
}
void ParticleEffect::EndPlay() void ParticleEffect::EndPlay()
{ {
CacheModifiedParameters(); CacheModifiedParameters();
@@ -706,6 +748,9 @@ void ParticleEffect::OnEnable()
GetScene()->Ticking.Update.AddTickExecuteInEditor<ParticleEffect, &ParticleEffect::UpdateExecuteInEditor>(this); GetScene()->Ticking.Update.AddTickExecuteInEditor<ParticleEffect, &ParticleEffect::UpdateExecuteInEditor>(this);
#endif #endif
if (PlayOnStart)
Play();
// Base // Base
Actor::OnEnable(); Actor::OnEnable();
} }

View File

@@ -184,6 +184,7 @@ private:
uint32 _parametersVersion = 0; // Version number for _parameters to be in sync with Instance.ParametersVersion uint32 _parametersVersion = 0; // Version number for _parameters to be in sync with Instance.ParametersVersion
Array<ParticleEffectParameter> _parameters; // Cached for scripting API Array<ParticleEffectParameter> _parameters; // Cached for scripting API
Array<ParameterOverride> _parametersOverrides; // Cached parameter modifications to be applied to the parameters Array<ParameterOverride> _parametersOverrides; // Cached parameter modifications to be applied to the parameters
bool _play = false;
public: public:
/// <summary> /// <summary>
@@ -235,9 +236,21 @@ public:
bool IsLooping = true; bool IsLooping = true;
/// <summary> /// <summary>
/// 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.
/// </summary> /// </summary>
API_FIELD(Attributes="EditorDisplay(\"Particle Effect\"), DefaultValue(true), EditorOrder(60)") API_FIELD(Attributes="EditorDisplay(\"Particle Effect\"), DefaultValue(true), EditorOrder(60)")
bool PlayOnStart = true;
/// <summary>
/// If true, the particle effect is playing.
/// </summary>
API_FIELD()
bool IsPlaying = false;
/// <summary>
/// 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.
/// </summary>
API_FIELD(Attributes="EditorDisplay(\"Particle Effect\"), DefaultValue(true), EditorOrder(70)")
bool UpdateWhenOffscreen = true; bool UpdateWhenOffscreen = true;
/// <summary> /// <summary>
@@ -337,6 +350,22 @@ public:
/// <param name="singleFrame">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.</param> /// <param name="singleFrame">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.</param>
API_FUNCTION() void UpdateSimulation(bool singleFrame = false); API_FUNCTION() void UpdateSimulation(bool singleFrame = false);
/// <summary>
/// Plays the simulation.
/// </summary>
/// /// <param name="reset">If true, the simulation will be reset</param>
API_FUNCTION() void Play(bool reset = true);
/// <summary>
/// Pauses the simulation.
/// </summary>
API_FUNCTION() void Pause();
/// <summary>
/// Stops and resets the simulation.
/// </summary>
API_FUNCTION() void Stop();
/// <summary> /// <summary>
/// Updates the actor bounds. /// Updates the actor bounds.
/// </summary> /// </summary>
@@ -389,6 +418,7 @@ public:
protected: protected:
// [Actor] // [Actor]
void BeginPlay(SceneBeginData* data) override;
void EndPlay() override; void EndPlay() override;
void OnEnable() override; void OnEnable() override;
void OnDisable() override; void OnDisable() override;