How can I add arguments to a custom "callback"

Hi,

So I was wondering if it was possible to add an Argument to a function argument, like so:

function callback(func) -- how can I add an argument to the callback?
    func()
end

callback(function(argument) -- which would look like this?
    print(argument) --> something like maybe a number or string
end)
function callback(func, …)
    func(…)
end
callback(function(a)
    print(a)
end), “arg goes here”)

not what I meant, I meant like an argument like the callback function, where it has another set of arguments.

Just call the callback with the arguments inside.

func(arg1, arg2)

A callback is a reference to a function only, it knows nothing about the arguments required. The caller of the callback has to pass in the arguments.

However, you can use a closure, to create an effect where a callback’s behavior changes like so…

function callback(func)
	func()
end

local arg1 = 1
local arg2 = 2

function closure()
	print(arg1, arg2)
end

callback(closure)
-- displays: 1 2

arg1 = 3
arg2 = 4
callback(closure)
-- displays 3 4
function callback(callbackParams, func, functionParams)
func(table.unpack(functionParams))
--do stuff
end

Is this what you’re looking for?

While these are all helpful, It isnt what im asking about, and I feel like that is my fault so I’ll try to explain a bit better,

What I mean is like when you are trying to Connect a Event, or callback function. And The Function it requires has a set argument inside it already. Like with OnServerEvent or PlayerAdded with the Player argument already set as the first argument.

Im wondering if it is possible to do that?

You might be looking for a factory…? Your use case is unclear.

local function outer(outerArg)
    return function(innerArg)
        print(outerArg, innerArg)
    end
end

local inner = outer("foo")
inner("bar") --> foo bar

Other than using Luau types, you can’t give a function parameter their own parameters, and most of the time you don’t need to anyway. In similar such cases, arguments are forwarded in from the outer function or outside code has no influence on arguments and parameters of an inner one.


ETA after I saw your recent reply:

In terms of signals on Roblox instances (RBXScriptSignal), these don’t follow the same conventions as what you’re requesting. You’re connecting a handler knowing what arguments are going to be received by the function: Roblox internally fires these events from CoreScripts.

For your example of PlayerAdded:

-- Developer side
PlayerAdded:Connect(function (player)
    -- Your code
end)

-- Core side
PlayerAdded:Fire(player)

You can do this yourself with BindableEvents as well, but the flow of signals is different from what you wanted in the OP, just keep that in mind. Without context, the first reply by Offpath would be how you forward arguments, but otherwise a function parameter can’t have its own parameters without types.

CMIIW.

2 Likes

I see, Thank you.

But also, I believe to have Fiqured it out:

function callback(func:(testArg: string) -> ()) -- this was what I was trying to ask for
    -- sorry if I was unclear, Im not good at explaining things if you couldnt tell
     func("example")
end

Im not exactly sure how it works, but it works either way because now it has its own separate list of arguments.

This may not be related to the same thing, but I wills still answer it.

So Basically what I’m trying to do is make a Module that Handles Products, and for one of the functions, I had this:

function _products.iter(val, t, id, func)
	assert(t ~= nil or next(t) ~= nil)
	for i,v in t do
		if id ~= i then continue end
		val.Value = func(val.Value, v)
	end
end

and I wanted the function to have its own arguments, And after figuring it out, looks like this:

func:(x: number, y: number) -> ()

and when I fire it, it would do this:

_products.iter(player.Cash, _products.CashData, 999, function(x, y)
	return x + y
end)

However, may not exactly work how I want it to, I was just wondering if it was even possible to do as I saw it in Events and some callbacks.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.