Today I came across a problem, which is written in the title. I am not trying to see if InstanceA is InstanceA; I want to know if there is any way to see that an instance is identical to another, which is Instance ~= Instance, but that they have completely the same properties and descendants. (passage improved using AI)
just use for loop and check propertiess you want, e.g you don’t need to know much propertiess, and it’s pretty much so lightweight it wouldn’t be a problem, you might also implement some specific ID, like ItemID or ToolID if you’re making a game
No direct command for that.. Maybe something like this will work out.
local function same(a,b)
if a.ClassName~=b.ClassName or a.Name~=b.Name then return false end
for _,p in ipairs(a:GetAttributes()) do
if a:GetAttribute(p)~=b:GetAttribute(p) then return false end
end
for _,p in ipairs(b:GetAttributes()) do
if a:GetAttribute(p)~=b:GetAttribute(p) then return false end
end
for _,descA in ipairs(a:GetChildren()) do
local descB=b:FindFirstChild(descA.Name)
if not descB or not same(descA,descB) then return false end
end
return true
end
Yup, you’ll have to compare every property like everyone said. However, depending on the specifics and how many properties you are checking you can potentially filter out ones that don’t match faster by using a hashing function and hashing all the properties you are interested in. Then if you cache the hash (it’s no faster and actually slower if you don’t cache the hash for the object) you can simply compare hashes and if they don’t match you know the properties can’t be the same. If they do match though they likely actually match, but there is a small chance of something called a hash collision which means 2 different instances/properties could give the same answer so you’ll have to check the matched hash against all properties to know for sure.
Why do you want to check if two instances are identical? The problem you’re facing might be solvable in much easier ways. Especially if you control the system that creates these instances.
For example you can set up attributes for the instances representing specific things(for example an attribute “sword” for a gear of a sword, so you know that if two instances have the attribute “sword” they must be the same baseline thing).
In general when you design any sort of system things need to be organized in such a way so you don’t really need to “search” for them. Especially if you have to do it many times.
Also ideally you can include the actual problem in the topic, because what you’re describing seems like an attempted solution for said problem.