Simple Knit Runtime

Knit Runtime

  • This module allows you to create services/controllers and components without requiring knit. On AGF getting other module creates/overwrite __index function, which is a problem because I sometimes create my own __index.

  • Added tables are Knit.Shared and Knit.Module; shared are the components on ReplicatedStorage, while modules is the given argument on SetupContorller(...).

  • Allows you to access components without requiring them, simply use Knit.Shared.SharedComponent or Knit.Module.TestComponent.

  • You no longer need to add Name when creating a service/controllers. If a name is already present does not change the name.

How to use Knit Runtime

Client
repeat
	task.wait()
until game:IsLoaded()

-- Knit Runtime Directory
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local KnitRuntime = require(ReplicatedStorage.Packages.Runtime)

local PlayerScripts = game:GetService("Players").LocalPlayer.PlayerScripts

-- Controllers and Components Directory
local Source = PlayerScripts:WaitForChild("Source")

-- Creates a Module table on Knit
KnitRuntime:SetModule(Source:WaitForChild("Modules"))

--[[
	Similar to Knit.AddServices this function has a second argument a boolean
	which requires all the modules that are descendants of the given parent.
]]
KnitRuntime:SetupControllers(Source:WaitForChild("Controllers"))

KnitRuntime.Start():catch(warn)
Server
-- Knit Runtime Directory
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local KnitRuntime = require(ReplicatedStorage.Packages.Runtime)

-- Services and Component's Directory
local Source = game:GetService("ServerStorage").Source

-- Creates a Module table on Knit
KnitRuntime:SetModule(Source.Modules)

--[[
	Similar to Knit.AddServices this function has a second argument a boolean
	which requires all the modules that are descendants of the given parent.
]]
KnitRuntime:SetupServices(Source.Services)

KnitRuntime.Start():catch(warn)

Examples

Controller
local TestComponent, TestService, TestShared

local TestController = {}

--[[
	This function allows you to use Knit
]]
function TestController:GetKnit(Knit)
	TestComponent = Knit.Module.TestComponent
	TestShared = Knit.Shared.TestShared
end

--[[
	This is necessarry; if we get a controller/service
	on self.GetKnit, we will just get an error:
	"Cannot call GetController/GetService until Knit has been started"
]]
function TestController:GetKnitOnStart(Knit)
	TestService = Knit.GetService("TestService")
end

return TestController
Service
local TestComponent, OtherService

local TestService = { Client = {} }

--[[
	This function allows you to use Knit
]]
function TestService:GetKnit(Knit)
	TestComponent = Knit.Shared.TestComponent
	TestShared = Knit.Shared.TestShared

	-- We can also add Signals/Property on the client table
	self.Client.DoSomething = Knit.CreateSignal()
end

--[[
	This is necessarry; if we get a service
	on self.GetKnit, we will just get an error:
	"Cannot call GetService until Knit has been started"
]]
function TestService:GetKnitOnStart(Knit)
	OtherService = Knit.GetService("OtherService")
end

return TestService
Component/Shared
-- Similar to service/controller
local OtherService, TestShared

local TestComponent = {}

function TestComponent:GetKnit(Knit)
	TestShared = Knit.Shared.TestShared
end

function TestComponent:GetKnitOnStart(Knit)
	OtherService = Knit.GetService("OtherService")
end

return TestComponent

I really hope that they add KnitStart(Knit) and KnitInit(Knit), I mean it’s really simple:
Screenshot_3

3 Likes

Really cool, I look forward to trying this sometime.

1 Like