Skip to content

背包模块管理接口 Backpack

具体函数名及描述如下:

序号函数名函数描述
1SetGridItem(...)设置背包格道具
2RemoveGridItem(...)移除背包格道具
3RemoveGridItemByItemID(...)通过道具ID移除背包道具
4ClearPack(...)清空背包栏
5ClearAllPack(...)清空全部背包
6MoveGridItem(...)移动背包道具
7SwapGridItem(...)交换背包道具
8EnoughSpaceForItem(...)检查背包空间是否足够
9CalcSpaceNumForItem(...)计算背包剩余空间
10HasItemByBackpackBar(...)检测背包是否持有道具
11GetItemNumByBackpackBar(...)获取背包栏道具数量
12GetItemNum(...)获取背包道具数量
13GetGridItemID(...)获取背包格道具ID
14GetGridItemName(...)获取背包格道具名称
15AddItem(...)添加道具到背包
16DiscardItem(...)丢弃背包格道具
17DiscardItemByID(...)通过道具ID丢弃道具
18ActEquipUpByResID(...)穿上装备
19ActEquipOffByEquipID(...)脱下装备
20ActDestructEquip(...)销毁装备
21CreateItem(...)创建道具到背包
22PlayShortCutIxParticle(...)手持道具播放粒子特效
23StopShortCutIxEffect(...)手持道具停止特效
24PlayShortCutItemParticle(...)手持指定道具播放粒子特效
25StopShortCutItemEffect(...)手持指定道具停止特效
26GetGridInfos(...)批量获取背包格信息
27LoadGridInfos(...)加载背包和快捷栏格子信息数据
28DecodeGridInfo(...)解析格子信息
29EncodeTableGridInfo(...)打包格子信息
30SetGridsLock(...)设置背包格锁定状态
31ClearGrids(...)批量清理背包格
32SetBackPackNum(...)设置背包数量
33GetAllBackPackInstanceIds(...)获取背包所有道具实例ID
34CreateItemInstInBackpack(...)创建道具实例到背包
35CreateGunInBackpack(...)创建枪械到背包
36GetInstIdByGridIndex(...)根据背包索引获取道具实例ID
37GetGunInstIdInBackpack(...)获取背包所有枪械实例ID
38IsLock(...)获取背包格是否锁定
39GetGridAttr(...)获取背包格属性
40SetGridAttr(...)设置背包格属性

SetGridItem

  • 参数及类型:
    • playerid:number玩家ID
    • gridid:number格子ID
    • itemid:number,string道具类型
    • num:number道具数量(默认1)
    • durability:number耐久值(默认1)
  • 返回值及类型:
  • 该方法的其他说明: 设置背包指定格子中的道具(对应UGC3.0版本)
  • 具体使用案例如下:
lua
    local playerid = Player:GetHostUin()

    -- 设置快捷栏第一个格子为100,数量为3

    local result = Backpack:SetGridItem(playerid, BackpackBeginIndex.Shortcut, 100, 3)

RemoveGridItem

  • 参数及类型:
  • 返回值及类型:
  • 该方法的其他说明: 移除背包格内一定数量道具
  • 具体使用案例如下:
lua
    local playerid = Player:GetHostUin()

    -- 移除快捷栏第一个格内1个道具

    local result = Backpack:RemoveGridItem(playerid, BackpackBeginIndex.Shortcut, 1)

RemoveGridItemByItemID

  • 参数及类型:
    • playerid:number玩家ID
    • itemid:number,string道具类型
    • num:number道具数量
  • 返回值及类型:
    • num:number移除数量(num and num> 0 成功)
  • 该方法的其他说明: 移除背包内一定数量道具,通过道具ID移除,默认全部移除
  • 具体使用案例如下:
lua
    local playerid = Player:GetHostUin()

    -- 移除背包内 5个草块

    local result = Backpack:RemoveGridItemByItemID(playerid, 100, 5)

    -- 移除背包内 全部草块

    local resul2t = Backpack:RemoveGridItemByItemID(playerid, 100)

ClearPack

  • 参数及类型:
  • 返回值及类型:
  • 该方法的其他说明: 清空指定背包栏中的所有道具
  • 具体使用案例如下:
lua
    local playerid = Player:GetHostUin()

    -- 清除快捷栏全部道具

    Backpack:ClearPack(playerid, BackpackType.Shortcut)

ClearAllPack

  • 参数及类型:
  • 返回值及类型:
  • 该方法的其他说明: 清空玩家所有背包栏中的道具(包含背包栏、快捷栏、装备栏)
  • 具体使用案例如下:
