Imagine we're building a fighting game and we wanna create a Special Move. For example the Hadoken from Street Fighter. How can we create an Input Handler that watches the Input and Determines whether or not the move should be activated or not.

For example If i press

↓ , ↘ , → , [ A ] (The Keyboard Button)

I want it to Print "true" otherwise I want it to do nothing.

How can we create such a script where we can Specify The Required Inputs in the Script, and the Input Handler just watches the input and determines if they were correct/wrong and which move was executed.

It was kind of difficult to find a solution for this, and I'm not Super Used to GDScript So It will be appreciated if someone can give a bit of insight to this problem.

Thanks in Advance! Best Regards

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.

I have not watched it myself, but PigDev on Youtube has a video explaining how they wrote a combo system in Godot:

There is also this Reddit post, where a user has implemented a combo system. They link to a GitHub project that has the code they used for the combo system, which might be useful.

I think @"Peter Boughton" 's example covers the general idea. As far as I can tell, implementing a combo system primarily involves storing the input, checking for combos, and optionally clearing the input when a combo has been met and/or a timer has expired (depending on the type of game).

(Side note: Welcome to the forums)

3 years later