MatchmakingService 负责管理自定义匹配特性。使用它来阅读和写入匹配数据。
概要
方法
获取特定服务器属性的值。
启动服务器属性模型和其值在 Studio 进行测试。
为特定服务器属性分配值。
属性
方法
GetServerAttribute
获取特定服务器属性的值。
参数
服务器属性名称。最多限制为 50 个字符。
返回
返回服务器属性值,如果属性被找到且错误为 nil 。否则,返回nil属性值和一个错误信息。
InitializeServerAttributesForStudio
启动服务器属性模型和其值在 Studio 进行测试。该方法可选,在 Studio 之外运行时没有影响。
参数
一组属性名称-值对。
返回
如果调用成功,返回 true;否则返回 false 并一个错误信息。
SetServerAttribute
为特定服务器属性分配值。
参数
服务器属性名称。最多限制为 50 个字符。
value: Variant
服务器属性的值。最多限于 50 个字符。
返回
如果调用成功,返回 true;否则返回 false 并一个错误信息。
代码示例
The following code sample:
- Tests the hard-coded Level, Elo, and TrainingMode attributes in Studio with InitializeServerAttributesForStudio.
- Gets the Level attribute with GetServerAttribute.
- Updates the player's Level attribute with SetServerAttribute.
MarketplaceService sample
local MatchmakingService = game::GetService("MatchmakingService")
local RunService = game:GetService("RunService")
if RunService:IsStudio() then
-- Sets up initial attributes and schema for testing
MatchmakingService:InitializeServerAttributesForStudio(
{
Level = "Advanced",
Elo = 123.456,
TrainingMode = true
})
end
-- Retrieves the Level attribute
local currentLevel, error = MatchmakingService:GetServerAttribute("Level")
if error then
print(error)
else
print("Current level: " .. currentLevel)
end
-- Updates the Level attribute value to Advanced
local success, error = MatchmakingService:SetServerAttribute("Level", "Advanced")
if not success then
print("Failed to update server attribute [Level] to [Advanced] due to error: " .. error)
else
print("Successfully set [Level] to [Advanced]")
end