Advertisement
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Repeating task per 1 tick or PlayerMoveEvent?

Comments in 'Plugin Development' started by Legoboy0215, Jan 10, 2016.

  1. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,724
    Minecraft User:
    Legoboy0215
    Basically, everything is in the title. Should I use PlayerMoveEvent? Or loop through every player and check if their doing something?
  2. Tim // robske Büba
    Offline

    Tim // robske Büba Notable Member

    Joined:
    Feb 26, 2014
    Posts:
    606
    Minecraft User:
    robske_110
    Events are better (theyre much faster)
  3. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    If you just want to check things like portals, use a repeating task with auto adjusting ticking rate like I told you before. You wouldn't want to use PlayerMoveEvent just for checking portals unless you are lazy and want to do things conveniently, because that may be a source of lag.
    Legoboy0215 likes this.
  4. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,724
    Minecraft User:
    Legoboy0215
    You never did XD
  5. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    I think I did. Schedule a task of every tick, check if microtime difference from last tick greater than your interval, compare positions of all players.
    This depends on what you want to do though. If you want to check only when a player has truly moved, use PlayerMoveEvent instead.
  6. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,724
    Minecraft User:
    Legoboy0215
    That wasn't auto adjusting :p Or is it same? Any ways, I remember it now! Ok. A task running every tick. Would it lag a lot?
  7. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    You can choose to do it every two ticks. (Also, since you aren't looking for accurate timing, it's not necessary to measure the microtime difference here.)
    If you use PlayerMoveEvent, PocketMine goes through a sophisticated series of function calls and checks to reach your handler function. For scheduler, AFAIK, the background things done by PocketMine is less complicated.
    However, this difference is less noticeable. The notable performance difference comes from the number of times the function is called.
    We have to run the same code for every player anyway. Since we assume that most players move every tick, and that your implementation for each player is not laggy (you shouldn't do that anyway), either method has little difference in terms of the code run per player from your plugin. What makes the difference is server internal computation.
    The time for PlayerMoveEvent is O(n), where n is number of online players. O(1) is the time for one PluginManager::callEvent(PlayerMoveEvent) call.
    The time for scheduler to call your PluginTask is O(1), where O(1) is the time for one check for one PluginTask (whether it should run in this tick).
    With either method, your plugin runs for the same time (unless 90% of your players don't like walking). Meanwhile, calling an event for n players, given n≥1 (if there are no players, who cares how much lag there is?), i.e. for n times, is apparently slower than calling one task for once. Both are considered in one tick.
    Last edited: Jan 10, 2016
    Legoboy0215 likes this.
  8. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder

Share This Page

Advertisement