Having trouble starting particle systems from script
I've created an explosion sort of particle system and I've got it to start from a script by attaching the GlobalTime and ExternalTimeController scripts from the Particles template and then making calls to enable those scripts and the particle system scene object when I want the system to begin simulating.
However, if I disable said components and scene object and reenable them all again later in order to re-fire the explosion, it ends up either doing nothing or firing very late. It seems there's likely a time variable not being reset properly but it's not very clear how these scripts GlobalTime and ExternalTimeController are working along with all the variables exposed by the particle system material inspector.
It would be really nice to have a simple particle system component with proper methods for starting a system's simulation, resetting it, etc.
But until that's developed, how should I control the start and restart of a particle system currently?
Thanks!
Hi Frank!
How GlobalTime and ExternalTimeController work with particle system material
You're right, GlobalTime and ExternalTimeController will help you with this.
ExternalTimeController connects with the variables exposed by the particle system inspector where it says
That is: it is looking for a mesh visual in the same SceneObject where the script is added to, accessing its material (the particle system) and modifying it's time (what state the particle system is in).
Above that, we can see that positiveTime, is a variable based on global.controlTime, which is set by the GlobalTime.js
In other words, these two scripts control what state the particle system is in by storing when the lens started (global.controlTime), and then calculating how long time has passed, and finally passing it into the particle material.
It's important to note, that the particle system has a checkbox to tell it to use externalTime rather than its own time.
(from: https://lensstudio.snapchat.com/templates/world/particles/)
(Re)starting material
Currently, in ExternalTimeController, there is a function called resetTime which resets the particle system after a certain time has elapsed. This is how you can restart the particle system. We can repurpose this to reset the particle system on command.
Let's say we want the particle system to restart when the user taps.
What we need to do is to remove the time check if statements and then bind the reset function to the touch event.
Alternatively, we can expose this function to other scripts so that they can call it.
and then in the other script, we can get a reference to this script component / function
Check out the API Reference section here: https://lensstudio.snapchat.com/guides/scripting/scripting-overview/
That's a great idea on particle system component--I will pass that on to the team :)
Can't wait to see what you come up with!
Jon