steamworks.setUserStatValues()

类型 函数
返回值 布尔值
修订版 发行版 2024.3703
关键字 steam, steamworks, setUserStatValues
另请参见 steamworks.getUserStatValue()
steamworks.*

概述

为当前用户设置一个或多个数值状态。请注意,状态在 Steamworks 网站上的“状态与成就”部分进行定义。状态和成就部分。状态用于存储由开发者定义的用户游戏统计信息,例如已玩游戏次数、已赢游戏次数、已输游戏次数,等等。

如果当前用户至少有一个状态值已更新,则返回 true

如果给出了无效参数,如果未更新给定的任何状态,或者如果 steamworks.isLoggedOn 属性为 false,则返回 false

语法

steamworks.setUserStatValues( arrayOfStatTables )
arrayOfStatTables (必需)

数组. 包含状态详细信息的 表格 的数组 — 有关详细信息,请参见下一部分。

状态引用

arrayOfStatTables 数组可以包含 表格,其中每个表格接受以下参数

statName (必需)

字符串. 在 Steamworks 网站上,这是在 **API 名称** 列中设置的状态的唯一名称。

type (必需)

字符串. 指示状态的值类型。必须设置为 "int""float""averageRate"。在 Steamworks 网站上,这必须与在 **类型** 列下设置的值类型一致,否则 Steam 将无法将给定的值分配给该状态。

value (必需)

数字. 要分配给状态的数值。

sessionTimeLength (可选)

数字. 仅当状态的 type 参数设置为 "averageRate" 时才需要此参数。设置为 value 参数的持续时间(以 Steamworks 网站上指定的单位为单位)。例如,如果状态测量“平均速度”,则 value 将设置为游戏中行进的距离,而 sessionTimeLength 参数将设置为该游戏会话中发生的时间长度。然后,Steam 将自动计算该会话和所有以前会话的平均速度。

示例

local steamworks = require( "plugin.steamworks" )

-- Fetch the user's previous stats
local totalGamesPlayed = steamworks.getUserStatValue( { statName="Games Played", type="int" } )
local totalFeetTraveled = steamworks.getUserStatValue( { statName="Feet Traveled", type="float" } )

-- Increment total number of games played
if ( totalGamesPlayed == nil ) then
    totalGamesPlayed = 0
end
totalGamesPlayed = totalGamesPlayed + 1

-- Add number of feet traveled from this game session to the total
-- For this example, we set it to a random number
math.randomseed( os.time() )
local feetTraveled = ( math.random( 1, 100 ) / 10 )
if ( totalFeetTraveled == nil ) then
    totalFeetTraveled = 0
end
totalFeetTraveled = totalFeetTraveled + feetTraveled

-- Update the user's stats on Steam
local wasSuccessful = steamworks.setUserStatValues(
{
    {
        statName = "Games Played",
        type = "int",
        value = totalGamesPlayed
    },
    {
        statName = "Feet Traveled",
        type = "float",
        value = totalFeetTraveled
    },
    {
        statName = "Average Speed",
        type = "averageRate",
        value = feetTraveled,    -- Feet for this session; do not use "totalFeetTraveled"
        sessionTimeLength = 0.5  -- In hours (this is 30 minutes)
    },
})