{"id":"pepeeltoro41/wired","name":"wired","scope":"pepeeltoro41","platform":"roblox","description":"Lightweight Remote Wrapper for Roblox","version":"0.1.3","latest":"0.1.3","versions":["0.1.1","0.1.2","0.1.3"],"license":"MIT","licenseRating":"safe","licenseCaveats":[],"licenseVerified":true,"dependencies":{"evaera/promise":{"version":"^4.0.0","alias":"Promise"}},"integrity":"3d2744a8873588bbc09a1f028f3668e16451f4079a3882dfc759648b1bd5e4f8","likes":0,"downloads":0,"install":"forest install pepeeltoro41/wired","url":"https://forest.dev/p/roblox/pepeeltoro41/wired","files":"https://api.forest.dev/ai/package/roblox/pepeeltoro41/wired/files","readme":"# Wired is a lightweight remote wrapper for Roblox\r\n\r\nWired abstracts roblox remotes for you to use them in Roblox-ts offering fully typed remotes you can use in your project!\r\n\r\n### Changelog 0.1.0\r\n\r\n-   Added `FakeFire(player)` for simulating a player firing a **ClientEvent** for testing purposes\r\n-   Added `FakeCall(player)` for simulating a player calling a **ServerAsync** for testing purposes, returns what the server returned\r\n-   Added `FakeFire()` for simulating the server firing a **ServerEvent** for testing purposes\r\n-   Changed function name from `DefineRemotes()` to `DeclareRemotes()`\r\n-   Added `OnPrepared` signal\r\n\r\n## Declaring your remotes\r\n\r\nFirst declare your remotes with `const remotes = DeclareRemotes(name, declaration)`. You might want to export the returned value to be used somewhere else. It's important that the export is available for both Server and Client\r\n\r\n```ts\r\nimport { DeclareRemotes } from \"@rbxts/wired\";\r\n\r\nexport const Remotes = DeclareRemotes(\"Remotes\", {\r\n\t//Your declarations here\r\n});\r\n\r\nconst ServerRemotes = Remotes.Server;\r\nconst ClientRemotes = Remotes.Client;\r\n```\r\n\r\nWe can define as much remotes as we want in different files. This can be useful for modularity, but, depending on your project, creating everything in the same file might be easier to manage\r\n\r\n## Constructing the Roblox instances\r\n\r\nAfter we declared our remotes, it's important that we construct the corresponding roblox `RemoteEvent` and `RemoteFunction` instances. For this we are gonna call `Remotes.Server.Construct()` on the Server.\r\n(_If we use DeclareRemotes() on more files, you need to call it on each one_)\r\n\r\n```ts\r\nimport { Remotes } from \"\"; //Remote Declarations path\r\n\r\nRemotes.Server.Construct();\r\n//Created instances can be found in ReplicatedStorage._wired\r\n```\r\n\r\nWe can use these declarations before the instances have been constructed and anything we do will be queued up until the instances exist. If an action has been queued up for more than 7 seconds, wired will warn about it.\r\n\r\nThis is mainly so we can get our remotes at the top of our scripts without wired yielding or erroring if the instances haven't been created. It's recommended to call `Construct()` as soon as possible.\r\n\r\n## Replicating Remotes to the Client\r\n\r\nWhen our remotes have been created on the Server, we need the client to wait for these instances before using them. For this we need to call `Remotes.Client.Prepare()` this function returns a `Promise` that resolves when all the instances are ready to be used\r\n\r\n```ts\r\nimport { Remotes } from \"\"; //Remote Declarations path\r\n\r\nRemotes.Client.Prepare().andThen(() => {\r\n\tprint(\"Instances are ready!\");\r\n});\r\n//Created instances can be found in ReplicatedStorage._wired\r\n```\r\n\r\nSimilar to Server, anything we do will be queued up until the instances exist, wired will warn you if it's been waiting for more than 7 seconds. It's recommended to call `Remotes.Client.Prepare` as soon as possible. You can use the returned promise for creating a loading screen\r\n\r\n-   **[ WARNING ]**  \r\n    In the **Server** context `Remotes.Client` will be nil, and similarly In the **Client** `Remotes.Server` wil be nil. Roblox-ts wont be able to check this\r\n\r\n## Using your remotes\r\n\r\nWe can use `Remotes.Get(remote: string)` to find a remote\r\nLet's see all the remotes we can create\r\n\r\n```ts\r\nimport { DeclareRemotes, ServerAsync, ServerEvent, ClientEvent, RemoteEvent } from \"@rbxts/wired\";\r\n\r\nexport const Remotes = DeclareRemotes(\"Remotes\", {\r\n\tDoubleNumber: ServerAsync<(num: number) => number>(),\r\n\tOnDamageTaken: ServerEvent<(damage: number) => void>(),\r\n\tOnPlayerLoaded: ClientEvent<() => void>(),\r\n\r\n\t//First generic is for server, second one for client\r\n\tOnMessageSent: RemoteEvent<(from: Player, msg: string) => void, (msg: string) => void>(),\r\n});\r\n```\r\n\r\n-   **[ WARNING ]**  \r\n    The generic you give for the remotes is useful for type inference, but wired **will not** typecheck the remotes on runtime. Any check needs to be done by you to avoid exploiters\r\n\r\n#### ServerAsync\r\n\r\n`RemoteFunction` that clients can call to the server expecting it to return something\r\n\r\n```ts\r\nimport { Remotes } from \"\"; //Remote Declarations path\r\n\r\n//Server\r\nconst DoubleNumber = Remotes.Server.Get(\"DoubleNumber\");\r\n\r\nDoubleNumber.SetCallback((player, num) => {\r\n\treturn num * 2;\r\n});\r\n\r\n//Client\r\nconst DoubleNumber = Remotes.Client.Get(\"DoubleNumber\");\r\n\r\nAsyncFunction.CallServer(5).andThen((double) => print(double)); //10\r\n```\r\n\r\n#### ServerEvent\r\n\r\n`RemoteEvent` that a server can fire to clients\r\n\r\n```ts\r\nimport { Remotes } from \"\"; //Remote Declarations path\r\n\r\n//Server\r\nconst OnDamageTaken = Remotes.Server.Get(\"OnDamageTaken\");\r\n\r\nOnDamageTaken.FirePlayer(player, 50);\r\nOnDamageTaken.FireAll(50);\r\nOnDamageTaken.FireExcept(player, 50);\r\nOnDamageTaken.FireFor((p) => {\r\n\t//return true to fire, false to omit\r\n\treturn p == player;\r\n}, 50);\r\n\r\n//Client\r\nconst OnDamageTaken = Remotes.Client.Get(\"OnDamageTaken\");\r\n\r\nconst connection = OnDamageTaken.Connect((damage) => {\r\n\tprint(\"Ouch!\", damage);\r\n});\r\nconnection.Disconnect();\r\n```\r\n\r\n#### ClientEvent\r\n\r\n`RemoteEvent` that clients can fire to the server\r\n\r\n```ts\r\nimport { Remotes } from \"\"; //Remote Declarations path\r\n\r\n//Server\r\nconst OnPlayerLoaded = Remotes.Server.Get(\"OnPlayerLoaded\");\r\n\r\nconst connection = OnPlayerLoaded.Connect((player) => {\r\n\tprint(\"Player is ready to play!\");\r\n});\r\nconnection.Disconnect();\r\n\r\n//Client\r\nconst OnPlayerLoaded = Remotes.Client.Get(\"OnPlayerLoaded\");\r\n\r\ngame.IsLoaded() || game.Loaded.Wait();\r\nRemotes.Prepare().await();\r\n\r\nOnPlayerLoaded.FireServer();\r\n```\r\n\r\n#### RemoteEvent\r\n\r\nBidirectional `RemoteEvent` that both the Server and Client can Fire or Connect. Methods like `FireAll`, `FireExcept`, `FireFor` are available here for the server too\r\n\r\n```ts\r\nimport { Remotes } from \"\"; //Remote Declarations path\r\n\r\n//Server\r\nconst OnMessageSent = Remotes.Server.Get(\"OnMessageSent\");\r\n\r\nOnMessageSent.Connect((player, msg) => {\r\n\t// broadcast to all players except the one who sent the message\r\n\tOnMessageSent.FireExcept(player, msg);\r\n});\r\n\r\n//Client\r\nconst OnMessageSent = Remotes.Client.Get(\"OnMessageSent\");\r\n\r\nOnMessageSent.FireServer(\"Hello!\");\r\n\r\nOnMessageSent.Connect((player, msg) => {\r\n\tprint(player.Name, \"says:\", msg);\r\n});\r\n```\r\n\r\n### Unreliable Events\r\n\r\nRemoteEvents have their unreliable equivalents. They have the same typings and work the same way, except they use `UnreliableRemoteEvent`s.\r\n\r\n`UnreliableClient -> ClientEvent`\r\n`UnreliableServer -> ServerEvent`\r\n`UnreliableEvent -> RemoteEvent`\r\n\r\n## Namespaces\r\n\r\nNamespaces are useful for grouping your remotes, you can create namespaces directly inside of your remote declarations, and you can create namespaces inside namespaces.\r\n\r\nYou can separate namespaces in different files as an alternative to calling DeclareRemotes() on different files\r\n\r\n```ts\r\nimport { DeclareRemotes, Namespace, ServerAsync } from \"@rbxts/wired\";\r\n\r\nexport const Remotes = DeclareRemotes(\"Remotes\", {\r\n\tShopRemotes: Namespace({\r\n\t\tBuyRequest: ServerAsync(),\r\n\t}),\r\n\tGameplayRemotes: Namespace({\r\n\t\t//Remotes here\r\n\t}),\r\n\tNamespaceRemotes: Namespace({\r\n\t\tSubNamespace1: Namespace({\r\n\t\t\t//Remotes here\r\n\t\t}),\r\n\t\tSubNamespace2: Namespace({\r\n\t\t\t//Remotes here\r\n\t\t}),\r\n\t}),\r\n});\r\n```\r\n\r\nYou can access these namespaces with the `Remotes.Namespace()` method\r\n\r\n```ts\r\nimport { Remotes } from \"\"; //Remote Declarations path\r\n\r\n//Server\r\nconst ShopRemotes = Remotes.Server.Namespace(\"ShopRemotes\");\r\nconst BuyRequest = ShopRemotes.Get(\"BuyRequest\");\r\nBuyRequest.SetCallback();\r\n\r\n//Client\r\nconst ShopRemotes = Remotes.Client.Namespace(\"ShopRemotes\");\r\nconst BuyRequest = ShopRemotes.Get(\"BuyRequest\");\r\nBuyRequest.CallServer();\r\n```\r\n","readmeTruncated":false}