I was hoping to get some feedback on an idea.
Sorry it's kind of long.
What I'm trying to build:
I am interested in making a custom particle system that uses sprite objects as the particles.
Reason being, I'd like to have access to rotation, animation, collisions, and possibly other complex behavior on a particle-by-particle basis.
I'm looking to build this in events, rather than making a new plugin.
Ultimately what I want is a single emitter object ("ParticleEmitter"), such that I could make an instance of it for every particle effect I need. I would also have several different sprite objects each serving as a particle type, (e.g. "ParticleSnow", "ParticleFire", "ParticleRain", etc).
The emitter object would have an instance variable that stores the name string of the particle object it should spawn.
And this is where things get tricky. As far as I know, in C2, there's no generalized way to use that name string to spawn an object. Every single object type must have its instances explicitly spawned with event code dedicated to that object in particular.
"Create by name" workaround:
So, I have some thoughts on how I might work around this.
For each Particle object (snow, fire, etc), I can use the Function object to create a "constructor" function of sorts. The name of the function will be the name of the object to be spawned, (maybe with a prefix like "spawn_").
With this method, I still have to create one constructor function for each particle object, but now I get to store the name of the item to be spawned as a string in the emitter. The other benefit, is that if I eventually have 30 different types of particles, I don't need to have an event with 30 switches that runs once for every emitter in the game, and does that every game tick. The function object allows me to "point" only to the code I want to execute, and only for the particles I need to spawn.
Initializing the new particle:
I will still need a way of initializing the particle object I just spawned, so that I can populate its instance variables with information from the emitter, (e.g. Lifetime, speed, opacity, etc).
I could do this initialization within the constructor function event, but I would need to know which emitter is the particle's parent, and as far as I know I can't preserve picked objects through a function call. So, I was thinking that, inside the event that calls the constructor function, I could pass the parent emitter's UID as a function parameter. Then, once I arrive inside the on-function event, I can use that function parameter to pick the parent emitter by UID. From there I can do the particle's initialization, with the correct emitter having been picked.
It would be great if I could put all the particle objects in a family and then use generalized initialization code to set up the particles using family references, but I'm not sure if I can make that work. I don't yet know enough about how object picking interacts with families and functions.
Does this make sense?
This might take a while to build, so I wanted to double check with the forum to see if there is maybe a simpler solution I'm overlooking.
If you made it this far, thanks for taking the time to read all that.