If you try to write a game for Mini Micro, you'll eventually find the need to write timer code. Perhaps you want the player to shoot projectiles of some sort, but you want to limit the rate of fire. Perhaps you want a bomb to explode after 60 seconds. No matter the case, a timer is going to be what you need if you're trying to do things after a set amount of time.
The essence of a timer is simple. You can do it by storing the amount of time left in the timer, and then subtracting the amount of time passed with each frame from that. This requirement of getting the time passed with each frame means you require a game loop, which I wrote about already.
I wrote a Timer class in my MiniScript library bclib. This has proven to be very useful to me during my two recent game jams.
Example Usage
A simple use of it would be as follows:
import "bclib"
onTimeout = function(timer)
print "Hello world!"
engine.quit
end function
timer = bclib.Timer.Create(5, @onTimeout, true)
engine = bclib.Engine.Create(timer)
engine.run
This program waits 5 seconds and then prints "Hello world!" to the screen. Simple!
The arguments to Timer.Create
in this example are simple:
- The amount of time in seconds before the callback function should be called.
- A reference to the callback function, or a map that contains a function called
onTimeout
. This is what will be called when the timer goes off. - Whether or not the timer is considered started. We want it active right away.
There are other options as well, but this is as simple as it gets.
As for the last two lines, Engine
merely supplies the mechanism for the game loop, as I wrote in the article previously mentioned. Our callback function quits the program by calling engine.quit
.
By the way, you don't need to use Engine
in order to use Timer
, but it does make your life easier in my opinion. Otherwise, you need to call Timer.tick
on every frame yourself.
Another Example
Let's say you have a class called Fighterjet that can fire missiles. You want to have a 5 second cooldown between missile shots for balancing. How might that look?
Fighterjet = {}
Fighterjet.Create = function
instance = new self
instance.canFireMissile = true
instance.timerCooldownMissile = bclib.Timer.Create(5, @instance)
return instance
end function
Fighterjet.fire = function(self)
if self.canFireMissile then
self.canFireMissile = false
self.timerCooldownMissile.start
// Fire missile code goes here
end if
end function
Fighterjet.onTimeout = function(self, timer)
canFireMissile = true
end function
Fighterjet.tick = function(self, delta)
self.timerCooldownMissile.tick delta
end function
Let's assume you have an external mechanic that calls Fighterjet.fire
on your Fighterjet instance when the player attempts to fire a missile. Let's further assume that you have an external source (such as Engine
) calling Fighterjet.tick
every frame.
In this case, we have a variable, canFireMissile
that determines if the player can fire the missile. We originally have it set to true. When the player fires, the variable is set to false, and the timer is started. The player is unable to fire another missile until the timer goes off, and the Fighterjet.onTimeout
function is called.
As long as Fighterjet.tick
is being called every frame, this design should be fairly solid.
Conclusion
Timers are essential for many games, if not most. You should definitely have a Timer class in your arsenal of coding tricks. The library I'm working on is still under development, but feel free to check out the Timer page on the Github wiki for the latest API documentation.
Top comments (0)