why not working '['

Discussion in 'Development' started by Hoyee, Mar 15, 2020.

  1. Hoyee

    Hoyee Slime

    Messages:
    90
    Code:
    $this->db["BlueHome"] = array('x'=>(int)$player->getX(),'y'=>(int)$player->getY(),'z'=>(int) $player->getZ());
                    $this->database->setAll($this->db);
      $this->database->save();


    Code:
    $this->db["RedHome"] = array('x'=>(int)$player->getX(),'y'=>(int)$player->getY(),'z'=>(int)$player->getZ());
                    $this->database->setAll($this->db);
      $this->database->save();


    Code:
    if(isset($this->db["blue"]) && ($this->db["red"])){
    
                $this->db["blue"]->setSpawn(new Vector3($this->db["BlueHome"] -> ["x"], ["y"], ["z"]));
                $this->db["red"]->setSpawn(new Vector3($this->db["RedHome"] -> ["x"], ["y"], ["z"]));
    
              }


    It says

    "syntax error, unexpected '[', expecting identifier (T_STRING) or variable (T_VARIABLE) or '{' or '$'" (EXCEPTION)

    in here

    $this->db["blue"]->setSpawn(new Vector3($this->db["BlueHome"] -> ["x"], ["y"], ["z"]));
    $this->db["red"]->setSpawn(new Vector3($this->db["RedHome"] -> ["x"], ["y"], ["z"]));
     
  2. HimbeersaftLP

    HimbeersaftLP Fish

    Messages:
    2,361
    GitHub:
    HimbeersaftLP
  3. Hoyee

    Hoyee Slime

    Messages:
    90
    Sorry I saw but I tried to make another way. So I think many things solved but here is one left.

    it crash and says "Call to a member function setSpawn() on null" (EXCEPTION) in "

    here is the code
    Code:
    if(isset($this->db["blue"]) && ($this->db["red"])){
    
                    $blueplayerNames = $this->db["blue"];
                    $redplayerNames = $this->db["red"];
    
                    $blueplayers = $this->getServer()->getPlayer($blueplayerNames);
                    $redplayers = $this->getServer()->getPlayer($redplayerNames);
    
                    $BlueHome = $this->db["BlueHome"];
                    $RedHome = $this->db["RedHome"];
    
                $blueplayers->setSpawn(new Vector3($BlueHome["x"], $BlueHome["y"], $BlueHome["z"]));
                $redplayers->setSpawn(new Vector3($RedHome["x"], $RedHome["y"], $RedHome["z"]));
    
              }
     
  4. HimbeersaftLP

    HimbeersaftLP Fish

    Messages:
    2,361
    GitHub:
    HimbeersaftLP
    That code won't work, not even close. Please look at the example I provided in my second answer and try to understand it.
    PHP is not numPy, if you want to operate on an array, you will need to do it one element at a time using a foreach loop.
     
  5. Hoyee

    Hoyee Slime

    Messages:
    90
    I edit. You mean use foreach like this?
    Code:
    if(isset($this->db["blue"]) && ($this->db["red"])){
    
                    $blueplayerNames = $this->db["blue"];
                    $redplayerNames = $this->db["red"];
    
                    foreach($blueplayerNames as $blueplayerNamesFE){
    
                    $blueplayers = $this->getServer()->getPlayer($blueplayerNamesFE);
    
                    $BlueHome = $this->db["BlueHome"];
    
                    $blueplayers->setSpawn(new Vector3($BlueHome["x"], $BlueHome["y"], $BlueHome["z"]));
    
                  }//foreach
    
                  foreach($redplayerNames as $redplayerNamesFE){
    
                    $redplayers = $this->getServer()->getPlayer($redplayerNamesFE);
    
                    $RedHome = $this->db["RedHome"];
    
                    $redplayers->setSpawn(new Vector3($RedHome["x"], $RedHome["y"], $RedHome["z"]));
    
                  }//foreach
    
            }
    but is says "Invalid argument supplied for foreach()"
     
  6. HimbeersaftLP

    HimbeersaftLP Fish

    Messages:
    2,361
    GitHub:
    HimbeersaftLP
    Can you send your config file?
     

Share This Page

  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.