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

How to call function inside itself?

Discussion in 'Development' started by Kyd, Aug 19, 2017.

  1. Kyd

    Kyd Zombie Pigman

    Messages:
    678
    GitHub:
    boi1216
    I am trying to call function inside itself for my Murder minigame, but my server get restarted after I try to use it. How can I do it correctly?

    Code
    PHP:
    public function selectDetective(){
        
    $keys array_rand($this->players1);
        if(
    $this->players[$keys]->getName() !== $this->murder) {
            
    $name $this->players[$keys]->getName();
            return 
    $this->detective $name;
        }else{
            return 
    $this->selectDetective();
        }
    }
     
  2. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    Do you get any errors? If so, post it. Your code looks alright.
     
  3. Kyd

    Kyd Zombie Pigman

    Messages:
    678
    GitHub:
    boi1216
    I dont know if it is error , but console display this:
    Code:
    ./start.sh: line 57:  1588 Segmentation fault      "$PHP_BINARY" "$POCKETMINE_FILE" $@
    
     
  4. Sandertv

    Sandertv Zombie Pigman Poggit Reviewer

    Messages:
    786
    GitHub:
    Sandertv
    The error you posted above is an indication you have a recursive function that never stops. $this->players[$keys]->getName() is apparently always $this->murder. We can help you if you post a snippet of what $this->murder is defined like.
     
    HimbeersaftLP, jasonwynn10 and Muqsit like this.
  5. Sandertv

    Sandertv Zombie Pigman Poggit Reviewer

    Messages:
    786
    GitHub:
    Sandertv
    Also note that testing this will crash if you do it alone. If you select the murderer, you're the only player in the array, so you become murderer. You're then always murderer when it starts that function, so it keeps going forever and utimately gets a segmentation fault.
     
  6. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    Avoid using recursion unless it is necessary. Most of the time it can be avoided by a while loop.
    Even if you really need recursion, unless you are definitely sure that your recursion won't exceed the limit (i.e. it will only recurse for a few times), use tail recursion instead.
     
  7. XdmingXD

    XdmingXD Baby Zombie

    Messages:
    158
    GitHub:
    xdmingxd
    why don't you use loops instead ?
     
  8. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    1. Do not re-invent the wheel.
    2. PHP functions will probably handle the loops better than you do and are slightly faster because they're written in C. Example: array_merge. Instead of foreach merging two or more array, you can use array_merge to your advantage (I recommend you reading the PHP documentation, this is a very vague example).
    PHP:
    //Before
    $array = [12345];
    $array2 = [678910];
    $array3 = [];
    foreach(
    $array2 as $value){
        
    $array[] = $value;
    }

    //After
    $array3 array_merge($array$array2);
    PHP:
    //Before:
    $array = [[12345], [678910]];
    $newarray = [];
    foreach(
    $array as $anotherarray){
        foreach(
    $anotherarray as $value){
            
    $newarray[] = $value;
        }
    }

    //After:
    $newarray array_merge(...$array);
     
    Last edited: Aug 19, 2017
  9. Kyd

    Kyd Zombie Pigman

    Messages:
    678
    GitHub:
    boi1216
    Thanks for your explanation @Muqsit, problem solved after little changes in my code
     
  10. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    How is using a loop reinventing the wheel?
     
  11. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    That was for "Avoid recursions, unless necessary".
     
  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.