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

foreach(onlinePlayer) (over 100?)

Comments in 'Plugin Development' started by dxm_hippie, Dec 24, 2015.

  1. dxm_hippie
    Offline

    dxm_hippie Active Member Plugin Developer

    Joined:
    Feb 1, 2015
    Posts:
    413
    Plugins:
    1
    Minecraft User:
    XxDXM_hippiexX
    If you do a foreach($this->getServer()->getOnlinePlayers() as $player) with over 100 players online..will the server crash due to to many nested functions? or are loops different?
  2. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,470
    Minecraft User:
    PrimusLV
    foreach doesn't care how much players you will loop through, but it depends on what you are doing inside of that loop! But @falkirks made cool method to reduce lag for MineReset check it out.
  3. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Nested functions are like resistors in series. Foreach is like resistors in parallel.
  4. dxm_hippie
    Offline

    dxm_hippie Active Member Plugin Developer

    Joined:
    Feb 1, 2015
    Posts:
    413
    Plugins:
    1
    Minecraft User:
    XxDXM_hippiexX
    Well..there will be somthing inside of the foreach loop (obvious) :p so that will get run over 100 times if the player count is over 100..witch will crash the server..correct?
  5. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    But they are still run in parallel.
    The 100 times thing is just when you call a function that calls another function that calls yet another function, etc. When you foreach it, every time you go to the next player, it would exit the previous functions.

    Let's put it in another case. The server core ticking is controlled by something like this in Server.php:

    PHP:
    while($this->running){
      
    $this->tickCount++;
      
    $this->doStuffPerTick();
      
    usleep(50000); // to sleep until the next tick comes, although this isn't the actual implementation of the function, but you can think it like this
    }
    And we have our server running thousands and millions of ticks from this loop.
    PocketKiller, dxm_hippie and TigerHix like this.

Share This Page

Advertisement