Tick Tock

Time is an important concept in games, controlling and constraining player actions so we could have a better game structure.

From time to time, I had implemented a timer like this:

    class timer:
        public start=false
        public time=Time.time

        def Start ():
            record = timer()

        def Update ():
            if state["shoot"]==true:
                if record.start == false: 
                BL=GameObject.Instatiate(bullet, self.transform.position, self.transform.rotation) as GameObject
                record.time=Time.time
                record.start=true
            else:
                if (Time.time-record.time)>cooltime :
                    record.start=false
                    record.time=Time.time
                    state["shoot"]=false

This timer was the original implementation of a custom cooldown clock, but this code is messy and violates the DRY principle, so there has to be a better way to do it.

I took inspiration from the minimalist design of GUI buttons in Unity3D. The buttons return true if clicked, and false if not. So I went and create my own custom timer that would be imported whenever needed, based on that concept.

It returned true whenever the timer could be started and false whenever it was not! Of course, the timer could be customized further if required, but there was not a need yet.

Oh the joys of solving problems.

 
0
Kudos
 
0
Kudos

Now read this

Blogger doppelganger

After a brief stint at svbtle, I guess it would be time to move some of the posts to blogger again. One of the things you’ll notice when writing in svbtle is that it’s more like a private repo of text than a blog, though without anyway... Continue →