I’m working on a system that must check for multiple conditions. These conditions use varying types (strings, number ranges, etc.) and each may or may not exist depending on the situation. The maximum number of conditions which may exist is static, but I would like for it to be easily expandable.
Currently I have the following code structure:
--[[
valueA: String
valueB: NumberRange
valueC: NumberRange
conditionA: String OR nil
conditionB: NumberRange OR nil
conditionC: NumberRange OR nil
--]]
if
(valueA == conditionA or conditionA == nil)
and
((conditionB and valueB >= conditionB.Min and valueB <= conditionB.Max) or conditionB == nil)
and
((conditionC and valueC >= conditionC.Min and valueC <= conditionC.Max) or conditionC == nil)
then
-- Do stuff
end
My current solution works. However, I would like to know if there is a cleaner way of going about this.
This post is a bit nebulous without additional context, but it sounds like you could use a modulescript
For example, some of my games employ status effects, so I store the pertinent code for each one in a dictionary returned by a modulescript and have each one apply a counter to a specific folder created when the character is spawned
Then to certify whether or not a character has said status effect, I simply scan that folder for the relevant counter
Again, no idea if that helps solve your problem or not, since base post is missing critical amounts of detail
You’re right in that I may have generalised things a lot.
The system in question is for the procedural generation of various structures. These structures are defined in a ModuleScript that I can easily edit. Various conditions for generation can be defined there, and the generation script then checks them against the circumstances in a given area to decide whether or not that structure can generate there.
However, some or all of these conditions can be absent (e.g. a structure that can generate anywhere would have no defined conditions). I’m trying to find a cleaner and more scalable way of evaluating these conditions.
My only real suggestions would either be to employ a single top-down variadic function that can accept any number of arguments converted to a table like so:
function generateStructure(...)
local Conditions = {...}
for _,Property in next(Conditions) do
-- Stuff
end
end
OR, you could potentially itemize the conditions into a dictionary and then iterate over said dictionary, using the ‘continue’ operator when an entry is blank
Thank you for your suggestions. I just hashed out a function to evaluate conditions and used it when iterating over the definitions dictionary. Kind of an obvious thing but I got a bit blinded by some magical thinking.