lua
    local result = Backpack:ClearAllPack(Player:GetHostUin())

MoveGridItem

  • 参数及类型:
  • 返回值及类型:
  • 该方法的其他说明: 将指定格子的道具移动到目标格子,默认全部转移
  • 具体使用案例如下:
lua
    -- 移动快捷栏第一个格子到第二个格子

    local result = Backpack:MoveGridItem(playerid, BackpackBeginIndex.Shortcut, BackpackBeginIndex.Shortcut + 1)

SwapGridItem

  • 参数及类型:
  • 返回值及类型:
  • 该方法的其他说明: 交换两个背包格子中的道具位置
  • 具体使用案例如下:
lua
    local playerid = Player:GetHostUin()

    -- 交换快捷栏第一个格子和第二个格子

    local result = Backpack:SwapGridItem(playerid, BackpackBeginIndex.Shortcut, BackpackBeginIndex.Shortcut + 1)

EnoughSpaceForItem

  • 参数及类型:
    • playerid:number玩家ID
    • itemid:number,string道具类型
    • num:number道具数量(默认1)
  • 返回值及类型:
  • 该方法的其他说明: 检查背包(包含背包栏、快捷栏)是否有足够的空间存放指定数量的道具
  • 具体使用案例如下:
lua
    local playerid = Player:GetHostUin()

    local result = Backpack:EnoughSpaceForItem(playerid, 12005, 1)

CalcSpaceNumForItem

  • 参数及类型:
    • playerid:number玩家ID
    • itemid:number,string道具类型
  • 返回值及类型:
    • num:number可以存放的数量
  • 该方法的其他说明: 计算背包(包含背包栏、快捷栏)能存放指定道具的剩余总数量
  • 具体使用案例如下:
lua
    local playerid = Player:GetHostUin()

    local spaceNum = Backpack:CalcSpaceNumForItem(playerid, 12005)

HasItemByBackpackBar

  • 参数及类型:
  • 返回值及类型:
    • bool:boolean是否持有
    • id:number道具所在格子ID(如果持有)
  • 该方法的其他说明: 检测指定背包栏中是否持有指定道具
  • 具体使用案例如下:
lua
    -- 快捷栏是否有草块

    local result = Backpack:HasItemByBackpackBar(playerid, BackpackType.Shortcut, 100)

GetItemNumByBackpackBar

  • 参数及类型:
  • 返回值及类型:
    • num:number道具总数量
    • ids:table装有道具的格子ID数组
  • 该方法的其他说明: 获取指定背包栏中指定道具的总数量,并返回装有该道具的格子ID数组
  • 具体使用案例如下:
lua
    local itemNum, grids = Backpack:GetItemNumByBackpackBar(playerid, BackpackType.Shortcut, 100)

GetItemNum

  • 参数及类型:
    • playerid:number玩家ID
    • itemid:number,string道具类型
    • isAddEquip:boolean是否包含装备栏(默认True)
  • 返回值及类型:
    • num:number道具总数量
    • ids:table装有道具的格子ID数组
  • 该方法的其他说明: 获取背包中指定道具的总数量,并返回装有该道具的格子ID数组(可选择是否包含装备栏)
  • 具体使用案例如下:
lua
    -- 获取道具的数量,包含装备栏

    local itemNum = Backpack:GetItemNum(playerid, 12202)

    -- 获取道具的数量,不包含装备栏

    local itemNum2 = Backpack:GetItemNum(playerid, 12202, false)

GetGridItemID

  • 参数及类型:
  • 返回值及类型:
    • itemid:number,string道具类型
    • num:number道具数量(无道具时数量为0,itemid为0)
  • 该方法的其他说明: 获取指定背包格子中的道具ID和数量
  • 具体使用案例如下:
lua
    local playerid = Player:GetHostUin()

    local gridItemID = Backpack:GetGridItemID(playerid, BackpackBeginIndex.Shortcut)

GetGridItemName

  • 参数及类型:
  • 返回值及类型:
  • 该方法的其他说明: 获取指定背包格子中道具的名称
  • 具体使用案例如下:
lua
    -- 获取快捷栏第一个格子物品名称

    local gridItemName = Backpack:GetGridItemName(playerid, BackpackBeginIndex.Shortcut)

AddItem

  • 参数及类型:
    • playerid:number玩家ID
    • itemid:number,string道具类型
    • num:number道具数量
    • prioritytype:number优先存放位置(1优先快捷栏,2优先背包栏,默认1)
  • 返回值及类型:
    • num:number成功添加的数量
  • 该方法的其他说明: 向玩家背包添加指定数量的道具(可指定优先存放位置)
  • 具体使用案例如下:
