How to get components with Knit

Requiring the Components is one way of doing it until it returns the same instance. I personally require the buttons in the script Iā€™m using them in. For example:

-- KnitClientRuntime.client.lua
Knit.Components = script.Parent.Components

Knit.AddControllersDeep(script.Parent.Controllers)
Knit.Start():andThen(function()
     for _,v in pairs(componentDir:GetChildren()) do
        if (v:IsA("ModuleScript")) then
            require(v)
        end
     end
end):catch(warn)

-- SomeController.lua
local SomeComponent; -- Instances with comp Tag

SomeController:KnitStart()
     SomeComponent = require(Knit.Components.Core.SomeComponent)

    SomeComponent:WaitForInstance(yourInstance):andThen(function(instanceComp)
       instanceComp:yourCompMethod()
    end)
end)
1 Like

Try to think of them as ā€œreusable pieces of codeā€. For instance, lets say a TextButton with a Button Tag. I can construct hover methods in the button class, within a function named ā€œyourCompMethodā€, and return the hover for every button or retrieving mouse clicks with our comp methods.

Incredibly useful.

1 Like

Ah so it just combines OOP and CollectionService. Thatā€™s neat

1 Like

You are indeed correct! It has tons of dev-oriented features, which all comes with practice. A few months should get you somewhat knowledgeable with Knit.

1 Like