I planned on adding Input Buffer (in case you don’t know, it is a feature that allows you to perform abilities by requiring multiple inputs, instead of single one) to my melee system.
Using and modifying codes from this post, I managed to get it working. However, the problem is when I built a list of patterns like this:
--input order is from top to bottom
local Abilities = {
{
AbilityName = "Test Combo 1";
AbilityInputPattern = {
"Down",
"Right",
{["Keyboard"] = "Z", ["Controller"] = "ButtonB"},
};
AbilityServerFunc = function(Player, Tool, MouseHit)
warn("Combo 1 Activated")
end;
};
{
AbilityName = "Test Combo 2";
AbilityInputPattern = {
"Left",
"Left",
{["Keyboard"] = "X", ["Controller"] = "ButtonA"},
};
AbilityServerFunc = function(Player, Tool, MouseHit)
warn("Combo 2 Activated")
end;
};
{
AbilityName = "Long Combo";
AbilityInputPattern = {
"Left",
"Down",
"Right",
"Left",
"Up",
"Right",
{["Keyboard"] = "C", ["Controller"] = "ButtonX"},
};
AbilityServerFunc = function(Player, Tool, MouseHit)
warn("Long Combo Activated")
end;
};
{
AbilityName = "Konami";
AbilityInputPattern = {
"Up",
"Up",
"Down",
"Down",
"Left",
"Right",
"Left",
"Right",
{["Keyboard"] = "Z", ["Controller"] = "ButtonB"},
{["Keyboard"] = "X", ["Controller"] = "ButtonA"},
{["Keyboard"] = "C", ["Controller"] = "ButtonX"},
};
AbilityServerFunc = function(Player, Tool, MouseHit)
warn("Konami Code Activated")
end;
};
};
and performed an ability with long, complex input commands, the script did not execute specific action, but often one with shorter pattern. For examples:
-Konami Code:
-Konami Code with arrows only (with additional bug which executed random ability with short inputs even when you did not even form combo pattern)
The code:
local function CheckSequence()
local BufferIndex = 0
local LastMatchIndex = 0
local MatchedInputCount = 0
for i = 1, #InputPatterns do
BufferIndex = (#Combo - 1) + 1 --Always stays at 1
MatchedInputCount = 0
for ii = #InputPatterns[i], 1, -1 do
while BufferIndex >= 0 do
if typeof(InputPatterns[i][ii]) == "table" then --Detects if one of inputs is a table
local Matched = false
for name, value in pairs(InputPatterns[i][ii]) do
if value == Combo[BufferIndex] then
Matched = true
break
end
end
if Matched then
MatchedInputCount += 1
break
end
else
if InputPatterns[i][ii] == Combo[BufferIndex] then
MatchedInputCount += 1
break
end
end
BufferIndex -= 1
end
end
if (MatchedInputCount == #InputPatterns[i]) then
LastMatchIndex = i
--Debug
print("")
warn("<<<<<RESULT BEGIN>>>>>")
print("SEQUENCES:")
print(unpack(Combo))
print("BEST MATCHING PATTERN:")
print(unpack(InputPatterns[LastMatchIndex]))
warn(">>>>>RESULT END<<<<<")
print("")
--
Combo = {}
return InputPatterns[LastMatchIndex] --Returns best matching combo pattern
end
end
return nil --Returns nil if fails
end
OnComboPerformed = function(Input, Name)
--Debug
print("")
print("INPUT PRESSED: "..Name)
--
AddToCombo(Name)
local BestMatchingCombo = CheckSequence()
if BestMatchingCombo then
--Do something with this pattern
end
end
Any help or advice is appreciated
Thank you in advance.