我希望 lua 函数只运行一次

我对 lua 脚本语言相当新.. 现在我正在尝试编写游戏 Boss

local function SlitherEvents(event, creature, attacker, damage)
    if(creature:GetHealthPct() <= 60) then
        creature:SendUnitYell("将惩罚你们所有人",0)
        creature:RegisterEvent(AirBurst, 1000, 0) -- 1 秒
        return
    end
end

这应该在 Boss 的生命值等于或小于 60% 时,让他说话,但它应该只运行一次。当我运行代码时,Boss 一直说话并攻击所有的时间。我该如何让它只运行一次?

点赞
用户1190388
用户1190388

使用在函数回调范围之外创建的布尔型变量:

local has_talked = false
local function SlitherEvents(event, creature, attacker, damage)
  if creature:GetHealthPct() <= 60 and not has_talked then
    has_talked = true
    creature:SendUnitYell("Will punish you all",0)
    creature:RegisterEvent(AirBurst, 1000, 1) -- 1 秒
    return
  end
end

编辑

如果你在实际使用的是 Eluna 引擎的 RegisterEvent 调用,那么将重复次数设置为 1 而不是 0。这将解决你遇到的 问题

2017-02-12 23:32:30