Knit is a lightweight game framework. This version is an experimental version with parallelization support. The original framework can be found at sleitnick/knit.
This is is a fork of sleitnick/knit. All the standard Knit documentation is avalible at sleitnick.github.io/Knit. Only APIs that do not exist in standard Knit is documented here.
Similarly to services and controllers, this fork implements a new concept, managers. A manager is like a service or controller, but they run on a separate actor.
Current manager limitations:
- No direct client <-> (nor ->) manager communication
- Managers do not fully respect the
KnitInit
&KnitStart
lifecycle
(Registration & creation are used interchangeably)
Managers will not be registered before KnitInit
, but they may register before or after KnitStart
. A manager is ready to access services and controllers as soon as a manager is registered. You may GetManager
s in KnitStart
, but it is not safe to access manager methods in KnitStart
, nor KnitInit
.
KnitManagerCollection
will not be parented to ServerScriptService
(or ReplicatedStorage
on clients) until it is ready, WaitForChild
will yield until the module has been parented.
local ServerScriptService = game:GetService("ServerScriptService")
local ManagerCollection = require(ServerScriptService:WaitForChild("KnitManagerCollection"))
local ExampleManager = ManagerCollection.CreateManager({
Name = "ExampleManager",
})
function ExampleManager:Compute(data)
task.desynchronize()
local result
for key, value in data do
--proccess
end
return result
end
local ExampleService = ManagerCollection.GetService("ExampleService")
ExampleService:Hello("world")
Note: whenever a function cannot be run in a desynchronized context, the ManagerCollection
will automatically call task.synchronize()
. This is intentional. If you think this is an undesired behaviour, please open an issue.
If you ran the following code in a manager, the method would be called, but all return values would be discarded. The entirety of this code block can be run in a desynchronized context.
local ExampleService = ManagerCollection.GetService("ExampleService")
ExampleService:Hello("world")
If you want to get the return values, you must access the method via the Answer
subtable, this tells the ManagerCollection
that you want the return values. It is important that you are aware that this will yield until due to the nature of BindableFunction
s that are used under the hood when calling via the Answer
subtable.
local ExampleService = ManagerCollection.GetService("ExampleService")
local receivedData = ExampleService.Answer:Hello("world")
Make sure you only use the Answer
subtable when you really need to.
If you want to connect a method in a Manager
directly in parallel, use the Parallel
subtable. You cannot get return values from Parallel
methods.
local Manager = ManagerCollection.GetManager("OtherManager")
Manager.Parallel:Hello("world")
Note that the Parallel
subtable is only avalible on Manager
s.
The ManagerCollection
should only be accessed from Manager
s.
Access the ManagerCollection
on the server via:
local ServerScriptService = game:GetService("ServerScriptService")
local ManagerCollection = require(ServerScriptService:WaitForChild("KnitManagerCollection"))
On the client:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ManagerCollection = require(ReplicatedStorage:WaitForChild("KnitManagerCollection"))
Example:
local ExampleService = ManagerCollection.GetService("ExampleService")
local receivedData = ExampleService.Answer:Hello("world")
Only avalible on clients. Example:
local ExampleController = ManagerCollection.GetController("ExampleController")
local receivedData = ExampleController.Answer:Hello("world")
Used to access other Manager
s from a manager, does not travel through client <-> server.
Example:
local OtherManager = ManagerCollection.GetManager("OtherManager")
OtherManager:Hello("world")
Runs in a synchronized context.
Used to create/register a Manager
. Works similarly to services and controllers.
Example:
local ExampleManager = ManagerCollection.CreateManager({
Name = "ExampleManager",
})