0

我对 LUA 脚本完全陌生,并尝试创建一个表,其中包含一个接收到的数据,然后在同一函数中使用它来输出收集的数据。

function PremadeFrame_OnEvent(self, event, arg1, arg2)
  --start chat_msg_raid_leader
  if (event == "CHAT_MSG_RAID" or event == "CHAT_MSG_RAID_LEADER" ) then
    battlegrounds = {};
    if string.find(arg1,"BGNUMBER") then
      number = string.match(arg1,"%d+");
      battlegrounds[number] = "";
      battlegrounds[number] = battlegrounds[number]..","..arg2;
      for k,v in pairs(battlegrounds) do
      DEFAULT_CHAT_FRAME:AddMessage ("\124c0000FFFF[BG Number:"..k.."]"..v.."");
      end
    end
  end
end

我试图通过使用添加其他数据的数据端

battlegrounds[number] = battlegrounds[number]..","..arg2;

但它只输出收到的一个数据,例如第一个玩家名字是 Scarill,其他人是 John,Max,它从 Scarill,John,Max 获取数据,但输出如下

[BG Number 43], Scarill

我希望它输出像

[BG Number 43] Scarill, Max
[BG Number 54] John
4

2 回答 2

1

我猜这PremadeFrame_OnEvent是一个事件回调函数。每次调用时battlegrounds都会在内部创建新表,因此很自然它只有一个项目。如果您想捕获比当前项目更多的内容,则需要像这样在battlegrounds外部进行PremadeFrame_OnEvent

battlegrounds = {}

function PremadeFrame_OnEvent(self, event, arg1, arg2)
  --start chat_msg_raid_leader
  if (event == "CHAT_MSG_RAID" or event == "CHAT_MSG_RAID_LEADER" ) then
    if string.find(arg1,"BGNUMBER") then
      number = string.match(arg1,"%d+")
      battlegrounds[number] = arg2
      for k,v in pairs(battlegrounds) do
        DEFAULT_CHAT_FRAME:AddMessage ("\124c0000FFFF[BG Number:"..k.."]"..v.."")
      end
    end
  end
end

所以它不会在事件处理程序中每次都重新创建。

于 2014-03-31T08:37:51.603 回答
0

每次调用事件处理程序时都需要扩展字符串:

local battlegrounds = {}

function PremadeFrame_OnEvent(self, event, arg1, arg2)
  --start chat_msg_raid_leader
  if (event == "CHAT_MSG_RAID" or event == "CHAT_MSG_RAID_LEADER" ) then
    if string.find(arg1,"BGNUMBER") then
      number = string.match(arg1,"%d+")
      if battlegrounds[number] == nil then
          battlegrounds[number] = arg2
      else
          battlegrounds[number] = battlegrounds[number] .. "," .. arg2
      end
      for k,v in ...

但是如果可以用相同的andPremadeFrame_OnEvent()多次调用,但是你在字符串中重复,那么你需要你的 Battlegrounds[number] 是一个表,并且只有在该表中没有时才添加。示例(未经测试,可能是语法错误):arg1arg2arg2

local battlegrounds = {}

function PremadeFrame_OnEvent(self, event, arg1, arg2)
  --start chat_msg_raid_leader
  if (event == "CHAT_MSG_RAID" or event == "CHAT_MSG_RAID_LEADER" ) then
    if string.find(arg1,"BGNUMBER") then
      local number = string.match(arg1,"%d+")
      if battlegrounds[number] == nil then
          battlegrounds[number] = {[arg2] = 1} -- use hash map, will be easiest to search for arg2 later
      else
          -- only add arg2 if not already in there:
          local bgn = battlegrounds[number] 
          if bgn[arg2] == nil then
              table.insert(bgn, arg2)
          end
      end

      for k,v in pairs(battlegrounds) do
          local msg = "\124c0000FFFF[BG Number:" .. k .. "]" .. table.concat(v, ',')
          DEFAULT_CHAT_FRAME:AddMessage(msg)
      end
    end
  end
end

local文体说明:除非需要全局,否则尝试始终使用

于 2014-03-31T20:49:22.607 回答