Matter
Check out the adapted version of the Matter Example game to see how it's used in a real game.
See all of YetAnotherNet's example projects in the repository.
- Luau
- Typescript
Being initially made for the Matter ECS, YetAnotherNet provides a simple function for scheduling your Routes to run on your Matter Loop.
Firstly, create a routes.luau
ModuleScript in ReplicatedStorage to strictly declare your Routes.
local YetAnotherNet = require("@shared/routes")
local Route = YetAnotherNet.Route
type Route<U...> = YetAnotherNet.Route<U...>;
local defaultConfiguration = {
Channel = "Reliable",
Event = "default",
}
-- Payload for replicating Entities
type EntityPayload = {
[string]: { -- EntityId
[string]: { -- Component name
data: ComponentInstance<T>
}
}
}
-- Replicate Matter Components
local MatterReplication: Route<EntityPayload> = Route.new(defaultConfiguration)
-- Signal that the Player has loaded
local PlayerLoaded: Route<boolean> = Route.new(defaultConfiguration)
return {
MatterReplication = MatterReplication,
PlayerLoaded = PlayerLoaded,
}
And now in the same script where you create your Matter Loop, you can run the YetAnotherNet.start(Loop, { Route })
function to schedule your Routes to run on Matter's Middleware.
local RunService = game:GetService("RunService")
local Matter = require("@packages/Matter")
local World = Matter.World
local Loop = Matter.Loop
local YetAnotherNet = require("@packages/YetAnotherNet")
local routes = require("@shared/routes")
local world = World.new()
local loop = Loop.new(world)
-- Schedules your Routes
YetAnotherNet.start(loop, routes)
local systems = {}
for _, child in script.systems:GetChildren() do
if child:IsA("ModuleScript") then
table.insert(systems, require(child))
end
end
loop:scheduleSystems(systems) -- Schedule systems after running ``YetAnotherNet.start()``
-- Begin the loop and make sure the ``Event`` key in your Routes configuration are added here
loop:begin({
default = RunService.Heartbeat
})
Finally, in a Matter System we can use our routes.luau
ModuleScript to access our Routes and
use them within our Systems.
local routes = require("@shared/routes")
local PlayerLoaded = routes.PlayerLoaded
local function exampleSystem(world)
-- Query through every networking call that frame on the Server
for i, player, ...data in PlayerLoaded:query() do
-- Do something
end
-- Query through every networking call that frame on the Client
for i, _, ...data in PlayerLoaded:query() do
-- Do something
end
-- Send data from the Client to the Server
PlayerLoaded:send(...data)
-- Send data to a Client from the Server
PlayerLoaded:send(...data):to(Player)
end
Being initially made for the Matter ECS, YetAnotherNet provides a simple function for scheduling your Routes to run on your Matter Loop.
Firstly, create a routes.ts
ModuleScript in ReplicatedStorage to strictly declare your Routes.
import { Route, Configuration } from "@rbxts/yetanothernet";
const defaultConfiguration: Configuration = {
Channel: "Reliable",
Event: "default",
};
// Replicate Matter Components
const MatterReplication = new Route(defaultConfiguration);
// Signal that the Player has loaded
const PlayerLoaded: Route<[boolean]> = new Route(defaultConfiguration);
export = {
MatterReplication: MatterReplication,
PlayerLoaded: PlayerLoaded,
};
And now in the same script where you create your Matter Loop, you can run the Net.start(Loop, { route: Route })
function to schedule your Routes to run on Matter's Middleware.
import { RunService } from "@rbxts/services";
import { Loop, System } from "@rbxts/matter";
import Net from "@rbxts/yetanothernet";
import routes from "shared/routes";
const loop = new Loop();
// Schedules your Routes
Net.start(loop, routes);
// Schedule systems
const systems: System<[]>[] = [];
loop.scheduleSystems(systems);
// Begin the loop and make sure the ``Event`` key in your Routes configuration are added here
loop.begin({
default: RunService.Heartbeat
});
Finally, in a Matter System we can use our routes.ts
ModuleScript to access our Routes and
use them within our Systems.
import routes from "shared/routes";
const PlayerLoaded = routes.PlayerLoaded;
function exampleSystem(world) {
// Query through every networking call that frame on the Server
for (const [pos, player, bool] of PlayerLoaded.query()) {
// Do something
}
// Query through every networking call that frame on the Client
for (const [pos, _, bool] of PlayerLoaded.query()) {
// Do something
}
// Send data from the Client to the Server
PlayerLoaded.send(true);
// Send data to a Client from the Server
PlayerLoaded.send(true).to(Player);
}
export = {
system: exampleSystem
}