Finding out if some keys were pressed in a certain order

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By arthurZ

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. :slight_smile:

:bust_in_silhouette: Reply From: Eric Ellingson

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.