How to add the cooldown

magwiz

New member
Joined
Jan 12, 2024
Messages
19
How to add the cooldown? Like making the player cannot move for a while (it would be nice if I can set the time in milliseconds or seconds).


Here is the code. I don't think the player is stopping during currentCoolDown is lesser than coolDownTime. Probably the order problem?
Cerberus:
Method OnUpdate()
    
        oldTime = time
        time = Millisecs()
        frameTime = Float(time - oldTime) / 1000.0
        moveSpeed = frameTime * 5.5
        rotSpeed = frameTime * 260.0
        
        coolDownTime = frameTime * 6.5
        
        If isCoolDown
        Return 0
        EndIf
        
        If currentMovement > 0.0 Or KeyDown(KEY_UP)
    
            currentMovement += Abs(dirX * moveSpeed) + Abs(dirY * moveSpeed)
            
            '//Do after moved.
            If currentMovement > 1.0
            
                currentCooldownTime = 0
                Print "Step made"
                moveSpeed -= (currentMovement - 1.0)
                currentMovement = 0.0
                
                Repeat
                    currentCooldownTime += frameTime * 0.1
                    isCoolDown = True
                    Until currentCooldownTime > coolDownTime
                
                    isCoolDown = False
                currentCooldownTime = 0
                Print "End cooldown"
            EndIf
            
            If worldMap[WorldIndex(posX + (dirX * moveSpeed), posY)] = False
            posX = posX + dirX * moveSpeed
            EndIf
                    
            If worldMap[WorldIndex(posX, posY + (dirY * moveSpeed))] = False
            posY = posY + dirY * moveSpeed
            EndIf
                
        EndIf
End
 
The main problem I see with your code is that you are stopping the whole game until your cooldownTime is passed.
Here is a simple code that shows the workings of OnCreate(), OnUpdate() and OnRender() with a simple timer checking on every frame if the time has passed.
Cerberus:
Strict
Import mojo

Function Main:Int()
    New Game()
    Return 0
End Function

Class Game Extends App
  
    Field startTime:Int
    Field currentTime:Int
    Field coolDownTime:Int = 5 * 1000  ' Sets the cooldown to 5 secs.
  
    Method OnCreate:Int()
        Print "OnCreate()"
        SetUpdateRate(1)
        startTime = Millisecs()
        Return 0
    End
  
    Method OnUpdate:Int()
        currentTime = Millisecs()
        Print "OnUpdate currentTime: " + currentTime
        If currentTime > startTime + coolDownTime
            Print "**** coolDownTime has passed! ****"
        EndIf
        Return 0
    End
  
    Method OnRender:Int()
        Print "OnRender"
        Cls(50,50,50)
        Return 0
    End
  
End
 
Does the time depend on the value of SetUpdateRate in the code? Though I know this is a simple example, I've never heard the game that its fps set to 1 for cooldown.
 
No, you can set it to any value and the timer is still working. It is simply much easier to understand the output in the console if it is not filled instantly with those logs.
 
Cerberus:
                Repeat
                    currentCooldownTime += frameTime * 0.1
                    isCoolDown = True
                Until currentCooldownTime > coolDownTime

I read your code more carefully and you are not stopping code execution here, but you are also not causing any substantial delay with it as you would have expected. This loop just adds up the frameTime until it hits the cooldownTime value and then it continues after that. This takes probaby less than a millisecond to do. What you have to do is to compare from one call of OnUpdate to the next to see how much time has passed. For that you need a variable that is keeping its value from one OnUpdate() the next. A good way to do this is by using a Field in the surrounding class like shown in my example.
 
Cerberus:
                Repeat
                    currentCooldownTime += frameTime * 0.1
                    isCoolDown = True
                Until currentCooldownTime > coolDownTime

I read your code more carefully and you are not stopping code execution here, but you are also not causing any substantial delay with it as you would have expected. This loop just adds up the frameTime until it hits the cooldownTime value and then it continues after that. This takes probaby less than a millisecond to do. What you have to do is to compare from one call of OnUpdate to the next to see how much time has passed. For that you need a variable that is keeping its value from one OnUpdate() the next. A good way to do this is by using a Field in the surrounding class like shown in my example.
I understand what I was doing. I'll try it, thanks again!
 
Or you can set cooldownRemaining to a value and decrement it each frame - then you just have to test whether it's less than zero.
 
@magwiz You chose a compact but fairly complex example as a first project to tinker with Cerberus X. From time to time I also have difficulties with some basic things in larger projects and what I always get back to is making a simple test code that does't do anything else than solving one single problem.
If you need some help getting your cooldown into the project just show what you came up with so far and I can help you with it.
 
Back
Top Bottom