如何在lua中正确定义局部变量并将其引用到XML?

时间:2017-12-22 23:30:54

标签: xml lua

我正在尝试将公共公会注释识别为lua中的局部变量,并在XML文件中引用该变量的值。现在我不确定这是否是正确的代码。

local gngn()
_, _, _, _, _, _, gngn, _ = GetGuildRosterInfo("player");
end

如果是这样,我想将此变量嵌入XML文件

<FontString name="$parent_Text" inherits="GameFontNormal" 
text="&lt;22575&gt; ">

将被替换为虚拟文本&lt; 22575&gt;

2 个答案:

答案 0 :(得分:0)

您可以通过以下方式在Lua中定义局部变量:

local var1, _, var3 = 1, 2, 3

下划线_是一个特殊的占位符来解除一个值。所以,2将会丢失,但1&amp; 3存储在变量中。

获取玩家的公会指示:

local _, _, _, _, _, _, playerNote = GetGuildRosterInfo("player");

对于XML文件,您有两个选项:

  1. 创建一个新的XML条目并自己将其写入XML文件
  2. ```

    local newXmlEntry = '<FontString name="$parent_Text" inherits="GameFontNormal" text="'.. playerNote ..' ">'
    
    1. 替换XML文件的当前条目,然后更新整个 文件
    2. ```

      -- open file
      local xmlFile = assert(io.open(xmlFilePath, "r"))
      -- read and store all text
      local xmlText = xmlFile:read("*a")
      xmlFile:close()
      
      -- replace all placeholders with playerNote text
      string.gsub(xmlText, "&lt;22575&gt;", tostring(playerNote))
      
      -- write updated xml file
      xmlFile = assert(io.open(xmlFilePath, "w+"))
      xmlFile:write(xmlText)
      xmlFile:close()
      

答案 1 :(得分:0)

看来你正在为魔兽世界做一个插件。假设这个,你的问题目前无法实现。

WoW中的Lua环境不包含io库,因此无法创建或编辑xml文件。此外,嵌入在XML文件中的XML属性和Lua代码只能访问全局变量(当然,除了嵌入式上下文中创建的任何本地变量)。此外,GetGuildRosterInfo需要索引,而不是单位。

那就是说,你可以做的是从你的Lua文件访问$parent_Text并调用$parent_Text:SetText()来更新字符串。

例如,如果您正在寻找自己的笔记,可以使用:

local playerName = UnitName("player")
for i = 1, GetNumGuildMembers() do
    local name, _, _, _, _, _, note = GetGuildRosterInfo(i)
    -- GetGuildRosterInfo provides the name of a player in the
    --  format "charName-serverName", we can use Ambiguate to
    --  get just the charName.

    if Ambiguate(name) == playerName then
        -- we found the player, so set the note

        -- "$parent" should be replaced with whatever the parent frames' name is
        $parent_Text:SetText(note)

        -- we've done what we wanted, so exit the loop
        break
    end
end