addEvent()

  • I am having trouble understanding how addEvent is supposed to work. Given the following example:


    [lua]addEvent("eventName", listenerFunction1);
    addEvent("eventName", listenerFunction2);[/lua]


    I would expect listenerFunction1 and listenerFunction2 to listen to the same event "eventName". However, it seems only one or the other can be added as a listener. This makes scripting separate command handlers impossible. For example, I have:


    [lua]
    function motdCommands(event)
    -- some code here
    end
    addEvent("PlayerCommand", motdCommands);
    [/lua]


    [lua]
    function areaProtectCommands(event)
    -- some code here
    end
    addEvent("PlayerCommand", areaProtectCommands);
    [/lua]


    I would have expected both of these functions to have registered as listeners of the PlayerCommand event. But only one of them listens to that event. Am I wrong? Both were reported to have registered successfully in the console output.

  • Sorry for the late response!


    Unfortunately, only one event can be linked to a function. Otherwise it would make things really complicated (especially when coming to callbacks and return values).
    You could create a central function which calls all other functions:


    [lua]function playerCommand(event)
    motdCommands(event);
    areaProtectCommands(event);
    end
    addEvent("PlayerCommand", playerCommand);


    function motdCommands(event)
    -- code
    end


    function areaProtectCommands(event)
    -- code
    end[/lua]

  • Doesn't make it any easier for the player to just plug their scripts in though. I guess I am just too used to Google's guava event bus. You can register multiple listeners to a single event. They all get notified when an event takes placed based on method signatures and a @Subscribe annotation within the registering listener. Even Lua itself allows multiple listeners to one event if I remember the manual correctly.

  • I think I may set up a global table containing the player command and function to execute. Then as players add their scripts, they just add their commands to the player command table.

  • For now I will just go with the way you pointed out. I am still learning about require, and modules, etc. I want the player to be able to place their scripts in the script directory and be automatically required by the main script when the server loads for the first time. However, my Lua knowledge is still limited (I am a java guy), so I may continue this on the 19th of December after reading some more.

  • This is not a technical limitation, it's a decision on our side to keep things simple (several functions per event become really confusing when it comes to callbacks and results) and also getting better performance result^^


    But using a global table sounds like a good approach!
    LUA things will generally become easier as soon as our wiki is available ;)

  • I did read that when a Lua module is loaded that there is a global module table somewhere. I was trying to takes that approach at one point but the default loading of scripts seemed to be alphabetical by directory name, where each script has a directory and a definition.xml. This wasn't working because the onEnable event seemed to be executed when the script was loaded, so not every script had been loaded yet, so certain things didn't exist from other scripts yet, like the scriptDatabase objects. This meant that I couldn't create a main script and have others tie into it.


    It's not a problem; to me it is just another challenge, and I love challenges, especially in programming. I may look into Lua events. I rebuilt luaj from source and ran that with the server and it appears that you all haven't modified your version. If I am correct, I think I will look into extending that a bit.

Participate now!

Don’t have an account yet? Create a new account now and be part of our community!