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

Get Entities in a certain area.

Discussion in 'Facepalm' started by RumDaDuMCPE, Aug 19, 2017.

  1. RumDaDuMCPE

    RumDaDuMCPE Witch

    Messages:
    67
    GitHub:
    RumDaDuMCPE
    Let's say, I have an area -

    PHP:
    $x = [100,200];
    $z = [300,400];
    How do I get the Entities within this area? I do know how to check whether a player is in the area - that is by using events.

    I'm trying to count the Entities in this area and display it in a FloatingTextParticle.
     
  2. DanielYTK

    DanielYTK Zombie

    Messages:
    227
    tried this (ps: i don't tested)
    PHP:
    $entities 0;

    foreach(
    $level->getEntities() as $ent){
        if(
    $this->isInSide($ent)){ // check if entity is in area
            
    $entities++;
        }
    }
     
  3. RumDaDuMCPE

    RumDaDuMCPE Witch

    Messages:
    67
    GitHub:
    RumDaDuMCPE
    I don't think that'll work. But you might have helped me there! This might work:

    PHP:
    <?php

    $lvl 
    Server::getInstance()->getDefaultLevel();
    $ents $lvl->getEntities();
    foreach (
    $ents as $ent) {
    $x $ent->getX();
    $z $ent->getZ();
    $xx = [100,200];
    $zz = [300,400];
    if (
    $x() >= min($xx) && $x() <= max($xx) && $z >= min($zz) && $z <= max($zz)) {
    //How can I count the number of Entities INSIDE this region?
    //doing count($ent); would return 1, right?
    }
    }
    ?>
     
  4. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    Set a counter variable. Every time an entity is valid, add one to the counter.
     
    ProjectDefect likes this.
  5. RumDaDuMCPE

    RumDaDuMCPE Witch

    Messages:
    67
    GitHub:
    RumDaDuMCPE
    PHP:
    public (int) $counter 0;

    If (
    $ent instanceof \pocketmine\Player) {
      while (
    $this->counter count($ent)) {
        
    $this->counter++;
      }
    }
    Echo 
    "Entities in the region: ".$this->counter;
    Would this work SOF3? Thanks for helping me :)
     
  6. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    What do you want to do with `while($this->counter = count($ent))`?
     
  7. DanielYTK

    DanielYTK Zombie

    Messages:
    227
    look this:
    PHP:
    $entities 0;
    foreach(
    $level->getEntities() as $ent){
        
    $entities++; // its is same that: $entities = $entities+1;
    }
     
  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.