Solved Add new variable

Discussion in 'Development' started by Dyntes, Oct 18, 2020.

  1. Dyntes

    Dyntes Spider Jockey

    Messages:
    43
    GitHub:
    Dyntes
    Is anyone know how to add new variable to an object? For example,
    PHP:
    ....->getPlayer()->newFunc();
    This code add newFunc() function to Player object. I want to do it... But how?
     
    Last edited: Oct 18, 2020
  2. Primus

    Primus Zombie Pigman

    Messages:
    460
    Edit the PocketMine's source code.

    What are you trying to achieve? What is the end goal here?
     
  3. Dyntes

    Dyntes Spider Jockey

    Messages:
    43
    GitHub:
    Dyntes
    My goal is to make a plugin that can detect if player was in my minigame or not
     
  4. Dyntes

    Dyntes Spider Jockey

    Messages:
    43
    GitHub:
    Dyntes
    If I can't do it, I think I should make a new class extends the player class but I don't know to set the variables same as the defined player object...
     
  5. Primus

    Primus Zombie Pigman

    Messages:
    460
    You're thinking of this in a wrong way.
    PHP:
    public $list = [];

    function 
    addToMatch(Player $player) : void {
       
    $this->list[] = $player;
    }

    function 
    isPlayerInMatch(Player $player) : bool {
       return 
    in_array($player$this->listtrue);
    }

    function 
    removeFromMatch(Player $player) : void {
       if(!
    $this->isPlayerInMatch($player)) return;
      
       unset(
    $this->list[array_search($player$this->listtrue)]);
    }
    Why the heck you think that these methods needs to be inside of a Player class?
    PHP:
    # Context of Listener

    function onPlayerMove(PlayerMoveEvent $event) {
       
    /** @var $plugin PluginBase */
       
    $plugin $this->getMain();
       
    $player $event->getPlayer();
       
    $player->sendTip("In Match? - " . ($plugin->isInMatch($player) ? "True" "False"));
    }
    Take a look at other plugins to see how they've done it. Learn from other's code.

    If none of the code above makes sense to you, then it's time for you take some programming lessons.
     
    Dyntes and minijaham like this.
  6. Dyntes

    Dyntes Spider Jockey

    Messages:
    43
    GitHub:
    Dyntes
    Ok Thx... Aq think such a bed way.. I should change myself from now own
     
    Primus likes this.

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.