0 votes

I would like to know if there is a way to find out if some keys were pressed in a certain order (immediately or not, one after another), let's say I was pressing attack and then pressed jump (attack -> jump). Another example, let's say I was running right, I pressed left to run in this direction. In this situation, how do you know that I pressed -> and <- in that order?

I'm not looking for a way to save these combinations. For example, like the Guile Super in Street Fighter which is back, foward, back, foward + punch (<-, ->, <-, ->). I just want to know if the last 2 or 3 keys pressed came out in that order to put in a if statement.

Thank you and hope I made myself understood. :)

in Engine by (179 points)

1 Answer

+1 vote
Best answer

You could just store the key presses in an array, and just grab the most recent N-presses from that array and compare to some const that holds the presses you're looking for.

const COMBO = ["run", "left", "right"]
const REMEMBER = ["jump", "kick", "left", "right", "run"] 

var action_history = [] 

func _process(delta):
    for action in REMEMBER:
        if Input.is_action_just_pressed(action):
            action_history.append(action)

    if  (action_history[i] for i in range(action_history.size() - COMBO.size(),action_history.size() - 1,1)) == COMBO:
        # do something

I wrote this on my phone, so take this with a grain of salt. Especially the part about getting the last however many values from the history array.

by (1,650 points)
selected by
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 Frequently asked questions and 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 [email protected] with your username.