I'm developing an Entity Component System and I need a method for finding EntityIDs from Roblox Instances.
I have already considered a couple solutions:
1) A function which searches every "Instances" component in a table of components for a matching entity, then returns the entityID associated with that component.
local Instances = {
42 = [Button = workspace.Part]
}
local function FindEntityFromInstance(obj)
for entityID, component in pairs(Instances) do
local component = Instances(index)
for key, value in pairs(component) do
if value == obj then return entityID end
end
end
print(FindEntityFromInstance(workspace.Part)) --42
2) A table of Instance-EntityID pairs which is updated whenever an instance is added or removed from a component. I can index this table with a given instances to find it's associated entity.
local InstanceEntityIndex = {workspace.Part = 42}
function FindEntiyFromInstance(obj)
return InstanceEntityIndex[obj]
end
print(FindEntityFromInstance(workspace.Part)) --42
3) Every instance can be wrapped in a table with an EntityID property. These tables can be indexed similar to the second method, but will simplify how Instances fit into my framework and will allow for instances to be shared by entities or have custom tags which aren't replicated.
local WrapperIndex = {}
local function WrapInstance(obj)
local wrapper = {}
local wrapperMeta = {
__index = obj,
__newIndex = function(tab, property, value)
if obj[property] then
obj[property] = value
else
tab[property] = value
end
end
}
setmetatable(wrapper, wrapperMeta)
WrapperIndex[obj] = wrapper
return wrapper
end
local function GetInstanceWrapper(obj)
return WrapperIndex[obj]
end
local wrapper = WrapInstance(workspace.Part)
wrapper.entityID = 42
print(GetInstanceWrapper(workspace.Part).entityID) --42
4) Every instance can have a StringValue or IntValue named EntityID with the EntityID as their value.
5) Something to do with Collection Service
Some Issues:
I probably won't be able to use StringValues, IntValues or Collection Service because of replication issues. The client and server will keep track of their own unique entities and components.
There will be several entities with instance components in the game and this is a frequent problem, so looping over them all to compare is expensive.
I'm building a survival game, so the more memory I can conserve the better.
An Instance can be used by multiple components in an entity
I'll probably be using the second method because I don't have to loop over several components to make instance comparisons and it takes comparatively less memory than the other methods.
If anyone has any different suggestions or input on which method I described is best suited to my situation, please let me know.

2 comments
Every instances have three methods that called :SetAttribute(), GetAttribute() and GetAttributes(). May be you can use it to set/get EntityID, but there aren't much docs about the methods so I may wrong about it.
I thought they haven't implemented custom attributes yet but looking into it you can enable them. There's also surprisingly little documentation on them on the dev site. Anwyways that would probably suffer the same replication issues as Collection Service or string/int values