【发布时间】:2020-10-20 10:52:38
【问题描述】:
我可以看到有人问过类似的问题,但是我对 lua 编码并不是很熟悉。 我正在尝试修复一个旧的魔兽世界香草插件,以便在经典客户端中运行。
代码如下:
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”语句,但我不确定该怎么做,我想看看你们这里的聪明人是否知道发生了什么
【问题讨论】:
-
“框架”,如 WoW 附加组件框架通常会给语言带来新的味道,所以我的评论可能对你的情况有误。 Lua 中对当前对象的引用称为
self。当您使用:而不是.调用函数时,您调用此函数的对象将作为第一个参数隐式传递。如果将函数定义为something:yourfunction()而不是something.yourfunction(),则第一个参数隐式命名为self。函数定义function something:yourfunction() [...] end等于function something.yourfunction(self) [...] end -
那么
27会在你的脚本中出现什么? -
第27行是onload; this:RegisterEvent("PLAYER_ENTERING_WORLD");我想下一行也是一样的,因为它是一个相同的函数
-
这真的取决于正在发生的事情以及原始开发人员采取的方法。在过去的某个时候,Blizz 从各种全局变量转移到了局部变量。检查 FHH_OnLoad 的实际使用位置(名称表明它位于 XML 中的某个位置)。
<OnLoad>将单个参数self传递给其内容,然后可以将其传递给FHH_OnLoad。 -
你是对的,它在一个 XML 文件中。我想我可能已经吃得太饱了,我是一个爱好 C# 编码器,并认为我可以通过修复这个插件来寻找我的方式,但看起来它比我预期的要复杂。 @green 虽然你是对的,但 FHH_OnLoad 存在于 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>
标签: lua add-on world-of-warcraft