function DengjiTiShengA:YiJianHeCheng(actor)
local curDaLu = GetInt(0, actor, self.varName)
print(curDaLu)
print(777)
-- local curDaLu = SetInt(0, actor, self.varName, 5)
print(curDaLu)
local index = 1
if curDaLu == 5 th
🤖 回答引擎: 后端Lua |
🕒 时间: 2026-04-29 15:46:45 |
👁️ 阅读: 9
我是lua后端老师!小仙你好,看了你的代码,是想根据`index`的值来决定使用`getbindmoney`还是`querymoney`来获取货币数量,并且后续的扣除和判断也要对应起来,对吧?这个思路很清晰,我们来调整一下。
你的代码里,`itemNameIds`这个变量名其实是个字符串,它应该是货币的名称,比如“元宝”、“钻石”之类的。`getbindmoney`和`querymoney`的第一个参数都是玩家对象`actor`,但第二个参数不同:
- `getbindmoney(actor, moneyname)` 需要传入**货币名称**。
- `querymoney(actor, id)` 需要传入**货币ID**(数字)。
所以我们需要在配置里区分开。假设你的配置`page[index]`里,`costs`字段格式是 `物品ID#数量&货币标识#数量`,其中“货币标识”在index为1时是货币名称,index为2时是货币ID。我们来重构一下逻辑:
```lua
function DengjiTiShengA:YiJianHeCheng(actor)
local curDaLu = GetInt(0, actor, self.varName) -- 获取玩家当前大陆等级
release_print("当前大陆等级:", curDaLu)
local index = 1
if curDaLu == 5 then
index = 2
end
release_print("当前使用配置索引index:", index)
local page = self._cfg
local cost = page[index].costs
local t1 = string.split(cost, "&")
local needItem, needCurrency = t1[1], t1[2] -- 拆分出物品和货币部分
-- 解析物品需求
local itemId, itemNum = string.match(needItem, "(%d+)#(%d+)")
itemNum = tonumber(itemNum)
local itemName = getstditeminfo(itemId, 1) -- 获取物品名称
release_print("需要物品:", itemName, "数量:", itemNum)
-- 解析货币需求,并根据index决定是名称还是ID
local currencyIdOrName, currencyNum = string.match(needCurrency, "(.+)#(%d+)")
currencyNum = tonumber(currencyNum)
local currencyCount = 0
local currencyType = "" -- 用于记录是“bindmoney”还是“money”,方便后面扣除
if index == 1 then
-- index==1时,currencyIdOrName是货币名称(如“元宝”),使用getbindmoney
currencyCount = getbindmoney(actor, currencyIdOrName)
currencyType = "bindmoney"
release_print("需要货币(名称):", currencyIdOrName, "数量:", currencyNum, "玩家拥有:", currencyCount)
elseif index == 2 then
-- index==2时,currencyIdOrName是货币ID(如“2”),使用querymoney
local currencyID = tonumber(currencyIdOrName)
currencyCount = querymoney(actor, currencyID)
currencyType = "money"
release_print("需要货币(ID):", currencyID, "数量:", currencyNum, "玩家拥有:", currencyCount)
end
-- 获取玩家背包中等级丹的数量
local levelDanCount = getbagitemcount(actor, itemId) -- 注意:getbagitemcount第一个参数是物品ID,不是名称
release_print("背包中等级丹数量:", levelDanCount)
-- 计算最大可合成次数
local maxDan = math.floor(levelDanCount / itemNum)
local maxCurrency = math.floor(currencyCount / currencyNum)
local max = math.min(maxDan, maxCurrency)
release_print("最大可合成次数:", max)
if max <= 0 then
-- 材料不足提示
if levelDanCount < itemNum then
sendmsg(actor, "等级丹数量不足,需要"..itemNum.."个", 1) -- 使用sendmsg提示
end
if currencyCount < currencyNum then
local tipMsg = ""
if index == 1 then
tipMsg = currencyIdOrName.."不足,需要"..currencyNum.."个"
else
tipMsg = "货币(ID:"..currencyIdOrName..")不足,需要"..currencyNum.."个"
end
sendmsg(actor, tipMsg, 1)
end
return
end
-- 扣除材料
-- 构建扣除字符串,货币部分需要根据类型调整
local deductStr = itemId.."#"..(itemNum * max)
if currencyType == "bindmoney" then
-- 扣除绑定货币
local success = consumebindmoney(actor, currencyIdOrName, currencyNum * max, "一键合成消耗")
if not success then
release_print("扣除绑定货币失败")
return
end
deductStr = deductStr -- 物品扣除部分单独处理
else
-- 扣除普通货币,加入到扣除字符串
deductStr = deductStr.."&"..currencyIdOrName.."#"..(currencyNum * max)
end
-- 扣除物品(如果上面没把货币加进去,这里只扣物品)
if not GMLib:takes(actor, deductStr, "合成") then
release_print("扣除物品失败")
return
end
-- 给予合成产物
local give = page[index].give
local giveId, giveNum = string.match(give, "(%d+)#(%d+)")
giveNum = tonumber(giveNum) * max
local giveStr = giveId.."#"..giveNum
gives(actor, giveStr)
sendmsg(actor, "合成成功!获得"..getstditeminfo(giveId, 1).."x"..giveNum, 1)
end
```
**主要改动点:**
1. **货币识别**:将`needItems`改名为`needCurrency`,用`(.+)#(%d+)`来匹配货币标识,它可能是名称也可能是ID。
2. **分支判断**:根据`index`值,分别调用`getbindmoney`(传入名称)或`querymoney`(传入ID)。
3. **扣除逻辑分离**:因为`consumebindmoney`是独立函数,所以扣除绑定货币和扣除物品/普通货币是分开处理的。普通货币依然可以拼接到`takes`的字符串里。
4. **提示优化**:将`Msg.richMsg`换成了引擎通用的`sendmsg`,并丰富了提示信息。
**注意:** 你的配置表`self._cfg`里,`costs`字段需要根据`index`正确配置,例如:
- `index = 1`时:`costs = "1001#2&元宝#500"` (“元宝”是货币名称)
- `index = 2`时:`costs = "1001#5&2#1000"` (“2”是货币ID,在`cfg_item.xls`里定义)
这样改完后,逻辑就清晰了,后续的数据处理也会自动对应上。如果还有不明白的,随时可以问你师傅浮生梦尊师哦!
我是lua后端老师!小仙你好,看了你的代码,是想根据`index`的值来决定使用`getbindmoney`还是`querymoney`来获取货币数量,并且后续的扣除和判断也要对应起来,对吧?这个思路很清晰,我们来调整一下。
你的代码里,`itemNameIds`这个变量名其实是个字符串,它应该是货币的名称,比如“元宝”、“钻石”之类的。`getbindmoney`和`querymoney`的第一个参数都是玩家对象`actor`,但第二个参数不同:
- `getbindmoney(actor, moneyname)` 需要传入**货币名称**。
- `querymoney(actor, id)` 需要传入**货币ID**(数字)。
所以我们需要在配置里区分开。假设你的配置`page[index]`里,`costs`字段格式是 `物品ID#数量&货币标识#数量`,其中“货币标识”在index为1时是货币名称,index为2时是货币ID。我们来重构一下逻辑:
```lua
function DengjiTiShengA:YiJianHeCheng(actor)
local curDaLu = GetInt(0, actor, self.varName) -- 获取玩家当前大陆等级
release_print("当前大陆等级:", curDaLu)
local index = 1
if curDaLu == 5 then
index = 2
end
release_print("当前使用配置索引index:", index)
local page = self._cfg
local cost = page[index].costs
local t1 = string.split(cost, "&")
local needItem, needCurrency = t1[1], t1[2] -- 拆分出物品和货币部分
-- 解析物品需求
local itemId, itemNum = string.match(needItem, "(%d+)#(%d+)")
itemNum = tonumber(itemNum)
local itemName = getstditeminfo(itemId, 1) -- 获取物品名称
release_print("需要物品:", itemName, "数量:", itemNum)
-- 解析货币需求,并根据index决定是名称还是ID
local currencyIdOrName, currencyNum = string.match(needCurrency, "(.+)#(%d+)")
currencyNum = tonumber(currencyNum)
local currencyCount = 0
local currencyType = "" -- 用于记录是“bindmoney”还是“money”,方便后面扣除
if index == 1 then
-- index==1时,currencyIdOrName是货币名称(如“元宝”),使用getbindmoney
currencyCount = getbindmoney(actor, currencyIdOrName)
currencyType = "bindmoney"
release_print("需要货币(名称):", currencyIdOrName, "数量:", currencyNum, "玩家拥有:", currencyCount)
elseif index == 2 then
-- index==2时,currencyIdOrName是货币ID(如“2”),使用querymoney
local currencyID = tonumber(currencyIdOrName)
currencyCount = querymoney(actor, currencyID)
currencyType = "money"
release_print("需要货币(ID):", currencyID, "数量:", currencyNum, "玩家拥有:", currencyCount)
end
-- 获取玩家背包中等级丹的数量
local levelDanCount = getbagitemcount(actor, itemId) -- 注意:getbagitemcount第一个参数是物品ID,不是名称
release_print("背包中等级丹数量:", levelDanCount)
-- 计算最大可合成次数
local maxDan = math.floor(levelDanCount / itemNum)
local maxCurrency = math.floor(currencyCount / currencyNum)
local max = math.min(maxDan, maxCurrency)
release_print("最大可合成次数:", max)
if max <= 0 then
-- 材料不足提示
if levelDanCount < itemNum then
sendmsg(actor, "等级丹数量不足,需要"..itemNum.."个", 1) -- 使用sendmsg提示
end
if currencyCount < currencyNum then
local tipMsg = ""
if index == 1 then
tipMsg = currencyIdOrName.."不足,需要"..currencyNum.."个"
else
tipMsg = "货币(ID:"..currencyIdOrName..")不足,需要"..currencyNum.."个"
end
sendmsg(actor, tipMsg, 1)
end
return
end
-- 扣除材料
-- 构建扣除字符串,货币部分需要根据类型调整
local deductStr = itemId.."#"..(itemNum * max)
if currencyType == "bindmoney" then
-- 扣除绑定货币
local success = consumebindmoney(actor, currencyIdOrName, currencyNum * max, "一键合成消耗")
if not success then
release_print("扣除绑定货币失败")
return
end
deductStr = deductStr -- 物品扣除部分单独处理
else
-- 扣除普通货币,加入到扣除字符串
deductStr = deductStr.."&"..currencyIdOrName.."#"..(currencyNum * max)
end
-- 扣除物品(如果上面没把货币加进去,这里只扣物品)
if not GMLib:takes(actor, deductStr, "合成") then
release_print("扣除物品失败")
return
end
-- 给予合成产物
local give = page[index].give
local giveId, giveNum = string.match(give, "(%d+)#(%d+)")
giveNum = tonumber(giveNum) * max
local giveStr = giveId.."#"..giveNum
gives(actor, giveStr)
sendmsg(actor, "合成成功!获得"..getstditeminfo(giveId, 1).."x"..giveNum, 1)
end
```
**主要改动点:**
1. **货币识别**:将`needItems`改名为`needCurrency`,用`(.+)#(%d+)`来匹配货币标识,它可能是名称也可能是ID。
2. **分支判断**:根据`index`值,分别调用`getbindmoney`(传入名称)或`querymoney`(传入ID)。
3. **扣除逻辑分离**:因为`consumebindmoney`是独立函数,所以扣除绑定货币和扣除物品/普通货币是分开处理的。普通货币依然可以拼接到`takes`的字符串里。
4. **提示优化**:将`Msg.richMsg`换成了引擎通用的`sendmsg`,并丰富了提示信息。
**注意:** 你的配置表`self._cfg`里,`costs`字段需要根据`index`正确配置,例如:
- `index = 1`时:`costs = "1001#2&元宝#500"` (“元宝”是货币名称)
- `index = 2`时:`costs = "1001#5&2#1000"` (“2”是货币ID,在`cfg_item.xls`里定义)
这样改完后,逻辑就清晰了,后续的数据处理也会自动对应上。如果还有不明白的,随时可以问你师傅浮生梦尊师哦!
← 返回问答库