How do I link a function to a variable, while passing parameters to it, and not calling it?

Alright, so I know how to like set a variable to be a function:

local function catFunction()
       ---do something
end

local makeAnimal = catFunction

Well, I want to do that but also pass parameters to the function without calling it. For an example, something like:

local shootFunction = gunTypes["Shotgun"]["shootFunction"](pellets, damage, etc.)

The problem is that in doing this, the function gets called.

1 Like

If you want to do something like that, it would probably make sense to investigate into the route of object oriented programming. Currently, you have a class (your gun) which has a ton of generic attributes (pellets, damage, whatever), and are looking to use a function for that gun, or otherwise ā€œobjectā€, which would become a method.

There are tons of guides online to this concept, so I’ll link whatever ones I find online here - this doesn’t mean they’re necessarily good, they’re just the first to come up.

Yeah, I’m going to make this Object Oriented but like the question still stands lol.

To clarify, I’m going to have a module script with all the different gun types(burst, shotgun, projectile,. etc.) and I’m going to have generic functions attached to those that I can just pull out of the dictonary. However, then I’ll pull more specific stats like damage and firerate out of a different dictonary and pass them into the generic function.

Finally, I’ll do something like self.fireFunction = genericFunction(stats) in the gun object but I can’t do that; that’s why I asked the question lol.

A function is automatically called if you put something after it (most commonly parenthesis) that behavior cannot change. But you can call a function within another function

local function funcx(x)
	return x
end

local function funcy(y)
	return y
end

local function call_funcs()
	local x = funcx
	local y = funcy

	local value_x = x(2)
	local value_y = y(5)

	print(value_x + value_y)
end


call_funcs() --> 7

So, you’re saying, if I make a lambda function with the generic function and stats passed in, it’ll work. Ok, wow, can’t believe I didn’t think of that lol, thank you so much.

1 Like

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