範例程式碼
生成推薦物品列表
local RecommendationService = game:GetService("RecommendationService")
-- 定義生成推薦列表的請求
local request: GenerateRecommendationItemListRequest = {
ConfigName = "MaximizeEngagement",
LocationId = "Lobby",
PageSize = 10
-- 注意:取消註解並設置 CustomContexts.UserId 用於伺服器腳本。
-- 本地腳本不需要設置。
-- CustomContexts = {
-- ["UserId"] = tostring(player.UserId),
-- }
}
-- 調用 GenerateItemListAsync 獲取推薦頁面
local success, recommendationPages = pcall(function()
return RecommendationService:GenerateItemListAsync(request)
end)
if success then
-- 獲取當前推薦物品頁面
local currentPage = recommendationPages:GetCurrentPage()
for _, item in ipairs(currentPage) do
print("ItemId: " .. item.ItemId)
print(" ReferenceId: " .. item.ReferenceId)
print(" TracingId: " .. item.TracingId)
end
else
warn("RecommendationService 錯誤: " .. tostring(recommendationPages))
end
--[[ 返回的數據格式,每個頁面中的項目
{
"ItemId": string,
"ReferenceId": string,
"TracingId": string,
"Creator": {
"CreatorId": number,
"CreatorType": Enum.CreatorType,
}?,
"Attributes": {
{
"AssetId": number?,
"Text": string?,
"Description": string?,
"SeekStartTime": number?,
"SeekEndTime": number?,
"TrimStartTime": number?,
"TrimEndTime": number?
}
}?,
"CustomTags": { string }?
}
--]]