luaazerothcoreeluna-lua-engine

worldobject:RegisterEvent error on indexing worldobject


When I attempt to use the examples shown on site for making a worldobject registerevent I get an error on the worldobject, implying it is a nil value like so:

lua_scripts/test.lua:5: attempt to index global 'worldobject' (a nil value)

Tried a few different examples with the same outcome, so naturally I expect its probably some oversight on my part.

Tested examples:

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)

Both return the error stated in the beginning.


Solution

  • You have to specify which worldobject should have the script.

    Here is an example for a creature :

    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)
    

    On the creature spawn, the creature will say 5 times "My name is " with 10sec delay. It'll only work for the creature "100" so, don't forget to change the ID.

    Official Eluna doc: http://www.elunaengine.com/WorldObject/RegisterEvent.html