• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Particle system depth sorting

mX273

Member
Hi guys

I'm working on a game where each instance uses the formula depth = -y for depth sorting.
I would like to emmit or create some particles on top of all that instances using a praticle system.
How can i solve this (that the particles appear on the correct depth). Should I create a particle system for each instance, and
permanently change its depth (part_system_depth)?
Does the particle system has to be global?
Can I solve this with a single particle system as well?

Thank you very much!

kind regards

mx273
 

obscene

Member
This is what I do. A ton of particle systems on screen at a time but GM seems to be pretty good at handling them.

Create global particle system
Code:
global.part_system=0;
for (var yy=0; yy<=room_height; yy+=1)
    {
    global.part_system[yy]=part_system_create();
    part_system_depth(global.part_system[yy],-yy);
    }
Create particles
Code:
gml_pragma("forceinline");
/// @function scr_global_particles_create
/// @param x
/// @param y
/// @param parttype
/// @param number

var xx=argument0;
var yy=argument1;
yy=clamp(yy,0,room_height); // You may not need this part but I've got slop to deal with
var parttype=argument2;
var number=argument3;
var ps=global.part_system[yy];
part_particles_create(ps,xx,yy,parttype,number);
Destroy global particle system
Code:
for (var yy=0; yy<=room_height; yy+=1)
    {
    part_system_destroy(global.part_system[yy]);
    }
This will work pretty well as long as you don't need particles to move between depths, in which case you'll need custom objects instead.
 
Last edited:

Nocturne

Friendly Tyrant
Forum Staff
Admin
This is what I do. A ton of particle systems on screen at a time but GM seems to be pretty good at handling them.
Just to clarify this point... having multiple systems existing should be fine, simply because a system only takes up memory once created, and has pretty much zero processing requirements unless it's being used. So you can have a load of them in your game, and only using one or two at a time shouldn't create any more issues than using a single particle system.
 

mX273

Member
does the particle system always has to be global? In every example I found it is global. A particle system stored in an instance variable doesnt seem to work...
is my guess correct?
 
No, particle systems do not have to be global at all. But if they're an instance variable you'll have to treat them as you would any other instance variable (i.e. only accessible within the instance itself, or in a with statement involving the instance, or with dot notation thingy (instance.part_system_name)). And if you're making it a local variable (var) then you're not doing it right.
 

mX273

Member
So, wouldn’t it be the best way to create the particle system in the create event of the specific instance and store it in an instance variable “particleSystem”.
Then in the step event I change it’s depth: part_system_depth(particleSystem, -y) and burst some particles.

(I mean, wouldn’t it be better this way, instead of creating hundreds of particle systems with each on its own depth like in the code from obscene)?
 
No, I think @obscene's code would be more performant. This is because the systems, once created, use very little processing power unless they are being used (as @Nocturne pointed out). So creating a lot of systems and then leaving them alone until something actually uses them will use less processing power per step than constantly changing a particle systems depth each step. Having said that, 2 notes to consider:

1. Is this an example of premature optimisation? I would, in this circumstance, worry about it a lot less until I actually hit performance problems.

2. This is an example of easily testable material. You have the code for both, so rig up a little side-project where you can alternate between the two ways of doing it (probs with a state machine, but whatever) and run some tests, see which is more performant yourself. It's unlikely that someone automatically knows the exact cost/benefit for this type of thing off the top of their head, so you're getting educated guesses. If it's really important to you, then benchmarking it yourself is the way to go.
 

obscene

Member
The reason I do it like I did is I have hundreds of objects making particles at different ys and depths at the same time so its easier to just have a system for every depth and one script to make use of it. But also consider that if you use one system and change the depth on the fly all the particles already made will change depth too, which could look bad.
 
Top