lua
    local playerid = Player:GetHostUin()

    -- 创建物品,只添加能放入的数量

    local successNum = Backpack:AddItem(playerid, 100, 10)

DiscardItem

  • 参数及类型:
  • 返回值及类型:
  • 该方法的其他说明: 丢弃指定背包格子中的道具,可设置是否允许拾取
  • 具体使用案例如下:
lua
local bool = Backpack:DiscardItem(playerid, gridid, num, ablePick)

DiscardItemByID

  • 参数及类型:
    • playerid:number玩家ID
    • itemid:number,string道具ID
    • itemnum:number道具数量
  • 返回值及类型:
  • 该方法的其他说明: 通过道具ID丢弃背包中指定数量的道具
  • 具体使用案例如下:
lua
    local playerid = Player:GetHostUin()

    local result = Backpack:DiscardItemByID(playerid, 12005)

    local result2 = Backpack:DiscardItemByID(playerid, "r2_7485931052202679286_22924")

ActEquipUpByResID

  • 参数及类型:
    • playerid:number玩家ID
    • itemid:number,string道具类型
    • frompos:number格子ID(可不传,如果不为nil则必须和itemid一致)
  • 返回值及类型:
  • 该方法的其他说明: 让玩家穿上指定装备(自动从背包或快捷栏查找并装备)
  • 具体使用案例如下:
lua
    local playerid = Player:GetHostUin()

    local result = Backpack:ActEquipUpByResID(playerid, 12202)

ActEquipOffByEquipID

  • 参数及类型:
    • playerid:number玩家ID
    • grid:number装备栏ID(EquipStartIndex + EquipSlotType.Head是装备栏第一个格子,EquipSlotType.Weapon无效)EquipSlotType
    • togrid:number目标格子ID(可不传,如果不为nil则必须是空格子)
  • 返回值及类型:
  • 该方法的其他说明: 让玩家脱下指定装备栏中的装备(可指定移动到目标格子,否则自动寻找空格子或丢弃)
  • 具体使用案例如下:
lua
    local playerid = Player:GetHostUin()

    -- 卸下装备的皮胸甲

    -- 头盔1(BackpackBeginIndex.Equip),胸甲2,护腿3,靴子4,披风5

    local result = Backpack:ActEquipOffByEquipID(playerid, BackpackBeginIndex.Equip + 1)

ActDestructEquip

  • 参数及类型:
    • playerid:number玩家ID
    • grid:number装备栏ID(EquipStartIndex + EquipSlotType.Head是装备栏第一个格子,EquipSlotType.Weapon无效)EquipSlotType
  • 返回值及类型:
  • 该方法的其他说明: 销毁指定装备栏中的装备
  • 具体使用案例如下:
lua
  -- 销毁装备栏的皮胸甲

    local result = Backpack:ActDestructEquip(playerid, BackpackBeginIndex.Equip + 1)

CreateItem

  • 参数及类型:
    • objid:number玩家ID
    • itemid:number,string道具类型
    • itemnum:number道具数量
    • ipos:number优先存放的位置
  • 返回值及类型:
  • 该方法的其他说明: 创建指定数量的道具到玩家背包(可指定优先存放位置,背包满时生成掉落物)
  • 具体使用案例如下:
lua
    local playerid = Player:GetHostUin()

    -- 创建物品,默认放快捷栏跟背包栏(放不下会掉落在地上)

    Backpack:CreateItem(playerid, 100, 3)

    -- 创建物品,默认放快捷栏(快捷栏放不下会掉落在地上)

    Backpack:CreateItem(playerid, 100, 2, BackpackType.Shortcut)

    -- 创建皮胸甲,直接装备上,装备不上会掉落

    Backpack:CreateItem(playerid, 12202, 1, BackpackType.Equip)

PlayShortCutIxParticle

  • 参数及类型:
    • playerid:number玩家ID
    • effectids:number,string,table特效ID或特效名称或ID数组
    • offset:table偏移向量
    • rot:table旋转向量
    • scale:table缩放向量
  • 返回值及类型:
  • 该方法的其他说明: 在玩家手持道具上播放粒子特效(可设置偏移、旋转和缩放)
  • 具体使用案例如下:
lua
    local playerid = Player:GetHostUin()

    -- 播放手持特效, 偏移量(0, 1, 0), 不旋转, 缩放(2, 2, 2)

    local result = Backpack:PlayShortCutIxParticle(playerid, 1011, {x = 0, y = 1, z = 0}, nil, {x = 2, y = 2, z = 2})

