MIT License

项目介绍

Sleitnick/Knit 是一个托管在 GitHub 上的开源项目,主要使用 Luau 编写,采用 MIT License 许可证,目前获得 630 个 Star、109 次 Fork。

仓库全名
Sleitnick/Knit
开源许可证
MIT License
Star 数
630
Fork 数
109

README 摘要

:warning: No Longer Maintained :warning: Knit has been archived and will no longer receive updates. Please read here for more information. Knit 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 the documentation for more info. Install Installing Knit is very simple. Just drop the module into…

完整内容见 GitHub 原始 README 或本页 。

常见问题

Knit 用什么语言开发?

主要使用 Luau 编写,采用 MIT License 许可证,目前有 630 个 Star。

Knit 可以免费商用吗?

仓库采用 MIT License 许可证,可免费使用与修改,商业使用请遵守许可证条款(例如保留版权声明与许可声明)。

如何获取 Knit 的源码?

可以直接克隆仓库:git clone https://github.com/Sleitnick/Knit.git,默认分支为 main;安装与运行步骤见仓库 README。

Knit 最近还在维护吗?

仓库最近一次更新为 2024年7月31日,当前没有未关闭的 Issue;Star 数为 630,可作为社区活跃度的参考。

关于

Lightweight game framework for Roblox

描述取自 GitHub 官方 API,中文内容由 GitRead 中文社区 编辑团队整理。

简要分析

热度分
850
价值分
410
活跃状态
归档

语言占比

Luau
Python

Release

README

Release CI Docs

:warning: No Longer Maintained :warning:

Knit has been archived and will no longer receive updates.

Please read here for more information.

Knit

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 the documentation for more info.

Install

Installing Knit is very simple. Just drop the module into ReplicatedStorage. Knit can also be used within a Rojo project.

Roblox Studio workflow:

  1. Get Knit from the Roblox library.
  2. Place Knit directly within ReplicatedStorage.

Wally & Rojo workflow:

  1. Add Knit as a Wally dependency (e.g. Knit = "sleitnick/knit@^1")
  2. Use Rojo to point the Wally packages to ReplicatedStorage.

Basic Usage

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:

local Knit = 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 Simple Service

A 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:

local Knit = require(game:GetService("ReplicatedStorage").Packages.Knit)

-- Create the service:
local MoneyService = Knit.CreateService {
	Name = "MoneyService",
}

-- Add some methods to the service:

function MoneyService:GetMoney(player)
	-- Do some sort of data fetch
	local money = someDataStore:GetAsync("money")
	return money
end

function MoneyService:GiveMoney(player, amount)
	-- Do some sort of data fetch
	local money = self:GetMoney(player)
	money += amount
	someDataStore:SetAsync("money", money)
end

Knit.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. We could 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
...
function MoneyService.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.
	return self.Server:GetMoney(player)
end
...

We can write client-side code to fetch money from the service:

-- Client-side code
local Knit = require(game:GetService("ReplicatedStorage").Packages.Knit)
Knit.Start():catch(warn):await()

local MoneyService = 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.

评论

暂无评论