一旦所有状态为真,对于下一个循环结束

时间:2013-06-01 06:04:08

标签: lua

我正在编写一个脚本,用于制作我将制作的游戏地图。它有点像s4联赛中的追逐系统。

我的问题是,一旦每个人都将他们的“ischaser”设置为true,脚本应该发出“关闭地图”通知,但是一旦每个人都被追逐,没有任何反应,我无法找出原因。

以下是我的表格设置方式

chaserdb_players = { }
chaserdb_players[charname] = { score = 0, death = 0, kill = 0, waschaser = false }

现在地图启动后,它将使用waschaser = false搜索下一个可用的人。

   for characterName,i in next, chaserdb_players do
     if (i.waschaser == false) then
      local player = getPlayerByName(characterName, map_copy)
      if (player ~= nil) then
        addChaserState(player)
        break
       end
      end
    end

在addChaserState(播放器)函数中,它为该字符切换waschaser = true。

我遇到的问题是,一旦地图中的每个人都将他们的ischaser = true,我需要系统通知才能启动,我已经尝试过这样做

for characterName,i in next, chaserdb_players do
          if (i.waschaser == false) then
            local player = getPlayerByName(characterName, map_copy)
            if (player ~= nil) then
                addChaserState(player)
                break
            end
        else
            Notice("All players have been chaser! The map will close in 30 seconds!")
            map_close = true
        end
    end

这是getPlayerByName函数

function getPlayerByName(name, map_copy)
BeginGetMapCopyPlayerCha(map_copy)
for i = 0 , chaserdb_playercount - 1 , 1 do
    local player = GetMapCopyNextPlayerCha ( map_copy )
    if (player == 0 or player == nil) then 
        return nil
    else
        local playerName = GetChaDefaultName(player)
        if (playerName == name) then
            return player
        end
    end
end
end

但这不起作用。

有人可以帮忙吗?如果您需要更多信息,请随时告诉我,我会添加它。

2 个答案:

答案 0 :(得分:1)

将您的通知移到for循环之外。并使用布尔标志来检查是否存在任何错误:

local bFlag = true
for i,characterName in next, chaserdb_players do
    if (characterName.waschaser == false) then
    local player = getPlayerByName(i, map_copy)
        if (player ~= nil) then
            addChaserState(player)
            break
        end
    else
        bFlag = false
    end
end
if bFlag then
    Notice("All players have been chaser! The map will close in 30 seconds!")
    map_close = true
end

答案 1 :(得分:0)

略微更具惯用性:

local allChased = true
for name, character in pairs(chaserdb_players) do
    if not character.waschaser then
        local player = getPlayerByName(name, map_copy)
        if player then
            addChaserState(player)
            break
        end
    else
        allChased = false
    end
end
if allChased then
    Notice("All players have been chaser! The map will close in 30 seconds!")
    map_close = true
end

将函数包装在一个函数中效果很好:

function nextChaser()
    for name, character in ipairs(chaserdb_players) do
        if not character.waschaser then
            local player = getPlayerByName(name, map_copy)
            if player then
                return player
            end
        end
    end
end

local player = nextChaser()
if player then
    addChaserState(player)
else
    Notice("All players have been chaser! The map will close in 30 seconds!")
    map_close = true
end