3

当我尝试使用网站上显示的示例来制作世界对象注册事件时,我在世界对象上收到错误,暗示它是一个 nil 值,如下所示:

lua_scripts/test.lua:5: 尝试索引全局 'worldobject' (一个 nil 值)

尝试了几个具有相同结果的不同示例,所以我自然希望这可能是我的疏忽。

测试示例:

local function YourFunction(eventid, delay, repeats, worldobject)
      worldobject:SendUnitSay("My name is " .. worldobject:GetName(), 255)
end
worldobject:RegisterEvent(YourFunction, 10000, 5)
local function Timed(eventid, delay, repeats, worldobject)
    print(worldobject:GetName())
end
worldobject:RegisterEvent(Timed, 1000, 5)

两者都返回开头所述的错误。

4

1 回答 1

5

你必须指定哪个世界对象应该有脚本。

这是一个生物的例子:

local npcID = 100;
local YourNPC = {};

function YourNPC.YourFunction(eventid, delay, repeats, creature)
      creature:SendUnitSay("My name is " .. creature:GetName(), 255)
end

function YourNPC.OnSpawn(event, creature)
    creature:RegisterEvent(YourNPC.YourFunction, 10000, 5)
end

RegisterCreatureEvent(npcID, YourNPC.OnSpawn, 5)

在生物生成时,该生物会说 5 次“我的名字是”,延迟 10 秒。它只对生物“100”有效,所以不要忘记更改 ID。

Eluna 官方文档:http ://www.elunaengine.com/WorldObject/RegisterEvent.html

于 2019-06-19T21:15:14.447 回答