How can i get the time between a "IsActionJustPressed" and a "IsActionJustReleased" in C#

Godot Version

Godot 4.2.1 mono

Question

I’m new to game dev and godot in general and i can’t get a timer(no talking about the node) to work. I was trying to do a jump king clone for a game jam but wasn’t able to get the charging jump to work. Can someone explain to me how to do it in C#.

if there a way to do it with the timer node i’m all for it too

One very simple way to do it without a timer node is like this:

//create a jumpTimer float somewhere
float jumpTimer

public override void _PhysicsProcess(double delta)
{
	if (Input.IsActionPressed("jump"))
	    {
	        //+1 every second you hold jump (edited)
	        jumpTimer += delta
	    }
	else
	    {
	        //Resets jumpTimer when not jumping
	        if (jumpTimer > 0.0)
	            jumpTimer = 0.0
	    }
}

Read the jumpTimer or call a jump function before setting the jumpTimer back to zero, which doesn’t have to be in the “else” part but it’s easy to understand like this. You may want to move inputs out of the physics process anyway, and you can use the ActionJustReleased for these things as well.

This is very basic though so i’d recommend just watching a bunch of videos or read up on godot’s timers

it should work but now i’m curious if there’s a way to do it without the frame rate (with the PC time for example) tho i’ll look that up myself :stuck_out_tongue_winking_eye:

That’s a nitpick but there a error in your code you need to convert delta to float in the

jumpTimer += delta 
//should be
jumpTimer += (float)delta

My bad, delta time already takes into account the frame rate, my initial comment was wrong. Without delta it would have been every frame
jumpTimer += 1.0
in the case of delta it’s seconds.
Also i use gdscript so that’s where the error came from.

It doesn’t work the timer in the second if return 0 and i can’t find why and the timer in the first if just iterate ones

Vector2 velocity = Velocity;
float JValue = 0f;
float baseJV = 400;
float FJValue = 0f;
float timer = 0f;

 if (Input.IsActionPressed("jump") && IsOnFloor())
{
	timer += 10f;
	
	LTimer = GetNode<Label>("Timer");
	LTimer.Text = "Timer: " + timer.ToString(); 
}
 
 if (Input.IsActionJustReleased("jump") && IsOnFloor())
{

    FJValue = baseJV + timer;
    velocity.Y = -FJValue;

	LJump = GetNode<Label>("Jump");
	LJump.Text = "Jump: " + FJValue.ToString();
}

If you want it to happen every frame it needs to be in one of the process functions