StopShortCutIxEffect

  • 参数及类型:
    • playerid:number玩家ID
    • effectids:number,string,table特效ID或特效名称或ID数组
  • 返回值及类型:
  • 该方法的其他说明: 停止玩家手持道具上播放的特效
  • 具体使用案例如下:
lua
    local playerid = Player:GetHostUin()

    local result = Backpack:StopShortCutIxEffect(playerid, 1011)

PlayShortCutItemParticle

  • 参数及类型:
    • playerid:number玩家ID
    • itemid:number道具类型
    • effectids:number,string,table特效ID或特效名称或ID数组
    • offset:table偏移向量
    • rot:table旋转向量
    • scale:table缩放向量
  • 返回值及类型:
  • 该方法的其他说明: 在玩家手持指定道具时播放粒子特效(可设置偏移、旋转和缩放)
  • 具体使用案例如下:
lua
    local playerid = Player:GetHostUin()

    -- 播放手持能量剑时候播放特效1011, 不偏移量, 不旋转, 不缩放

    local result = Backpack:PlayShortCutItemParticle(playerid, 12005, 1011)

StopShortCutItemEffect

  • 参数及类型:
    • playerid:number玩家ID
    • itemid:number道具类型
    • effectids:number,string,table特效ID或特效名称或ID数组
  • 返回值及类型:
  • 该方法的其他说明: 停止玩家手持指定类型道具时播放的特效
  • 具体使用案例如下:
lua
    local playerid = Player:GetHostUin()

    -- 停止手持能量剑时候播放特效1011

    local result = Backpack:StopShortCutItemEffect(playerid, 12005, 1011)

GetGridInfos

  • 参数及类型:
  • 返回值及类型:
    • value:string格子信息字符串(nil表示失败,否则成功)
  • 该方法的其他说明: 批量获取背包和快捷栏连续格子的信息数据(需要连续格子否则返回nil)
  • 具体使用案例如下:
lua
    local result = Backpack:GetGridInfos(Player:GetHostUin(),BackpackBeginIndex.Inventory,BackpackBeginIndex.Inventory+29);

LoadGridInfos

  • 参数及类型:
    • playerid:number玩家ID
    • gridinfo:string格子信息字符串(由GetGridInfos返回)
  • 返回值及类型:
  • 该方法的其他说明: 加载背包和快捷栏格子信息数据(需要相应格子没有道具否则会加载失败)
  • 具体使用案例如下:
lua
    local sGridInfos = Backpack:GetGridInfos(Player:GetHostUin(),BackpackBeginIndex.Inventory,BackpackBeginIndex.Inventory+29);

    if sGridInfos then

        local result = Backpack:LoadGridInfos(Player:GetHostUin(),sGridInfos);

        return result

    end

DecodeGridInfo

  • 参数及类型:
    • str:stringGetGridInfos打包字符串
  • 返回值及类型:
    • obj:table解析后的数组(数组元素包含index格子ID、info格子详细信息、itemid物品ID、num数量,nil表示解析失败)
  • 该方法的其他说明: 解析格子信息数据(将GetGridInfos返回的字符串解析为表格)
  • 具体使用案例如下:
lua
    local sGridInfos = Backpack:GetGridInfos(Player:GetHostUin(),BackpackBeginIndex.Inventory,BackpackBeginIndex.Inventory+29);

    if sGridInfos then

        local result = Backpack:DecodeGridInfo(sGridInfos);

        return result

    end

EncodeTableGridInfo

  • 参数及类型:
    • infos:tableDecodeGridInfo解析后的数据
  • 返回值及类型:
    • value:string打包后的字符串(nil表示打包失败,否则成功)
  • 该方法的其他说明: 将解析的格子信息数据打包为字符串
  • 具体使用案例如下:
lua
    local sGridInfos = Backpack:GetGridInfos(Player:GetHostUin(),BackpackBeginIndex.Inventory,BackpackBeginIndex.Inventory+29);

    if sGridInfos then

        local infos = Backpack:DecodeGridInfo(sGridInfos);

        local result = Backpack:EncodeTableGridInfo(infos);

        return result

    end

SetGridsLock

  • 参数及类型:
    • playerid:number玩家ID
    • begingridId:number开始格子ID
    • endgridId:number结束格子ID
    • lock:boolean是否锁定(true锁定,false解锁)
  • 返回值及类型:
  • 该方法的其他说明: 设置背包和快捷栏连续格子的锁定状态(需要连续格子否则返回失败)
  • 具体使用案例如下:
lua
    local result = Backpack:SetGridsLock(Player:GetHostUin(),BackpackBeginIndex.Inventory,BackpackBeginIndex.Inventory+29,true);

