Anytime I have a piece of code that relies on a model using a specific structure I constantly find myself writing long and confusing if statements to verify the structure. This cannot only be tiresome but leads to nearly unreadable code at times.
function verifyDoor(door)
local isDoor = false
if typeof(door) == "Instance" then
if door:GetAttribute("Locked") and typeof(door:GetAttribute("Locked")) == "boolean" then
if door:FindFirstChild("TouchZone") and door:FindFirstChild("PathfindingPart") then
if door.PathfindingPart:FindFirstChild("PathFindingModifier") then
if door:FindFirstChild("Doors") and door.Doors:FindFirstChild("Right") and door:FindFirstChild("Left") then
isDoor = true
end
end
end
end
end
return isDoor
end
As You can see it’s very messy and this is only an example for a less complex object. Imagine doing this for something like a Spaceship or etc. Now lets write this exact same logic with our new Instance Verifier Module:
local InstanceVerifier = require(game:GetService("ReplicatedStorage"):WaitForChild("CoreComponents"):WaitForChild("InstanceVerifier"))
local doorModel = script.Parent:Clone()
local doorVerifier = InstanceVerifier.new("Door")
local pathfindingHelper
local doors
doorVerifier:AddAttribute("Locked","boolean")
doorVerifier:AddInstance("TouchZone")
--Ability to add in nested children requirements
pathfindingHelper = doorVerifier:AddInstance("PathfindingPart")
pathfindingHelper:AddInstance("PathfindingModifier")
doors = doorVerifier:AddInstance("Doors")
doors:AddInstance("Right")
doors:AddInstance("Left")
----Now when we want to verifiy a door model all we have to do is
local verified, foundErrorsTable = doorVerifier:Verify(doorModel)
if not verified then
print(foundErrorsTable)
end
Now isn’t this just much cleaner
The best part is this module is very powerful as it supports ALL instance sub-classes and an unlimited amount of nested children your instance may require. Do you have a Surface Gui that needs to have specific buttons before being initiated? NO PROBLEM.
The Verify() function returns a table of all missing aspects of your instance so you can easily pinpoint what an attributes/parts and etc your instance is missing. You can also use the SetPrintWarning function so you can know this info at runtime too.
new
local verifier = verifier.new("optionalName")
Parameters
name : optional, used to identify which verifier is which when printing out which parts of a instance failed.
Returns
InstanceVerifier
AddAttribute
verifier:AddAttribute("Speed", "number")
Parameters
attributeName : mandatory, the name of the attribute you would like the verifier to look for.
attributeType : mandatory, the type of the attribute you are looking for
Returns
InstanceVerifier
AddInstance
verifier:AddInstance("Part", "BasePart")
Parameters
instanceName : mandatory, the name of the instance you would like the verifier to look for.
instanceType : optional, the type of the instance you are looking for
Returns
InstanceVerifier
Important Note
>The verifier returned is not the original verifier used to call this function. This new verifier is linked to the added instance. this was done so adding in nested children is very easy and intuitive. Example:local BeachVerifier = InstanceVerifier.new()
local WaterVerifier = orgVerifier:AddInstance("Water") -- Child of Beach
local FishVerifier = WaterVerifier:AddInstance("Fish") -- Child of Water
FishVerifier:AddAttribute("SwimSpeed", "number")
BeachVerifier:Verify() -- Will call WaterVerifier:Verify() and FishVerifier:Verify()
--- This functionality allows for what I call intuitive linking
local BeachVerifier = InstanceVerifier.new()
BeachVerifier:AddInstance("Water"):AddInstance("Fish"):AddAttribute("SwimSpeed","number")
BeachVerifier:Verify() -- Will call Verify() for Water and Fish instances
Verify
local verified, failedChecks = verifier:Verify(doorModel,true)
Parameters
instance : mandatory, The instance you would like to verify
performanceMode : optional, initiates a return as soon as the first failed check is found.
example: if a part is missing multiple attributes this verification will only return the first missing attribute in the failed checks table.
Returns
boolean : representing whether the instance was sucessfully verified
table : describing where the instance failed it’s verification
VerifyWithError
local verified, failedChecks = verifier:VerifyWithError(doorModel,true)
Description
Exactly the same as Verify() except this will throw an error containing the failedChecks table if the instance fails verificationSetPrintWarning
verifier:SetPrintWarning(true)
Description
Tells the verifier if it should print a warning containg failedChecks anytime an instance fails verificationParameters
boolean : mandatory, Tells the verifier if it should print a warning containg failedChecks anytime an instance fails verification
Returns
nil
SetDebugMode
verifier:SetDebugMode(true)
Description
Used for debugging this module. You'll most likely never touch itParameters
boolean : mandatory, determines if debug mode should be turned on or off
Returns
nil