What can i improve in this UiD?

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
1 Like

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
1 Like

I used numbers because they are the best option for remotes, one number have 2 bytes when UiD generated by those methoods are up to 32 bytes

What use case are you needing to send UIDs through a remote for?

1 Like

Tower defense game, we consider using client rendering and we want to optimize game to handle hundreds of enemies

Neat. I recommend you look into string.pack so you can compress multiple numbers down into singular digits of bytes.

strings are heavier than numbers, one character is one byte, soo as i said i use 16 bit integers