I’m using BindAction and the second argument in that is a callback. BindAction sends the action’s name, userinputstate, and inputobject. Is there any way to send more arguments to the callback?
You could pass a function that calls the “real” callback with the parameters passed by BindAction plus some more parameters.
E.g.:
local ActionS = game:GetService("ContextActionService")
local function onFPressed(actionName, inputState, inputObject, aaaandAnotherOne, ...)
print(aaaandAnotherOne)
return Enum.ContextActionResult.Sink
end
ActionS:BindAction("actionName", function(an, is, io) return onFPressed(an, is, io, "test") end, false, Enum.KeyCode.F)
EDIT: You can do this a bit more elegantly with currying. It’s essentially creating a specialized version of a function where some of the parameters are decided before-hand, meaning you don’t have to call them with that parameter.
Here’s a function that curries a function:
local function curry(f, argN, argV)
return function( ... )
local args = {...}
table.insert(args, argN, argV)
return f(unpack(args))
end
end
Here’s an example of how it can be used:
Without currying:
local function errorPrint(firstString, ...)
print( script:GetFullName() .. " ERROR: " .. firstString, ... )
end
With currying:
local errorPrint = curry(print, 1, "ScriptName: " .. " ERROR: ")
In both cases, errorPrint("test print")
would print (ScriptName: ERROR: test print)
And here’s how the stuff from before could be done with currying:
ActionS:BindAction("actionName", curry(onFPressed, 4, "testString"), false, Enum.KeyCode.F)
Could you point me to any resources where I could learn more in depth about currying, I have a use-case for it and I really wanna understand how it works but I’m still confused about it
Hi, I’ve since learned that’s actually not currying but partial application, although the terms are often mixed up.
Here’s a decent explanation:
http://lua-users.org/wiki/CurriedLua
Wikipedia is also alright