Skip to main content

Routes

Routes are the core component of YetAnotherNet, these objects are how you send or receive data through YetAnotherNet. These are uniquely identified and come with no overhead, so you're encouraged to create as many as you need as if you were creating individual RemoteEvents.

Configuration

There are a few ways you can configure your Routes:

All Routes run on one of two channels: Reliable & Unreliable.
These channels change the way your packets are sent over the network.

ChannelDescription
ReliablePackets are never dropped, and are always ordered.
UnreliablePackets may be dropped per frame, but are always ordered per frame.

Type-checking

Routes can be Type-checked to provide intellisense and auto-completion by providing a type annotation when creating your Routes.

local Net = require("Net.luau")

local Route = Net.Route
type Route<U...> = Net.Route<U...>;

local route: Route<number, string, boolean> = Route.new()

-- Send will expect the types annotated
route:send(1, "Hello, world!", true)

-- The returned arguments are typed
for pos, num, str, bool in route:query() do
-- Do something
end

In this example, the Route is annotated with the types number, string, boolean, this means that our methods like Route:query() and Route:send() will return or expect these types.

This will enable auto-completion and type-checking when working with your Routes, as such it is encouraged you enable Strict Typing to compliment these features.

Sending

You can use Route:send(...data) to send data over the Route. This data will be processed next frame, which is when you'll be able to query it on the receiving end.

local Net = require("Net.luau")

local Route = Net.Route
type Route<U...> = Net.Route<U...>;

local route: Route<number, string, boolean> = Route.new()

-- Send will expect the types annotated
route:send(1, "Hello, world!", true)

-- The returned arguments are typed
for pos, num, str, bool in route:query() do
-- Do something
end

local Route: Net<number, string, boolean> = Net.new()

Route:send(5, true, "Hello, world!")

By default, Route:send() will send the Packet of data to all Clients when used on the Server. You can specify a Player or { Player } to send it to by using Route:send():to(recipient).

Querying

Querying is simple, you can query for Packets by using Route:query() which will return an iterator that will output position, sender, ...data in your for loop.

local Net = require("Net.luau")

local Route = Net.Route
type Route<U...> = Net.Route<U...>;

local Route: Route<number, string, boolean> = Route.new()

for pos, sender, num, str, bool in Route:query() do
-- Do something
end

When you call the query method, it will take a snapshot of all Packets from the previous frame and filter it for you, such as only returning Packets from the Route the method was called on. Or, you can also filter out senders by calling Route:query():from(...sender) on the QueryResult and putting any values of type { Player } | Player | Net.server in the arguments.