1. The forums will be archived and moved to a read only mode in about 2 weeks (mid march).

Player Count

Discussion in 'Development' started by Daniel23, Oct 8, 2017.

  1. Daniel23

    Daniel23 Witch

    Messages:
    65
    GitHub:
    daniel23
    When a player is in a particular gamemode it will add to count on HUD

    This doesn't seem to work?
    PHP:
    foreach($this->plugin->getServer()->getOnlinePlayers() as $player){
        foreach(
    $player as $p){if($p->isSurvival()){$survival=$survival+1;}}
        foreach(
    $player as $p){if($p->isSpectator()){$spectator=$spectator+1;}}
     
  2. Kyd

    Kyd Zombie Pigman

    Messages:
    678
    GitHub:
    boi1216
    Wtf? Why are you foreaching already foreached array? :facepalm:

    Try this:
    PHP:
    foreach($this->getServer()->getOnlinePlayers() as $p){
            if(
    $p->getGamemode() == Player::SURVIVAL){
              
    $survival++;
            }
            if(
    $p->getGamemode() == Player::SPECTATOR){
               
    $spectator++;
            }
    }
     
  3. Jack Noordhuis

    Jack Noordhuis Zombie Pigman Poggit Reviewer

    Messages:
    618
    GitHub:
    JackNoordhuis
    Why are you checking if a player is a spectator AFTER you've already checked if they're in survival?
    PHP:
    $survival $spectator 0;
    foreach(
    $this->getServer()->getOnlinePlayers() as $p) {
            if(
    $p->getGamemode() == Player::SURVIVAL) {
              
    $survival++;
            } elseif(
    $p->getGamemode() == Player::SPECTATOR) {
               
    $spectator++;
            }
    }
     
  4. Kyd

    Kyd Zombie Pigman

    Messages:
    678
    GitHub:
    boi1216
    There's a bracket after checking for Survival, so it don't make big difference
     
  5. Jack Noordhuis

    Jack Noordhuis Zombie Pigman Poggit Reviewer

    Messages:
    618
    GitHub:
    JackNoordhuis
    A simple gamemode check may not be a big difference in performance but it's bad practice. If the check costs more CPU time then it becomes a performance problem.
     
  6. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    :facepalm: This is not a deal as big as you make it sound like. Evaluating the expression $player->getGamemode() == Player::SPECTATOR takes extremely negligible time; focus on the important things and stop complaining about these minor issues and distracting everyone from the main topic. You may say that it's easier to understand the code flow if it is an elseif, but trying to make several posts complaining about it is as pointless as trying to determine if I should write the integral of 1/x as ln(x) or ln|x| at the cost of re-reading the question several times and evaluating the min/max points of the denominator terms (which is unfortunately required in my exam), and equally as pointless as me now trying to determine which words in my post should have the Courier New font.
     
    jasonwynn10 and HimbeersaftLP like this.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.