Soo i wanted to ask you if this code is good enough for re-usable UiD creator
local UidManager = {}
-- Tables
local inactiveUidList = {}
local activeUidList = {}
-- Variables
local CurrentUid = 0
function UidManager.GetUid(): number
if inactiveUidList[1] then
--/ Reuse id
local toReuse = inactiveUidList[1]
table.remove(inactiveUidList, 1)
activeUidList[toReuse] = 1
return toReuse
else
--/ Inrease current Uid
CurrentUid += 1
activeUidList[CurrentUid] = 1
return CurrentUid
end
end
function UidManager.RemoveUid(Uid: number)
if activeUidList[Uid] and not table.find(inactiveUidList, Uid) then
-- Move Uid to inactive list
activeUidList[Uid] = nil
inactiveUidList[#inactiveUidList + 1] = Uid
end
end
return UidManager
It is a cool little thing but for actual UIDs, you should use GenerateGUID. There is another unique way of generating a UID which is this:
-- Source: https://stackoverflow.com/a/24097793
local UID = tostring({}):sub(8)
print(UID) -- Outputs unique code, the same format as tables when you print them
--> 0x7f876b608b00