Best way to store a large amount of small functions?

local distances={
	function(p1pos,p2pos) return -p1pos.Z+p2pos.Z end,
	function(p1pos,p2pos) return -p1pos.X+p2pos.X+p1pos.Z-p2pos.Z end,
	function(p1pos,p2pos) return -p1pos.X+p2pos.X end
}
local equations={
	function(p1pos,p2pos,d1,d2,d3) return Vector3.new(p2pos.X,1,p1pos.Z) end,
	function(p1pos,p2pos,d1,d2,d3) return Vector3.new(p2pos.X-d1,1,p1pos.Z) end,
	function(p1pos,p2pos,d1,d2,d3) return Vector3.new(p2pos.X+d1,1,p1pos.Z) end,
	function(p1pos,p2pos,d1,d2,d3) return Vector3.new(p1pos.X+d1,1,p2pos.Z) end,
	function(p1pos,p2pos,d1,d2,d3) return Vector3.new(p2pos.X-d2/2,1,p2pos.Z+d2/2) end,
	function(p1pos,p2pos,d1,d2,d3) return Vector3.new(p2pos.X,1,p2pos.Z+d2) end,
	function(p1pos,p2pos,d1,d2,d3) return Vector3.new(p1pos.X,1,p1pos.Z-d2) end,
	function(p1pos,p2pos,d1,d2,d3) return Vector3.new(p1pos.X,1,p2pos.Z) end,
	function(p1pos,p2pos,d1,d2,d3) return Vector3.new(p1pos.X,1,p2pos.Z+d3) end,
	function(p1pos,p2pos,d1,d2,d3) return Vector3.new(p1pos.X+d2/2,1,p1pos.Z-d2/2) end,
	function(p1pos,p2pos,d1,d2,d3) return Vector3.new(p1pos.X-d1,1,p2pos.Z) end,
	function(p1pos,p2pos,d1,d2,d3) return Vector3.new(p2pos.X,1,p1pos.Z-d3) end,
}

These functions are used once every heartbeat. The equations array is used to get the positions of 12 parts using two root parts with a known position. Notably each part owns its own function. part1 only uses equation function 1 and so forth.
here is the code that calls the functions:

RS.Heartbeat:Connect(function()
	--...
	local p1pos=p1.Position
	local p2pos=p2.Position
	local d1=distances[1](p1pos,p2pos)
	local d2=distances[2](p1pos,p2pos)
	local d3=distances[3](p1pos,p2pos)
	
	for i, part in ipairs(folder) do
		part.Position=equations[i](p1pos,p2pos,d1,d2,d3)
		--...
end)

I saw once that it was bad to store code as a massive block and as such am wondering if there is a better way to store all my functions.

1 Like

You could put it in a module. It’d keep things more seperated.

That is true, but is there a better way to store the actual code itself inside the module script? Or is this the best way?

1 Like

Modules is a good way to store large amounts of functions.
(even better if using multiple modules to categorise functions)

Hello! Im new to the devform!

2 Likes

I’ll echo what everyone else has said so far and recommend that you use module scripts. I’d recommend one for each table of functions.

Secondly, I recommend that you name your functions instead of putting them in a numerical table.

For instance, instead of this in your module:

return {
	function(p1pos,p2pos,d1,d2,d3) return Vector3.new(p2pos.X,1,p1pos.Z) end,
	function(p1pos,p2pos,d1,d2,d3) return Vector3.new(p2pos.X-d1,1,p1pos.Z) end,
	function(p1pos,p2pos,d1,d2,d3) return Vector3.new(p2pos.X+d1,1,p1pos.Z) end,
	function(p1pos,p2pos,d1,d2,d3) return Vector3.new(p1pos.X+d1,1,p2pos.Z) end,
	function(p1pos,p2pos,d1,d2,d3) return Vector3.new(p2pos.X-d2/2,1,p2pos.Z+d2/2) end,
	function(p1pos,p2pos,d1,d2,d3) return Vector3.new(p2pos.X,1,p2pos.Z+d2) end,
	function(p1pos,p2pos,d1,d2,d3) return Vector3.new(p1pos.X,1,p1pos.Z-d2) end,
	function(p1pos,p2pos,d1,d2,d3) return Vector3.new(p1pos.X,1,p2pos.Z) end,
	function(p1pos,p2pos,d1,d2,d3) return Vector3.new(p1pos.X,1,p2pos.Z+d3) end,
	function(p1pos,p2pos,d1,d2,d3) return Vector3.new(p1pos.X+d2/2,1,p1pos.Z-d2/2) end,
	function(p1pos,p2pos,d1,d2,d3) return Vector3.new(p1pos.X-d1,1,p2pos.Z) end,
	function(p1pos,p2pos,d1,d2,d3) return Vector3.new(p2pos.X,1,p1pos.Z-d3) end,
}

Try this in your module:

local Equations = {}

function Equations.Name(p1pos, p2pos, d1, d2, d3)
end

-- and so on;
-- the function name could be the name of the mathematical operation
-- if there is one, and if not, a verb/verb phrase describing what it does.

return Equations

Naming your functions will help you to keep track of everything. It’s a few extra words to type, but believe me, descriptive naming pays off big time down the road, especially when editing/fixing your code! It’s much easier to fix a box:InsertItem(itemType, itemCount) than it is to fix a classes[3]:add(t, c). If I were you, I’d spare myself the headache and name the functions.

1 Like

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