{"id":"joshuaclarkelua/knit","name":"knit","scope":"joshuaclarkelua","platform":"roblox","description":"(FORKED) Knit is a lightweight game framework","version":"1.7.3","latest":"1.7.3","versions":["1.7.1","1.7.2","1.7.3"],"license":"MIT","licenseRating":"safe","licenseCaveats":["The package archive does not include its license text; the license is declared in its manifest metadata."],"licenseVerified":false,"dependencies":{"joshuaclarkelua/comm":{"version":"^1.0.0","alias":"Comm"},"joshuaclarkelua/promise":{"version":"^4.0.0","alias":"Promise"}},"integrity":"beba84624db6c1f3b8c3615ca1416b7c70abc990194d0ec6c765274a95b06a7f","likes":0,"downloads":0,"install":"forest install joshuaclarkelua/knit","url":"https://forest.dev/p/roblox/joshuaclarkelua/knit","files":"https://api.forest.dev/ai/package/roblox/joshuaclarkelua/knit/files","readme":"[![Release](https://github.com/Sleitnick/Knit/actions/workflows/release.yaml/badge.svg)](https://github.com/Sleitnick/Knit/actions/workflows/release.yaml)\n[![CI](https://github.com/Sleitnick/Knit/actions/workflows/ci.yaml/badge.svg)](https://github.com/Sleitnick/Knit/actions/workflows/ci.yaml)\n[![Docs](https://github.com/Sleitnick/Knit/actions/workflows/docs.yaml/badge.svg)](https://github.com/Sleitnick/Knit/actions/workflows/docs.yaml)\n\n# Knit\n\nKnit 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.\n\nRead the [documentation](https://sleitnick.github.io/Knit/) for more info.\n\n## Install\n\nInstalling Knit is very simple. Just drop the module into ReplicatedStorage. Knit can also be used within a Rojo project.\n\n**Roblox Studio workflow:**\n\n1. Get [Knit](https://www.roblox.com/library/5530714855/Knit) from the Roblox library.\n1. Place Knit directly within ReplicatedStorage.\n\n**Wally & Rojo workflow:**\n\n1. Add Knit as a Wally dependency (e.g. `Knit = \"sleitnick/knit@^1\"`)\n1. Use Rojo to point the Wally packages to ReplicatedStorage.\n\n## Basic Usage\n\nThe 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.\n\nThe most basic usage would look as such:\n\n```lua\nlocal Knit = require(game:GetService(\"ReplicatedStorage\").Packages.Knit)\n\nKnit.Start():catch(warn)\n-- Knit.Start() returns a Promise, so we are catching any errors and feeding it to the built-in 'warn' function\n-- You could also chain 'await()' to the end to yield until the whole sequence is completed:\n--    Knit.Start():catch(warn):await()\n```\n\nThat 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.\n\n### A Simple Service\n\nA service is simply a structure that _serves_ 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:\n\n```lua\nlocal Knit = require(game:GetService(\"ReplicatedStorage\").Packages.Knit)\n\n-- Create the service:\nlocal MoneyService = Knit.CreateService {\n\tName = \"MoneyService\",\n}\n\n-- Add some methods to the service:\n\nfunction MoneyService:GetMoney(player)\n\t-- Do some sort of data fetch\n\tlocal money = someDataStore:GetAsync(\"money\")\n\treturn money\nend\n\nfunction MoneyService:GiveMoney(player, amount)\n\t-- Do some sort of data fetch\n\tlocal money = self:GetMoney(player)\n\tmoney += amount\n\tsomeDataStore:SetAsync(\"money\", money)\nend\n\nKnit.Start():catch(warn)\n```\n\nNow 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. We _could_ create a controller, but it's not necessary for this example.\n\nFirst, we need to expose a method to the client. We can do this by writing methods on the service's Client table:\n\n```lua\n-- Money service on the server\n...\nfunction MoneyService.Client:GetMoney(player)\n\t-- We already wrote this method, so we can just call the other one.\n\t-- 'self.Server' will reference back to the root MoneyService.\n\treturn self.Server:GetMoney(player)\nend\n...\n```\n\nWe can write client-side code to fetch money from the service:\n\n```lua\n-- Client-side code\nlocal Knit = require(game:GetService(\"ReplicatedStorage\").Packages.Knit)\nKnit.Start():catch(warn):await()\n\nlocal MoneyService = Knit.GetService(\"MoneyService\")\n\nMoneyService:GetMoney():andThen(function(money)\n\tprint(money)\nend)\n```\n\nUnder 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.\n","readmeBorrowedFrom":{"version":"1.7.1","source":"wally"},"readmeTruncated":false}