Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issues with unreliable batching methods #19

Closed
jmesrje opened this issue Jul 22, 2024 · 1 comment
Closed

Issues with unreliable batching methods #19

jmesrje opened this issue Jul 22, 2024 · 1 comment

Comments

@jmesrje
Copy link
Member

jmesrje commented Jul 22, 2024

Problems arise when using unreliable events, suspected when batching

@Barenton
Copy link

Barenton commented Jul 27, 2024

In terms of unreliable batching, there are a few problems.

  • The player parameter is unnecessary within the SendUnreliable function that is present in the Client.luau file since this function runs on the client.

  • Referencing an original table won't make a separate copy of it. Currently, data that is being processed is duplicated when measuring its size. To create a true mimic table, we can instead perform a deep copy of the table.

-- Client.luau

-- A function to deep copy tables
-- (taken from Sleitnick's RbxUtil)
local function Copy<T>(t: T, deep: boolean?): T
	if not deep then
		return (table.clone(t :: any) :: any) :: T
	end
	local function DeepCopy(tbl: { any })
		local tCopy = table.clone(tbl)
		for k, v in tCopy do
			if type(v) == "table" then
				tCopy[k] = DeepCopy(v)
			end
		end
		return tCopy
	end
	return DeepCopy(t :: any) :: T
end

local function SendUnreliable(id: string, args: { any }) -- Player parameter is removed here
	if not Outgoing.Unreliable[id] then
		Outgoing.Unreliable[id] = {}
	end

	local OutgoingMimic = Copy(Outgoing.Unreliable, true) -- Perform a deep copy of the outgoing table
	
	-- Originally, inserting to the table would have duplicated data,
	-- but deep copying it makes a true separate copy that we can use to measure.
	table.insert(OutgoingMimic[id], args)

	local PacketSize = RemotePacketSizeCounter.GetPacketSize({
		RunContext = "Client",
		RemoteType = "RemoteEvent",
		PacketData = { OutgoingMimic },
	})

	if PacketSize >= 840 then
		Unreliable:FireServer({ [id] = { args } })
	else
		table.insert(Outgoing.Unreliable[id], args)
	end
end

This should solve any client-sided problems coming from using unreliable events while still incorporating batching.

  • Deep copying also needs to be included in the Server.luau file within its SendUnreliable function.
  • The OutgoingForPlayer variable needs to be updated so it won't be a nil reference.
-- From Server.luau

local function SendUnreliable(player: Player, id: string, args: { any })
	local OutgoingForPlayer = Outgoing[player]

	if not OutgoingForPlayer then
		CreateOutgoingForPlayer(player)
	end

	-- Update the variable. This was included within the `SendReliable`
	-- function, but may have been overlooked here.
	OutgoingForPlayer = Outgoing[player]

	if not OutgoingForPlayer.Unreliable[id] then
		OutgoingForPlayer.Unreliable[id] = {}
	end

	local OutgoingMimic = Copy(OutgoingForPlayer.Unreliable, true) -- Reusing the deep copy function
	table.insert(OutgoingMimic[id], args)

	local PacketSize = RemotePacketSizeCounter.GetPacketSize({
		RunContext = "Server",
		RemoteType = "RemoteEvent",
		PacketData = { OutgoingMimic },
	})

	if PacketSize >= 840 then
		Unreliable:FireClient(player, { [id] = { args } })
	else
		table.insert(OutgoingForPlayer.Unreliable[id], args)
	end
end

This should also resolve the related issue of batching and eliminate a bug where the OutgoingForPlayer variable was incorrectly referenced as nil due to the variable not being properly updated.

@jmesrje jmesrje closed this as completed Aug 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants