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

Code not functioning properly.

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

  1. RumDaDuMCPE

    RumDaDuMCPE Witch

    Messages:
    67
    GitHub:
    RumDaDuMCPE
    PHP:
    <?php

    namespace Core\Tasks\FloatingTexts;

    class 
    IceArena extends \pocketmine\scheduler\PluginTask {

    public 
    $ftp = [];

    public function 
    __construct(\Core\Loader $loader, \pocketmine\Player $player) {
    parent::__construct($loader);
    $this->loader $loader;
    $this->player $player;
    }

    public function 
    onRun($tick) {
    $key $this->player->getLowerCaseName();
    if(isset(
    $this->ftp[$key])){
    $this->player->sendMessage("If called!");
    $this->loader->removeAllTexts($this->player);
    unset(
    $this->ftp[$key]);
    } else {
    $this->player->sendMessage("Else called!");
    $this->ftp[] = $key;
    $this->loader->addText($this->player"Ice");
    }
    }
    }
    Issue: Else always gets called! If never gets called when it's supposed to be a loop! Possibly an error with the Arrays. :#
     
  2. XdmingXD

    XdmingXD Baby Zombie

    Messages:
    158
    GitHub:
    xdmingxd
    If you only loop one time , Else will always get called
     
  3. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    Because you are confused about keys and values.
    If you want to store data by keys:
    PHP:
    $this->ftp[$key] = true// any non-null value is ok, but true makes sure things work correctly
    isset($this->ftp[$key]) // check if key $key exists
    unset($this->ftp[$key]);
    If you store by values, and keys do not matter:
    PHP:
    $this->ftp[] = $key// the key will be automatically generated
    in_array($key$this->ftp// check if value $key exists
    As for unset, it is less convenient: https://stackoverflow.com/a/7225113/3990767

    I recommend using the first method. For your reference, the Config class (with type ENUM) also uses the first method
     
  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.