What's Knit.Start?

Hey I’m Trying To Understand What Exactly Knit.Start Does? I’m Designing My Own
Custom Framework So I Feel It’s Pretty Necessary For Me To Understand.

Knit is a lightweight framework for ROBLOX games, simplfying client-server communication. The Start method probably just initializes the Knit module.

2 Likes

This is your client runtime script that allows all your client side controllers and components to start working.

Knit.AddControllers() -- initializes controllers
Component.Auto() -- initializes components

-- starts Knit on the client given the previously initialized controllers and components
Knit.Start() 

:andThen() is a function of the promise module which can be a bit advanced but it can be learned by reading the documentation or by watching YouTube videos.

If you really want to know exactly what Knit.Start() does, open up the module and look at what’s happening under the hood. I’m pretty sure this is the method, found in KnitClient but I could be wrong:

function KnitClient.Start(options: KnitOptions?)
	if started then
		return Promise.reject("Knit already started")
	end

	started = true

	if options == nil then
		selectedOptions = defaultOptions
	else
		assert(typeof(options) == "table", `KnitOptions should be a table or nil; got {typeof(options)}`)
		selectedOptions = options
		for k, v in defaultOptions do
			if selectedOptions[k] == nil then
				selectedOptions[k] = v
			end
		end
	end
	if type(selectedOptions.PerServiceMiddleware) ~= "table" then
		selectedOptions.PerServiceMiddleware = {}
	end

	return Promise.new(function(resolve)
		-- Init:
		local promisesStartControllers = {}

		for _, controller in controllers do
			if type(controller.KnitInit) == "function" then
				table.insert(
					promisesStartControllers,
					Promise.new(function(r)
						debug.setmemorycategory(controller.Name)
						controller:KnitInit()
						r()
					end)
				)
			end
		end

		resolve(Promise.all(promisesStartControllers))
	end):andThen(function()
		-- Start:
		for _, controller in controllers do
			if type(controller.KnitStart) == "function" then
				task.spawn(function()
					debug.setmemorycategory(controller.Name)
					controller:KnitStart()
				end)
			end
		end

		startedComplete = true
		onStartedComplete:Fire()

		task.defer(function()
			onStartedComplete:Destroy()
		end)
	end)
end

Source: Knit/KnitClient.lua at main · Sleitnick/Knit · GitHub

Edit: The equivalent to this in the simplest way possible without using Knit would just be a script that requires all your ModuleScripts like this, Knit just does it in a better way:

require(ModuleScript1)
require(ModuleScript2)
require(ModuleScript3)
3 Likes

So It basically requires and initializes the functions within that modulle?

Yeah basically, but I think “load” is a better way to describe it than “initialize” (sorry for the late reply)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.