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

Comparing two positions

Discussion in 'Facepalm' started by Hipster, Sep 1, 2017.

  1. Hipster

    Hipster Zombie

    Messages:
    214
    I tried comparing two positions, it didn't work, i checked the coordinates they are the same, my code is:
    PHP:
    $pos = new Position($block->getX(), $block->gety(), $block->getZ(), $this->getServer()->getLevelByName("lobby");
    $pos2 = new Position($block["x"], $block["y"], $block["z"], $this->getServer()->getLevelByName("lobby");
    if(
    $pos === $pos2){
    //my code here
    }
    It is not working, coordinates are same, i debugged
     
    Last edited: Sep 2, 2017
  2. LCraftPE

    LCraftPE Silverfish

    Messages:
    20
    GitHub:
    LCraftPE
    Hello,
    Your code is full of errors: $block can not be a object and an array at the same time, you have to choose! Also the Block class is an extend of Position class, so you don't need to create an instantiated object of the Position class... So I give you a code that compares the position of 2 blocks: $block and $block2!
    PHP:
    if($block == $block2// Your code here
    When using the comparison operator (==), the objects are compared in a simple way: two objects are equal if they have the same attributes and values and they are instances of the same class.
    Keep me informed.
     
  3. Hipster

    Hipster Zombie

    Messages:
    214
    didn't work, tried this @LCraftPE
    PHP:
    $bk $block//block from playerinteractevent
    $xi $blocks["x"];
                
    $yi $blocks["y"];
                
    $zi $blocks["z"];
                
    $pos = new Position($xi,$yi,$zi,$this->getServer()->getLevelByName("lobby"));
                if(
    $pos == $bk){
                    
    //my code
                
    }
     
  4. LCraftPE

    LCraftPE Silverfish

    Messages:
    20
    GitHub:
    LCraftPE
    Re,
    I explained to you, the comparison operator (==) only works if the two instantiated classes are same. But in your code Position class and Block class are not identical. Here is the real code:
    PHP:
    $block2 = new Block($blocks["x"], $blocks["y"], $blocks["z"], $this->getServer()->getLevelByName("lobby"));
    if(
    $block == $block2// Your code here
    If this code doesn't work, could you var_dump $block and give me what you get please.
    Keep me informed.
     
  5. Hipster

    Hipster Zombie

    Messages:
    214
    didn't work, :/ my block is a sign btw
     
  6. LCraftPE

    LCraftPE Silverfish

    Messages:
    20
    GitHub:
    LCraftPE
    Re,
    Keep me informed.
     
  7. Hipster

    Hipster Zombie

    Messages:
    214
    Nvm I'll just compare the coords and level.
     
  8. LCraftPE

    LCraftPE Silverfish

    Messages:
    20
    GitHub:
    LCraftPE
    Hello,
    If the comparison operator (==) doesn't work, 2 possibility are proposed to us:
    1. The first is that one of the properties of $block has not been defined for $block2 and vice versa.
    2. The second is that the coordinates in the $block array are not properly saved.
    It's up to you to check this out!
    Keep me informed.
     
  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.