Improvements to a "Hybrid Mode" Filter?

Hi,

So currently I’m trying to create a difficulty system that selects a specific folder depending on said difficulty which ranges from 0 - 5 on the game’s scale, however I am trying to add a Difficulty 6 for private servers to use to modify gameplay, which I call Hybrid Mode, this mode essentially allows you to pick an item from each folder, as well filter from which ones you want to choose from.

However, I’m wondering if I am able to simplify my code to be more efficient, or less wordy, as this my 1st attempt at trying to create this kind of mode. So far it appears to be working as intended, but it doesnt seem to be an optimal way of doing it, what can I do to imporve this?

ModuleScript Functions:

local function GetFileByHybridFilter(self) -- Gets a random file using the filter
    -- the filter depends on the difficulty set, in this case, it only works at 6

    -- Filesafe in case all values are filtered
    local falseCount = 0;
    for i = 1,#self.HybridFilter do
        if self.HybridFilter[i] == false then
			falseCount += 1
		end
	end
	
	if falseCount == #self.HybridFilter then
		error("HybridFilter must have at least one unfiltered value!")
	end

	local y; -- used to get the file number
	while true do
		y = self.FileRandomizer:NextInteger(1, #self.HybridFilter) -- random number
		if self.HybridFilter[y] == false then continue; end -- continue if false
		break; -- break if true (it has found a non filtered item.
		
	end
	return y;
end

function gen:ApplyHybridFilter(...: boolean?)
	local l = {...};
	
	self.HybridFilter = {} -- replace current filter with empty table
	for i,v in l do
		v = v and true or false -- checks if there is an existing value
        -- if there isnt an existing value, replace it with a false value
		self.HybridFilter[i] = v -- adds boolean to filter
	end
    -- table is now usable for the rest of the metatable
end

Script Usage:

local a = Class.new(nil, 6, nil, nil, nil) -- testing difficulty 6 (Hybrid Mode)
a:ApplyHybridFilter(false, false, false, true, true) -- applying filter items

print(a.Seed) -- debugging (unrelated)
a:Generate(); -- unrelated

This is only part of the script, there are more functions, but im only showinf the relevant functions that this topic is about, but the function is being fired as such:

local CurrentFile = self.Difficulty; -- Difficulty
for i = 1,self.Size do -- Iterate based on the size specified before (unrelated)
		if self.HybridMode then -- check if Hybrid Mode is enabled for this mode
			CurrentFile = GetNumberByHybridFilter(self); -- get from filter
			print(CurrentFile) -- debugging stuff
		end
        -- I'm not yet to this point, so there isnt anything here yet... :(
	end
end
1 Like

This would probably be a better post for Code Review, rather than Scripting Support

But anyway, here’s one I could see:


function gen:ApplyHybridFilter(...: boolean?)
	local l = {...};
	
	self.HybridFilter = {} -- replace current filter with empty table
	for i,v in l do
		-- checking v in an "if/while/repeat until v" statement technically will read as a boolean
		-- (I.E. whether or not it exists), but will read as either an object or as nil elsewhere
		-- When assigning a BoolValue with it, just use a short ternary operator
		self.HybridFilter[i] = if not v then false else true
	end
end

That could work, though I was wondering about how it was looping through things.