- Notifications
You must be signed in to change notification settings - Fork99
Lightweight game framework for Roblox
License
Sleitnick/Knit
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Knit has been archived and will no longer receive updates.
Pleaseread here for more information.
Knit is a lightweight framework for Roblox that simplifies communication between core parts of your game and seamlessly bridges the gap between the server and the client.
Read thedocumentation for more info.
Installing Knit is very simple. Just drop the module into ReplicatedStorage. Knit can also be used within a Rojo project.
Roblox Studio workflow:
- GetKnit from the Roblox library.
- Place Knit directly within ReplicatedStorage.
Wally & Rojo workflow:
- Add Knit as a Wally dependency (e.g.
Knit = "sleitnick/knit@^1"
) - Use Rojo to point the Wally packages to ReplicatedStorage.
The core usage of Knit is the same from the server and the client. The general pattern is to create a single script on the server and a single script on the client. These scripts will load Knit, create services/controllers, and then start Knit.
The most basic usage would look as such:
localKnit=require(game:GetService("ReplicatedStorage").Packages.Knit)Knit.Start():catch(warn)-- Knit.Start() returns a Promise, so we are catching any errors and feeding it to the built-in 'warn' function-- You could also chain 'await()' to the end to yield until the whole sequence is completed:-- Knit.Start():catch(warn):await()
That would be the necessary code on both the server and the client. However, nothing interesting is going to happen. Let's dive into some more examples.
A service is simply a structure thatserves some specific purpose. For instance, a game might have a MoneyService, which manages in-game currency for players. Let's look at a simple example:
localKnit=require(game:GetService("ReplicatedStorage").Packages.Knit)-- Create the service:localMoneyService=Knit.CreateService {Name="MoneyService",}-- Add some methods to the service:functionMoneyService:GetMoney(player)-- Do some sort of data fetchlocalmoney=someDataStore:GetAsync("money")returnmoneyendfunctionMoneyService:GiveMoney(player,amount)-- Do some sort of data fetchlocalmoney=self:GetMoney(player)money+=amountsomeDataStore:SetAsync("money",money)endKnit.Start():catch(warn)
Now we have a little MoneyService that can get and give money to a player. However, only the server can use this at the moment. What if we want clients to fetch how much money they have? To do this, we have to create some client-side code to consume our service. Wecould create a controller, but it's not necessary for this example.
First, we need to expose a method to the client. We can do this by writing methods on the service's Client table:
-- Money service on the server...functionMoneyService.Client:GetMoney(player)-- We already wrote this method, so we can just call the other one.-- 'self.Server' will reference back to the root MoneyService.returnself.Server:GetMoney(player)end...
We can write client-side code to fetch money from the service:
-- Client-side codelocalKnit=require(game:GetService("ReplicatedStorage").Packages.Knit)Knit.Start():catch(warn):await()localMoneyService=Knit.GetService("MoneyService")MoneyService:GetMoney():andThen(function(money)print(money)end)
Under the hood, Knit is creating a RemoteFunction bound to the service's GetMoney method. Knit keeps RemoteFunctions and RemoteEvents out of the way so that developers can focus on writing code and not building networking infrastructure.
About
Lightweight game framework for Roblox