Skip to content
On this page

Identifiers

Identifiers are how Red manages events. They turn any string to a short string identifier that can be used efficiently over the network.

Identifier Generation

Identifiers are generated by incrementally increasing a value, and then turning that value into a string using utf8.char. We choose this specific function, as it will increase the length of the string as the value increases. It starts at a length of 1. At 2^7 it creates a length of 2. At 2^11 it creates a length of 3, and so on.

INFO

Most games don't use 2^7 events, so this is fine. The only issue here is if events are registered dynamically, but it's up to the library users to not do that.

lua
Serdes.NextId = 0

function Serdes.RegisterIdentifier(Name: string)
	local Id = utf8.char(Serdes.NextId)
	Serdes.NextId += 1

	return Id
end

Identifier Replication

Whenever an identifier is created, all clients need to be able to use it, so they can parse events sent using it. This is done by using attributes placed directly on RedEvent. As these attributes must be placed on the server to replicate, identifiers can therefor only be registered on the server.

INFO

RedEvent is the RemoteEvent made by Red, and placed in ReplicatedStorage.

lua
Serdes.NextId = 0

function Serdes.RegisterIdentifier(Name: string)
	assert(RunService:IsServer())

	local Id = utf8.char(Serdes.NextId)
	Serdes.NextId += 1

	Event:SetAttribute(Name, Id)

	return Id
end

One Time Identifiers

To facilitate returning values from function-like calls, Red uses one time identifiers. These are identifiers that are only used once, and then discarded. They are generated the same way as normal identifiers, except if the value reaches 0xFFFF + 1 it is reset back to 0.

lua
Serdes.NextOT = 0

function Serdes.OneTime()
	Serdes.NextOT += 1

	if Serdes.NextOT == 0xFFFF + 1 then
		Serdes.NextOT = 0
	end

	return utf8.char(Serdes.NextOT)
end