Skip to content
This repository has been archived by the owner on Nov 30, 2023. It is now read-only.
/ Knit Public archive
forked from Sleitnick/Knit

A modified version of Knit with parallelization support.

License

Notifications You must be signed in to change notification settings

Lucke0051/Knit

 
 

Repository files navigation

Knit is a lightweight game framework. This version is an experimental version with parallelization support. The original framework can be found at sleitnick/knit.

Usage

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.

Managers

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

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 GetManagers 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.

Example

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")

Parallelization & answers

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 BindableFunctions 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 Managers.

API

ManagerCollection

The ManagerCollection should only be accessed from Managers.

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"))

ManagerCollection.GetService(serviceName: string)

Example:

local ExampleService = ManagerCollection.GetService("ExampleService")
local receivedData = ExampleService.Answer:Hello("world")

ManagerCollection.GetController(controllerName: string)

Only avalible on clients. Example:

local ExampleController = ManagerCollection.GetController("ExampleController")
local receivedData = ExampleController.Answer:Hello("world")

ManagerCollection.GetManager(managerName: string)

Used to access other Managers from a manager, does not travel through client <-> server. Example:

local OtherManager = ManagerCollection.GetManager("OtherManager")
OtherManager:Hello("world")

ManagerCollection.CreateManager(manager: Manager)

Runs in a synchronized context.

Used to create/register a Manager. Works similarly to services and controllers. Example:

local ExampleManager = ManagerCollection.CreateManager({
	Name = "ExampleManager",
})

About

A modified version of Knit with parallelization support.

Resources

License

Stars

Watchers

Forks