lua - 尝试调用全局 'this'(零值)

标签 lua add-on world-of-warcraft

我可以看到有人问过类似的问题,但是我对 lua 编码不是很熟悉。 我正在尝试修复旧的魔兽世界 Vanilla 插件,以便在经典客户端中运行。

代码如下:

    function FHH_OnLoad()

    this:RegisterEvent("PLAYER_ENTERING_WORLD");
    this:RegisterEvent("UPDATE_MOUSEOVER_UNIT");

    -- Register Slash Commands
    SLASH_FHH1 = "/huntershelper";
    SLASH_FHH2 = "/hh";
    SlashCmdList["FHH"] = function(msg)
        FHH_ChatCommandHandler(msg);
    end
    
    local version = GetAddOnMetadata("GFW_HuntersHelper", "Version");
    GFWUtils.Print("Fizzwidget Hunter's Helper "..version.." initialized!");
    
end

它会抛出以下错误;

Message: Interface\AddOns\GFW_HuntersHelper\HuntersHelper.lua:27: attempt to index global 'this' (a nil value)
Time: Tue Jun 30 09:25:14 2020
Count: 1
Stack: Interface\AddOns\GFW_HuntersHelper\HuntersHelper.lua:27: attempt to index global 'this' (a nil value)
Interface\AddOns\GFW_HuntersHelper\HuntersHelper.lua:27: in function `FHH_OnLoad'
[string "*:OnLoad"]:1: in function <[string "*:OnLoad"]:1>

Locals: (*temporary) = nil
(*temporary) = nil
(*temporary) = nil
(*temporary) = nil
(*temporary) = nil
(*temporary) = "attempt to index global 'this' (a nil value)"

我尝试过使用“this”语句,但我不确定该怎么做,我想看看你们这里的聪明人是否知道发生了什么

最佳答案

如果有问题的插件很旧,那么在过去的某个时候(2010 年?)插件 API 已从全局变量移至局部变量。

框架是在 XML 文件中定义的,就像您在评论中发布的那样:

<Frame name="HuntersHelperFrame">
   <Scripts>
      <OnLoad>FHH_OnLoad();</OnLoad>
      <OnEvent>
         FHH_OnEvent(event, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9); 
      </OnEvent>
   </Scripts>
</Frame>

<Scripts>中的元素实际上被称为函数,其内容作为函数体。它们被调用时带有一些 参数。您可以使用 World of Warcraft API 找出什么参数作为引用。它不是官方的,但它是最接近引用手册的东西。

目前,您对 Widget handlers 感兴趣.

现在,您应该采取的第一步是:

  1. 更改 XML:
    <Frame name="HuntersHelperFrame">
       <Scripts>
          <OnLoad>FHH_OnLoad(self)</OnLoad>
          <OnEvent>
             FHH_OnEvent(self, event, ...) 
          </OnEvent>
       </Scripts>
    </Frame>
    
  2. 更改 lua 以反射(reflect):
    function FHH_OnLoad(self)
       self:RegisterEvent("PLAYER_ENTERING_WORLD")
       -- and so on, change all occurrences of `this` to `self`
       -- or simply name the first argument `this` instead of `self`:
       -- function FHH_OnLoad(this)
    end
    
    -- Change all of the functions:
    function FHH_OnEvent(self, event, ...)
       -- function body
    end
    

根据插件的大小,它可能会变成一大堆工作。可悲的是,这还没有结束。小心脚本可能直接依赖于全局变量的存在并做一些技巧。

我猜你可以尝试使用像 local this = self 这样的技巧来解决它和类似的,但这可能并不适用于所有情况,并且可能会由于框架解析 XML 的方式而导致一些问题。

最后一点;多年来 API 发生了很大变化,您很可能会遇到更多问题。祝你好运!

关于lua - 尝试调用全局 'this'(零值),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62652304/

相关文章:

lua - 如何制作魔兽世界插件?

lua - 循环浏览不同的文件位置选项

function - 从lua中的函数args获取子表的索引

sockets - 有没有办法在魔兽世界 Lua 脚本中使用套接字?

c++ - ToCString 的 header 在哪里,或者如何将参数转换为 cstrings?

javascript - 如何在 Firefox 插件中调用控制台调试面板?

lua - 哇: functions UnitIsPlayer() change in 6. 0?

linux - lua 5.1.4的开发包是什么?

包含子包的 Lua 包

firefox - 如何快速编写一个firefox扩展?