Timelines are a great way to make something happen in a predefined order and at a certain time. You can set it up to execute any code you wish, hence you can make a multitude of actions happen when you wish. As the set up uses only a single asset, editing and changing it can be done quickly. You could use a timeline to
Spawn enemies at set times.
Create a sequence of effects.
Spawn health packs.
Fire a spread of bullets.
Creating a Timeline
You can create a Timeline in the same way you would any other asset, as shown in Figure 26-1.
You can then name your timeline, as shown in Figure 26-2.
You can now click Add to create what is known as Moment. Click Add and edit the Moment to 60, as shown in Figure 26-3. This value tells the program when it should execute the code. At a default game speed of 60, this code would execute after one second, a value of 600 would be run at 10 seconds.
You can then put code into the moment, just as you would with any code, for example:
/// @description Create a firework
effect_create_above(ef_firework,room_width/2,room_height/2,5,c_red);
which after one second would create a firework in the middle of the room.
Your Timeline would then look like that shown in Figure 26-4.
You can also add/create other moments in the same way, for example, as shown in Figure 26-5.
Make a Timeline Play
You can make the timeline start by using
/// @description Start timeline
timeline_index = timeline_example;
timeline_position = 0;
timeline_running = true;
timeline_loop=true;
which would start the timeline and repeat until told otherwise.
The final line of the previous code can be changed to the following, so it plays through just once:
timeline_loop=false;
where setting:
timeline_running = false;
would stop the timeline.
If you have it on an endless loop and require a pause, you can add a moment like that shown in Figure 26-6, which will force the timeline to wait until that moment before looping:
Basic Projects
- A)
Create a firework display using effects. Add audio that plays so it matches the effect. Make the display loop through five times and then stop.
Advanced Projects
- B)
Make an enemy patrol in a square pattern, shoot a melee of bullets each time it changes direction.
Useful Functions
You can create a timeline with:
example=timeline_add();
which would set the timeline to execute the given script after two seconds.
You’ll also find these useful:
timeline_size()
timeline_max_moment()
timeline_moment_add_script()
Summary
You now know how to make things happen at certain times in an orderly pattern.