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

Custom Fuction

Discussion in 'Development' started by DanielYTK, Feb 4, 2017.

  1. DanielYTK

    DanielYTK Zombie

    Messages:
    227
    Why create a custom function?
    What is the use of a custom function?
    How to create a custom function?
     
  2. Junkdude

    Junkdude Zombie

    Messages:
    346
    GitHub:
    JunkDaCoder
    $this->publicfunction($player){
    }
     
  3. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    To put it simply, the same reason we use any functions in plugins. To save time and make things simpler. If you are going to have to run the same code at many different instances, it is way easier to type that code once, then use $this->my function($args); as often as needed later on.

    You can create one pretty easily:
    PHP:
    /*Keep in mind that you would have a lot more in your function, and you can "return" anything you need. This is just an example. */ public function myFunction(Player $player) {
      if(
    $player->getName() === "Steve") {
        return 
    true;
        } else {
        return 
    false;
        }
     
    Let's say our plugin is supposed to give a specific player an item. Using the above function, we can check and return easily.

    PHP:
    /* At another point in your plugin */

    if($this->myFunction($player) === true) {
        
    $player->getInventory()->addItem(Item::get(32201));
        
    $player->sendMessage("Your name is Steve!");
      }
      elseif(
    $this->myFunction($player) === false) {
        
    $player->sendMessage("Your name is not Steve");
      }
    Keep in mind that this is NOT a good example. In practice, you can use functions to run annoying math problems without typing it all every time. You can use functions to run code after you have done your checks, unlike in my example. You can return a player's name, their data you may have stored in a config, write data, parse data, etc. Basically, functions are immensely useful.
     
    applqpak likes this.
  4. Jack Noordhuis

    Jack Noordhuis Zombie Pigman Poggit Reviewer

    Messages:
    618
    GitHub:
    JackNoordhuis
    That's calling a 'custom' function.
    You can declare a simple function in OOP PHP like this:
    PHP:
    public function yourFunctionName(){
      
    // put your code here
    }
    Then you can call this function from any reference you have to an object that declares this function:
    PHP:
    // assuming we are working from your plugins main class (class extending plugin base)
    public function onEnable() {
        
    $this->yourFunctionName();
    }
    You can read more about functions in PHP here. Please refrain from asking PHP questions on these forums, these forums exist to discuss and seek help for PocketMine. Whilst PocketMine is written in PHP it does not mean it's forums should be swamped with PHP questions that have already been answered elsewhere.
     
    applqpak and corytortoise like this.
  5. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    you can also declare anonymous functions
    using something highly similar to
    Code:
    $func = function($arg1, $arg2){/*do stuff*/}
    $func(1,2)
    
     
  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.