Tutorial 17: Creating Particle Effects in GameMaker Studio 2
What are particle effects?
Particles are graphic resources that have certain properties which are defined within a particle system. These properties cannot be manipulated directly for individual particles but are changed through the code that is used to define the individual particle and the system that it belongs to. They are very useful for creating beautiful and flashy effects (or subtle and discreet ones!) like explosions, decals, rain, snow, star fields and debris in a game without the CPU overhead that using instances has.
The basic setup for a particle system follows two steps:
- Create a particle system: The particle system is like a container that we will use to hold our different particle types ready for use. We use code to define a series of visual aspects and behaviours for our particles, and then we place them in the “container” so that we can take them out and use them whenever and wherever we need later.
- Create particle types: Particle types are the graphic effect itself. You can have many different types, each with their own range of colours, alphas, sizes and movements, but its important to note that you do not have control over individual particles. You define a series of parameters and the particle will be created to have a random spread of behaviours chosen from them.
Setup a Particle Control Object
- Create an object called objParticleController
- Add a Create event to it with the following code:
//make sure that the layer in your room where you objects are is called Instances global.P_System = part_system_create_layer("Instances", false); //Create a particle type below and define how it works global.Particle1 = part_type_create(); part_type_shape(global.Particle1,pt_shape_pixel); //This defines the particles shape part_type_size(global.Particle1,2,2,1,2); //This is for the size part_type_color1(global.Particle1,c_orange); //This sets its colour part_type_alpha1(global.Particle1,1); //This is its alpha. There are three different codes for this part_type_speed(global.Particle1,0.50,2,-0.10,0); //The particles speed part_type_direction(global.Particle1,0,359,0,20); //The direction part_type_orientation(global.Particle1,0,0,0,0,true); //This changes the rotation of the particle part_type_blend(global.Particle1,1); //This is the blend mode, either additive or normal part_type_life(global.Particle1,5,30); //this is its lifespan in steps
- You can adjust the parameters in the above code for Partilce1 to change the design of the particle you want to create.
Create the particle explosion
- Open your enemy object, objEnemy1 and open the collision event with the player lazer.
Look at the code where you are checking if the enemies health has reached zero.
Place the following statement inside that if condition.
part_particles_create(global.P_System, x, y, global.Particle1, 50); //from the Particle System create a Particle 1 at the x and y co-ordinates of the enemy ship //50 times
See line 14 in the above example above
- Create an Room End event on the player object, objPlayer.
Other -> Room End.
Place the following code into it.
// This is to clean up the particles out of system memory when the game ends part_type_destroy(global.Particle1); part_system_destroy(global.P_System);
- Drag the objParticleController into your start room and game room
- Run your game to test that it works.
You should see an explosion on the enemy ship when it dies.
- You can setup more Particle effects by creating new particle types
Learn more: Watch a video on Particle Effects in Gamemaker Studio 2