Script being registered multiple times

  • I was playing around with an idea for a script and wondered why I always had error on lines that had no code. The error was going away when I was exiting the game, but errors would persist even after I fixed them in the code.


    So I tested


    Code
    local function onPlayerCommand(event)
    print("Ok");
    end -- function onPlayerCommand
    addEvent("PlayerCommand", onPlayerCommand);


    And, sure enough, it printed just fine when I typed any command; it echoed "Ok".


    hen I modified the code and changed "ok" to anything else (i.e. "Hello world!"). When I reload the world, and typed a command, I got two output :


    Code
    Ok
    Hello world!


    So, I thought, WTF? The event is not being unregistered/cleared when going back to the main menu and reloading a world?? Can it be possible that other scripts suffer from this, too? (i.e. areaprotection, worldedit, etc.) If so, no wonder the server crawls to it's knees when many users connect, get disconnected... especially admins, and commands are typed; they must be applied multiple times!


    Any thoughts, precisions?

  • reload game client if you are doing this in singleplayer
    there should be no issue when restarting a dedicated server


    Well, yes, that's what I figured. But even restarting the game is only but a temporary fix. It is an issue in singleplayer, and it does not help debug scripts.


    As for the server, the term "should" is what bothers me, and thus why I asked for more precision--as I know it happens in singleplayer, but did not bother test a server since I do not necessarily need to test one at the moment.


    My point is that, even when you load a same world in singleplayer multiple times, the console shows the scripts being loaded every time. However, AddEvent is supposed to be "locked" once onEnable is called. The fact that I received two outputs for two different singleplayer game instances (i.e. the same world loaded twice, and the same script loaded twice), shows that AddEvent is persistent, and the scripts are not being unloaded correctly. And I was wondering if that'd explain lagging server issues, too.

  • As @andyzee said: It's indeed a singleplayer-only issue. And only when you return to the main menu. We will fix it soon, we were aware of this issue, but it had a low priority, since Lua scripts would be barely used in singleplayer at all (mainly only for debugging).
    In multiplayer, all scripts will be unloaded once you restart the server ;)

Participate now!

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