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