使用 :split() 改变结果 [LUA]

我一直在编辑脚本时遇到了困难。 以下是造成麻烦的行

local name = Item(item):getName():split('+'

doItemSetAttribute(itemEx.uid,ITEM_ATTRIBUTE_NAME,it:getName()..((nLevel>0) and " +"..nLevel or ""))

好的,这将创建新名称的exitem,例如:

装甲+1

我的目标是更改它并获得以下内容:

装甲(1%)

我将第二行编辑为下面这个:

doItemSetAttribute(itemEx.uid,ITEM_ATTRIBUTE_NAME,it:getName()..((nLevel>0) and "("..nLevel.."%)"""))

当在游戏中“升级”物品时,脚本将更改物品名称为:

装甲(1%)

但是,现在通过这行local name = Item(item):getName():split('+') 脚本无法正确查看新的物品名称以进行下一次升级。

我尝试local name = Item(item):getName():split('(','%')等。但是我无法理解它。 应该将项目读作装甲(x%)。 我在这里寻求帮助: http://lua-users.org/wiki/SplitJoin

但我真的无法理解:\ |有人能为我解答吗?

点赞
用户3342050
用户3342050

这应该可以工作,或者说非常相似。我不知道您对doItemSetAttribute()预期的参数,所以我在猜测,但在给定的信息下看起来是正确的。

function string .split( text, delimiter )
    local head, tail = text, ''  --默认返回给定的内容,没有额外的奖励
    local location = text :find( delimiter )
    if location then  --如果预期的符号存在,则进行分割
        head = text :sub(  1,  location -1  )  --标记前面的内容
        tail = text :sub(  location +1,  -1  )  --标记后面的内容
    end
    return head, tail
end

local name, bonus = Item(item) :getName() :split('+')
if #bonus > 0 then bonus = '(' ..bonus ..'%)' end  --如果有奖励,则加上括号
doItemSetAttribute( itemEx.uid,  ITEM_ATTRIBUTE_NAME,  name ..bonus )
2021-01-05 12:27:42