1
\$\begingroup\$

Hey i am beginner gamedev. I couldn't find answer to this or I am bad articulating my problem. But really would like an answer to this. How can i make function to press two keys like A and D at the same time to make a jump animation if A and D are already taken separately to A = walk left and D = to walk right? Without them overlapping.

func _input(event):

if event.is_action_pressed("left") && event.is_action_pressed("right"):
    $DancerSprite.play("bluedouble")
    
if event.is_action_pressed("left"):
    $DancerSprite.play("blueleft")
    return
    
elif event.is_action_pressed("right"):
    $DancerSprite.play("blueright")
    return
\$\endgroup\$
1
  • \$\begingroup\$ Seems you already doing that, just need to add return after handling the key combo. \$\endgroup\$ Apr 11, 2021 at 1:31

1 Answer 1

3
\$\begingroup\$

Let us start with simple input. First of all, set your actions in the Input Map in project settings.

Now, there are two ways to go about getting input. You can add a _input function:

func _input(event):
    if event.is_action_pressed("action"):
        print("action start")

    if event.is_action_released("action"):
        print("action end")

Or you can use Input.is_action_pressed in your _physics_process:

func _physics_process(delta):
    if Input.is_action_pressed("action"):
        print("action is ongoing")

If you don't care if they actions were pressed previously, then using _physics_process it is easy to check for both "at the same time":

func _physics_process(delta):
    var a = Input.is_action_pressed("action_a")
    var b = Input.is_action_pressed("action_b")
    if a and b:
        print("both")
    elif a:
        print("a")
    elif b:
        print("b")
    

Except, we don't know for how long one was pressed before the other.


If the the player has one action pressed, and pressed the other… With the above code, the combined action would trigger. What if you wanted to distinguish if the player released and then pressed both concurrently?

In that case, we need to keep track if the action ongoing with a variable. So either do this:

var action_ongoing = false

func _input(event):
    if event.is_action_pressed("action"):
        action_ongoing = true

    if event.is_action_released("action"):
        action_ongoing = false

Or this:

var action_ongoing = false

func _physics_process(delta):
    action_ongoing = Input.is_action_pressed("action")

And for the combined action, you can do this:

var a_ongoing = false
var b_ongoing = false

func _physics_process(delta):
    var a = Input.is_action_pressed("action_A")
    var b = Input.is_action_pressed("action_B")
    if a and b:
        if !a_ongoing and !b_ongoing:
            print("both together")
        elif !a_ongoing:
            print("b then a")
        elif !b_ongoing:
            print("a then b")

    elif a and !a_ongoing:
        print("a!")
    elif b and !b_ongoing:
        print("b!")

    a_ongoing = a
    b_ongoing = b

Please notice that they weren't really pressed at the same time. They just were pressed close enough in time that they register at the same time. That is the time from one input to the other was short enough.

What if we wanted control over that time interval?

Well, let us keep track of the instant the action was pressed!

We can use OS.get_ticks_msec() to get milliseconds from engine start. If store the time for one input, when we get another input we can compare the time and decide if they are close enough in time.

I'll add a max_milliseconds variable, and if the time between one action is pressed and the other is less than that, we say they happened together.

It is time for _input to shine:

var a_time = null
var b_time = null
var max_milliseconds = 100

func _input(event):
    var now = OS.get_ticks_msec()
    if event.is_action_pressed("action_A"):
        if b_time != null:
            if now - b_time < max_milliseconds:
                print("both together")
            else:
                print("b then a")
        else:
            print("a!")

        a_time = now

    if event.is_action_released("action_A"):
        a_time = null

    if event.is_action_pressed("action_B"):
        if a_time != null:
            if now - a_time < max_milliseconds:
                print("both together")
            else:
                print("a then b")
        else:
            print("b!")

        b_time = now

    if event.is_action_released("action_B"):
        b_time = null
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .