One way to do this would be to keep an array of the last X key presses and compare it to preset arrays.
Here's a quick and dirty example:
var keysequence = []
var maxcombolen = 5
var combos = { "COMBO 1" : ["c","o","m","b","o"] , "COMBO 2" : ["a","b","c"] }
func pressKey(key):
keysequence.push_back(key)
# print(keysequence)
for curcombo in combos:
if checkCombo(keysequence,combos[curcombo]):
print( curcombo )
keysequence.clear()
if keysequence.size() > maxcombolen:
keysequence.pop_front()
func checkCombo(s,t):
# fail if not enough keys pressed
if s.size() < t.size():
return false
# slice the last X key presses so both arrays start at same point
var c = s.duplicate()
c.invert()
c.resize(t.size())
c.invert()
# fail if any pressed key sequence don't match the combo
for i in range(t.size()):
if c[i] != t[i]:
return false
# all keys matches, execute combo
return true
There's probably a cleaner way to do the array operations, but that should at least give you an idea if how it could be done.
It assumes multi-input presses like down+right have already been converted into a single "key". (I've not yet looked at how Godot/Gdscript handles input, hence why I stuck with simple characters.)
You may want to call keysequence.pop_front()
on a timer, to ensure the combo sequence is performed quick enough, or just record time since last key press and check its within acceptable margins to ensure that the keys are pressed at a constant rate, not too fast or slow.