ClearGrids

  • 参数及类型:
  • 返回值及类型:
  • 该方法的其他说明: 批量清理背包连续格子的数据(需要连续格子否则返回失败)
  • 具体使用案例如下:
lua
    local result = Backpack:ClearGrids(Player:GetHostUin(),BackpackBeginIndex.Inventory,BackpackBeginIndex.Inventory+29);

    return result

SetBackPackNum

  • 参数及类型:
    • playerid:number玩家ID
    • num:number背包格子数量(范围0-100)
  • 返回值及类型:
  • 该方法的其他说明: 设置玩家背包的格子数量(范围0-100)
  • 具体使用案例如下:
lua
    local result = Backpack:SetBackPackNum(Player:GetHostUin(),33);

    return result

GetAllBackPackInstanceIds

  • 参数及类型:
  • 返回值及类型:
    • ids:table道具实例ID数组
  • 该方法的其他说明: 获取指定背包栏中所有道具实例的ID列表
  • 具体使用案例如下:
lua
local ids = Backpack:GetAllBackPackInstanceIds(playerid, bartype)

CreateItemInstInBackpack

  • 参数及类型:
    • playerid:number玩家Uin
    • itemid:number,string道具ID
    • gridIndex:number,nil格子索引(nil表示自动选择)
  • 返回值及类型:
  • 该方法的其他说明: 在玩家的背包内创建一个道具实例(格子未锁定才能创建)
  • 具体使用案例如下:
lua
    local itemid = 12005

    local gridIndex = 2

    local instId = Backpack:CreateItemInstInBackpack(playerid, itemid, gridIndex)

    if instId then

        print("创建物品实例", instId)

    end

CreateGunInBackpack

  • 参数及类型:
    • playerid:number玩家Uin
    • itemid:number,string道具ID
    • gridIndex:number,nil格子索引(nil表示自动选择)
  • 返回值及类型:
  • 该方法的其他说明: 在玩家的背包内创建一把实例化的枪械(格子未锁定才能创建)
  • 具体使用案例如下:
lua
local id = Backpack:CreateGunInBackpack(playerid, itemid, gridIndex)

GetInstIdByGridIndex

  • 参数及类型:
  • 返回值及类型:
  • 该方法的其他说明: 根据背包索引获取道具实例ID(格子未锁定才能获取)
  • 具体使用案例如下:
lua
local id = Backpack:GetInstIdByGridIndex(playerid, gridIndex)

GetGunInstIdInBackpack

  • 参数及类型:
  • 返回值及类型:
    • ids:table枪械实例ID数组
  • 该方法的其他说明: 获取背包中所有实例化的枪械ID列表(仅返回未锁定格子的枪械)
  • 具体使用案例如下:
lua
local ids = Backpack:GetGunInstIdInBackpack(playerid)

IsLock

  • 参数及类型:
  • 返回值及类型:
    • bool:boolean是否锁定(参数错误返回true)
  • 该方法的其他说明: 获取背包格子是否锁定(1.48+版本,参数错误返回true)
  • 具体使用案例如下:
lua
local bool = Backpack:IsLock(playerid, gridIndex)

GetGridAttr

  • 参数及类型:
  • 返回值及类型:
  • 该方法的其他说明: 获取背包格子的指定属性值(支持道具数量、耐久度、韧性)
  • 具体使用案例如下:
lua
    local ret = Backpack:SetGridAttr(playerid, gridid,GridAttr.ItemNum,2)

    print("设置格子道具数量", ret) --如果数量超过最大堆叠数 则取最大堆叠数

    local num1 = Backpack:SetGridAttr(playerid, gridid,GridAttr.Durable,100)

    print("设置格子耐久度", num1) --最大耐久为0,设置值直接返回false

    -- local num2 = Backpack:SetGridAttr(playerid, gridid,GridAttr.Toughness)

    -- print("设置格子韧性", num2) --最大韧性为0,设置值直接返回false

    return ret and num1

end

function BackpackTask:GetGridAttr()

    local playerid = Player:GetHostUin()

    local gridid = 2

    --Backpack:GetGridAttr

    local num = Backpack:GetGridAttr(playerid, gridid,GridAttr.ItemNum)

    print("获取格子属性", num)

SetGridAttr

  • 参数及类型:
  • 返回值及类型:
  • 该方法的其他说明: 设置背包格子的指定属性值(支持道具数量、耐久度、韧性,会自动限制在有效范围内)
  • 具体使用案例如下:
lua
local bool = Backpack:SetGridAttr(playerid, gridid, attr, value)