Introduction: Gamemaker: Making Cooldowns for Abilities
Hey guys! (and girls). It's time to make some cooldowns in your game! In this tutorial, you will learn how to make cooldowns, show cooldowns, and add a timer to your cooldown!
Step 1: Creating Objects
So before we make an all powerful mage killing everyone with fireballs, we need to make a mage and his/her fireball. So go on and create your mage and fireball, with the mage having movement commands. The fireball doesn't have to have anything in it. Also, create a blue square decently small, and another square the same size, with a picture of the fireball.
Step 2: Programming the Fireball to Shoot
What we're going to do here is to make the fireball shoot, but only when the cdActive block is showing. Essentially, this gives the block a cooldown, saying: you can't shoot unless it is not on cooldown. So, first, we make a "when this mage is created" command, and in it, we put "set var_fireball to 1". This is saying that you can shoot when he is created. Next, what we are going to do is the event "Press space" we are going to say "if var_fireball" is equal to 1, then we will set it equal to 0, (making it go on "cooldown"). Then, we are going to make an alarm for 150 steps, equivalent to 5 seconds. When this alarm triggers, we are going to set var_fireball equal to 1, so the player can use fireball again. Finally, we will create a moving instance of object fireball at 0, 0 relative, at direction 'direction'. This will cause the fireball to move in the direction you're facing.
This should all be programmed into the player, aka the mage. Now, we can shoot, giving a limit on how often he can use fireball, every 5 seconds. However, there is a problem. The person playing the game can't see when it is on cooldown!
Step 3: Making Cooldown Blocks Work
Ok. So, now we want the player to be aware of when the ability is active. So, we are going to add the picture of the fireball into the room. Then, we are going to say: when I press space, I want to create an instance of the blue square at the picture of the fireball's coordinates, and then destroy instance of the fireball picture. Then, when the alarm happens, we want to do the opposite. Create the picture of fireball at blue square coordinates, then destroy blue square. This will ensure that whenever the ability is up, the picture of fireball will be showing. When it is not up, the blue square will be showing.
Now, we just need to show the seconds of cooldown that are remaining.
Step 4: Adding a Timer
So, let's now allow the player to see exactly how many seconds of the ability is left. First, we are going to create some numbers that will be objects. Make sure the sprites are all the same size and centered. This is important. Now, change the depth of the blue block to 100.
Next, we are going to add one more command to the player "space" event, we are going to say "whenever I press space, if fireball = 1, I am going to create an instance of object 5 at the blue block's coordinates." This should be at the end of the if then statement, still surrounded by those green arrow things.
Now, in every number block except obj_1, say "when I am created, set alarm 1 for 30 steps". When alarm 1 happens, "create instance of (next number ex. if it were number 4 that was setting the alarm, create number 3) at coordinates of number that is setting the alarm. Then, destroy this instance."
For number block 1, simply take out the create instance, and only put delete whenever alarm 1 happens.
Now, test it out!
We have a be nice policy.
Please be positive and constructive.