TypedRemote
Simple networking package that helps create typed RemoteEvents and RemoteFunctions.
-- ReplicatedStorage.Network (ModuleScript)
local TypedRemote = require(ReplicatedStorage.Packages.TypedRemote)
-- Get the RF and RE instance creators, which create RemoteEvents/RemoteFunctions
-- within the given parent (the script in this case):
local RF, RE = TypedRemote.parent(script)
-- Redeclare the TypedRemote types for simplicity:
type RF<T..., R...> = TypedRemote.Function<T..., R...>
type RE<T...> = TypedRemote.Event<T...>
-- Define network table:
return {
-- RemoteEvent that takes two arguments - a string and a number:
MyEvent = RE("MyEvent") :: RE<string, number>,
-- RemoteFunction that takes two arguments (boolean, string) and returns a number:
MyFunc = RF("MyFunc") :: RF<(boolean, string), (number)>,
}
-- Example usage of the above Network module:
local Network = require(ReplicatedStorage.Network)
-- If you type this out, intellisense will help with what the function signature should be:
Network.MyEvent.OnClientEvent:Connect(function(player, str, num)
-- Foo
end)
In most cases, the TypedRemote.parent()
function will be used to create the memoized
RemoteFunction and RemoteEvent builder functions. From there, call the given functions
with the desired name per remote.
The TypedRemote.func
and TypedRemote.event
functions can also be used, but the
parent must be supplied to each call, hence the helpful parent()
memoizer.
Types
Event<T...>
interface
Event<T...> {
OnServerEvent:
PlayerSignal
<
T...
>
,
FireAllClients:
(
self:
Event
<
T...
>
,
T...
)
→
(
)
,
FireServer:
(
self:
Event
<
T...
>
,
T...
)
→
(
)
,
}
Function<T..., R...>
interface
Function<T..., R...> {
InvokeServer:
(
self:
Function
<
T...
,
R...
>
,
T...
)
→
R...,
}
Functions
parent
Creates a memoized version of the func
and event
functions that include the parent
in each call.
-- Create RF and RE functions that use the current script as the instance parent:
local RF, RE = TypedRemote.parent(script)
local remoteFunc = RF("RemoteFunc")
func
Creates a RemoteFunction with name
and parents it inside of parent
.
If the parent
argument is not included or is nil
, then it defaults to the parent of
this TypedRemote ModuleScript.
event
Creates a RemoteEvent with name
and parents it inside of parent
.
If the parent
argument is not included or is nil
, then it defaults to the parent of
this TypedRemote ModuleScript.