Postby Emral » Mon Jan 20, 2020 1:10 am
Here's step-by-step how most people do this:
1) Make an empty table which you will use for all emitters that have been spawned. This list will be responsible for drawing the emitters. People don't use NPC code for drawing emitters because when the NPC dies, that just means all the particles will also die instantly as they no longer get drawn.
2) In onDraw, loop over the list and call :Draw(priority) on every emitter to make sure it draws.
3) Time to populate the list! In your for loop over your NPCs, wrap the NPC in pnpc and then, if v.data.emitter is nil, set it to a new emitter of the file you wish to use.
4) Remember to call v.data.emitter:Attach(v) to make the emitter move with the NPC, and to insert v.data.emitter into the emitter table for drawing.
For the last part, there are two ways to go about it. You have to stop spawning particles when the NPCs die.
5a) Set the rate of your emitter to 0 and manually spawn particles in the NPC tick function using v.data.emitter:Emit(amount).
5b) Assign v.data.emitter.enabled = false when the NPC dies in onNPCKill