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:
- Channel
- Event
All Routes run on one of two channels: Reliable & Unreliable.
These channels change the way your packets are sent over the network.
Channel | Description |
---|---|
Reliable | Packets are never dropped, and are always ordered. |
Unreliable | Packets may be dropped per frame, but are always ordered per frame. |
The Event
field is only used when you're using the Matter ECS.
This is used internally in the YetAnotherNet.start(loop, { routes })
hook to schedule your Routes.
You can safely ignore this field if you do not use the YetAnotherNet.start(loop, { routes })
hook.
Type-checking
Routes can be Type-checked to provide intellisense and auto-completion by providing a type annotation when creating your Routes.
- Luau
- Typescript
local YetAnotherNet = require("@packages/YetAnotherNet")
local Route = YetAnotherNet.Route
type Route<U...> = YetAnotherNet.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.
import { Route } from "@rbxts/yetanothernet";
const route: Route<[number, boolean, string]> = new Route({
Channel: "Reliable",
Event: undefined,
});
// Send will expect the types annotated
route.send(1, "Hello, world!", true)
// The returned arguments are typed
for (const [pos, sender, num, str, bool] of route.query()) {
// Do something
}
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
- Luau
- Typescript
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 YetAnotherNet = require("@packages/YetAnotherNet")
local Route = YetAnotherNet.Route
type Route<U...> = YetAnotherNet.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: Route<number, string, boolean> = Route.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)
.
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.
import Route from "@rbxts/yetanothernet";
const route: Route<[number, boolean, string]> = new Route({
Channel: "Reliable",
Event: undefined,
});
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
- Luau
- Typescript
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 YetAnotherNet = require("@packages/YetAnotherNet")
local Route = YetAnotherNet.Route
type Route<U...> = YetAnotherNet.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 | YetAnotherNet.server
in the arguments.
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.
import { Route } from "@rbxts/yetanothernet";
const route: Route<[number, boolean, string]> = new Route({
Channel: "Reliable",
Event: undefined,
});
for (const [pos, sender, num, str, bool] of route.query()) {
// Do something
}
When you call the query
function, 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 function 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.