KnitClient
Types
Middleware
ClientMiddlewareFn
type
ClientMiddlewareFn =
(
args:
{
any
}
)
→
(
shouldContinue:
boolean
,
...:
any
)
For more info, see ClientComm documentation.
ClientMiddleware
An array of client middleware functions.
PerServiceMiddleware
ControllerDef
interface
ControllerDef {
Name:
string
[any]:
any
}
Used to define a controller when creating it in CreateController
.
Controller
interface
Controller {
Name:
string
[any]:
any
}
KnitOptions
interface
KnitOptions {
}
ServicePromises
defaults totrue
and indicates if service methods use promises.-
Each service will go through the defined middleware, unless the service
has middleware defined in
PerServiceMiddleware
.
Properties
Player
This item is read only and cannot be modified. Read OnlyKnitClient.Player:
Player
Reference to the LocalPlayer.
Util
This item is read only and cannot be modified. Read OnlyKnitClient.Util:
Folder
References the Util folder. Should only be accessed when using Knit as a standalone module. If using Knit from Wally, modules should just be pulled in via Wally instead of relying on Knit's Util folder, as this folder only contains what is necessary for Knit to run in Wally mode.
Functions
CreateController
Creates a new controller.
caution
Controllers must be created before calling Knit.Start()
.
-- Create a controller
local MyController = Knit.CreateController {
Name = "MyController",
}
function MyController:KnitStart()
print("MyController started")
end
function MyController:KnitInit()
print("MyController initialized")
end
AddControllers
Requires all the modules that are children of the given parent. This is an easy way to quickly load all controllers that might be in a folder.
Knit.AddControllers(somewhere.Controllers)
AddControllersDeep
Requires all the modules that are descendants of the given parent.
GetService
Types
interface
Service {
[any]:
any
}
Returns a Service object which is a reflection of the remote objects within the Client table of the given service. Throws an error if the service is not found.
If a service's Client table contains RemoteSignals and/or RemoteProperties, these values are reflected as ClientRemoteSignals and ClientRemoteProperties.
-- Server-side service creation:
local MyService = Knit.CreateService {
Name = "MyService",
Client = {
MySignal = Knit.CreateSignal(),
MyProperty = Knit.CreateProperty("Hello"),
},
}
function MyService:AddOne(player, number)
return number + 1
end
-------------------------------------------------
-- Client-side service reflection:
local MyService = Knit.GetService("MyService")
-- Call a method:
local num = MyService:AddOne(5) --> 6
-- Fire a signal to the server:
MyService.MySignal:Fire("Hello")
-- Listen for signals from the server:
MyService.MySignal:Connect(function(message)
print(message)
end)
-- Observe the initial value and changes to properties:
MyService.MyProperty:Observe(function(value)
print(value)
end)
caution
Services are only exposed to the client if the service has remote-based
content in the Client table. If not, the service will not be visible
to the client. KnitClient.GetService
will only work on services that
expose remote-based content on their Client tables.
GetController
Gets the controller by name. Throws an error if the controller is not found.
GetControllers
Gets a table of all controllers.
Start
Starts Knit. Should only be called once per client.
Knit.Start():andThen(function()
print("Knit started!")
end):catch(warn)
By default, service methods exposed to the client will return promises.
To change this behavior, set the ServicePromises
option to false
:
Knit.Start({ServicePromises = false}):andThen(function()
print("Knit started!")
end):catch(warn)
OnStart
KnitClient.
OnStart
(
) →
Promise
Returns a promise that is resolved once Knit has started. This is useful
for any code that needs to tie into Knit controllers but is not the script
that called Start
.
Knit.OnStart():andThen(function()
local MyController = Knit.GetController("MyController")
MyController:DoSomething()
end):catch(warn)