delitembymakeindex
🤖 回答引擎: 后端Lua |
🕒 时间: 2026-06-13 16:20:30 |
👁️ 阅读: 3
我是lua后端老师!
修仙者,`delitembymakeindex` 是一个通过物品唯一ID来扣除物品的重要函数。这个函数需要谨慎使用,**务必判断返回值**,因为引擎锁定的物品并不会被成功扣除!
**函数格式如下:**
```lua
-- 函数原型
local success = delitembymakeindex(actor, ids, count, desc)
```
**参数说明:**
- `actor`:玩家对象(字符串ID)
- `ids`:物品唯一ID,多个ID用逗号(,)串联(字符串)
- `count`:可选参数,对于叠加物品,指定扣除的数量;不填则默认全部扣除;不可叠加物品会全部扣除
- `desc`:可选参数,描述信息(引擎64_24.03.14新增)
**返回值:**
- `result`:布尔值,`true` 表示扣除成功,`false` 表示失败
**使用示例:**
```lua
-- 扣除单个物品
local ids = "12345"
if not delitembymakeindex(actor, ids) then
say(actor, "物品扣除失败!")
end
-- 扣除多个物品
local ids = "1001,1002,1003"
if delitembymakeindex(actor, ids, 1, "任务消耗") then
say(actor, "物品扣除成功!")
end
```
**特别注意以下两种特殊情况:**
```lua
-- 1. 如果传入重复的ID,函数会返回true,但实际只扣除一份
local ids = "1234,1234" -- 重复ID
delitembymakeindex(actor, ids) -- 返回true,建议传入前去重
-- 2. 如果传入空字符串或nil,函数也会返回true
local ids = ""
delitembymakeindex(actor, ids) -- 返回true,需要自行过滤异常参数
```
**最佳实践建议:**
1. 在传入 `ids` 前先检查是否为空或无效
2. 对于多个ID,先去重再传入
3. **一定要判断返回值**,根据结果进行后续逻辑处理
如果你要扣除的是仓库里的物品,记得使用 `delstorageitem` 函数哦!
← 返回问答库