Adding a delay between a key press and applyMovement

I am using Riyuzakisan’s MouseMove to control the dynamic movement of my avatar. I am trying to add a delay between the key press and the applyMovement. When my character jumps, it is an instant reaction and the animation is way out of sync. I want the applyMovement to happen 15 frames after the key press. Does anyone know how to do this?

The applyMovement happens here;

    def state_onGround(self):
        controls = self.core.controls
        
        ### Adjust Speed and Damping ###
        if controls.crouch:
            self.speed *= 0.3
            self.damping = 0.85
        else:
            if controls.run and (controls.forward and not controls.back):
                self.speed = self.runspeed
                self.damping = 0.18
                
            if controls.jump and self.ray:
                self.finalVelocity[2] = self.jumpspeed
                self.run_state = self.state_inAir
            
        if not self.col:
            self.run_state = self.state_inAir
        
        ### Apply ###
        self.assignVelocity()
        self.applyMovement()
        self.limitVelocity()
        self.applyDamping()

and the key press is controlled here;

       
    def main(self):
        key = logic.keyboard.events
        self.jump = key[events.SPACEKEY]

Add a timer property to your player, as “jump_Cooldown”. When the jump button is pressed, start counting, till it reaches a limit. Hook the jump action to the propertie’s limit.

Thanks, I am looking into doing this now. I can’t seem to find the API reference for starting the timer, does any one have a link?

I don’t have a link, but you may be able to do it without a timer property. I used an int property on a player below, not saying this is the best solution or anything but it worked for me. You will need another bit of logic for a collision sensor or something like that.


import bge

scenes = bge.logic.getSceneList()
mainSceneStuff = scenes[0].objects
keyboard = bge.logic.keyboard
jumpKey = bge.logic.KX_INPUT_ACTIVE == keyboard.events[bge.events.SPACEKEY]
player = mainSceneStuff.get("Player")

if jumpKey and player["JumpTimer"] == 0:
    player["JumpTimer"] = 10
if player["JumpTimer"] >= 2:
    print('here',player["JumpTimer"])
    player["JumpTimer"] = player["JumpTimer"] -1
if player["JumpTimer"] == 0: # and other condition such as touch/collision with floor etc
    player.applyMovement((0,0,1),True)