0 votes

Hello! I'm still new to Godot and game devolping in general. Anyways I want to create a timer that starts when the player isn't on the ground. I will use this timer so the player will be able to jump 0.4 seconds after he has fallen from the platform.

I don't have any idea how to do it, but I have tried this:

#Movment Variables
const jump_force = -500

func _physics_process(delta):
#Input for vertical movment
    if is_on_floor(): #checks if player is on floor

        #Timer to check if 0.4 seconds have pasted
        timer = Timer.new()
        timer.set_wait_time(1)
        timer.connect("timeout", self, "")
        add_child(timer)

        if (Input.is_action_pressed("move_up")):
            motion.y = jump_force

#Movment of player
    move_and_slide(motion, Vector2(0,-1))

As you see I haven't connected the timer to a function. Bcs I thought it would be inefficient to make a whole function to just test if the timer is done. I also don't know how to implement the timer into my if statement. So if there is any way to do it, I would be happy to know

//Marre

in Engine by (26 points)

2 Answers

+3 votes
Best answer

Add delta to a float and reset it every time the jump is landed.

var jump_timer : float

if is_on_floor():
    jump_timer += delta
    if (Input.is_action_pressed("move_up") and jump_timer > 0.4):
        jump_timer = 0
        #won't start timing again until is_on_floor() is true
by (3,223 points)
selected by
+1 vote

You can also make a timer node. This node has four properties. You can configure it and then do the getnode("timernode").start().

Also, you have to link the timeout signal if you want to play something when it finishes.

yield (timer_node, "timeout") will wait in script.

by (329 points)

Thanks for the good info!

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.