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

Solved How to create a simple loop with task ??

Discussion in 'Development' started by Matrox, Jun 17, 2019.

  1. Matrox

    Matrox Slime

    Messages:
    92
    GitHub:
    MatroxMC
    Hello I would like to know how I can simply create a task loop in the Main ?

    Thank you :)
     
  2. Akmal

    Akmal Spider

    Messages:
    10
    GitHub:
    AkmalFairuz
    This is your mean?:

    Example Function:
    PHP:
    public function nameTask(){
           foreach(
    $this->getServer()->getOnlinePlayers() as $player){
                
    $player->sendTip(" HELLO WORLD! ");
           }
    }
    Main.php
    PHP:
    //...
    $interval 20// 1 second = 20 ticks
    $this->getScheduler()->scheduleRepeatingTask(new Task($this), $interval);
    //....
    Task.php
    PHP:
    <?php

    namespace Task;

    use 
    pocketmine\scheduler\Task as PMTask;

    class 
    Task extends PMTask {
            public function 
    __construct(Main $plugin) {
                   
    $this->plugin $plugin;
            }

            public function 
    onTick() : void {
                    
    //call function
                    
    $this->plugin->nameTask();
            }
    }
    This code call function every 1 seconds.
     
    Last edited: Jun 17, 2019
  3. Matrox

    Matrox Slime

    Messages:
    92
    GitHub:
    MatroxMC
    Error: "Cannot instantiate abstract class pocketmine\scheduler\Task" (EXCEPTION) in "plugins/Core/src/matrox/Main" at line 51
     
  4. Akmal

    Akmal Spider

    Messages:
    10
    GitHub:
    AkmalFairuz
    PHP:
    use pocketmine\schedule\Task;
     
  5. Matrox

    Matrox Slime

    Messages:
    92
    GitHub:
    MatroxMC
    Works thanks :)
     
  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.