Advertisement
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

AsyncTask Problems

Comments in 'Plugin Development' started by Samueljh1H8sLag, Jun 20, 2015.

  1. Samueljh1H8sLag
    Offline

    Samueljh1H8sLag Active Member

    Joined:
    Jun 20, 2015
    Posts:
    168
    Minecraft User:
    Samueljh1_
    Hi, This is Sam BTW :p Stoopid bans made teh forum lag :eek:

    Anyway, here is my asynctask script, which always returns null to the processResult() function:

    PHP:
    <?php

    ###
    # SamCraft SkyWars - Core Plugin | Sql Task
    ###

    namespace Samueljh1\Samcraft\SkyWars\LeaderboardSystem;use pocketmine\plugin\Plugin;

    use 
    pocketmine\scheduler\ServerScheduler;use pocketmine\scheduler\AsyncTask;
    use 
    pocketmine\scheduler\PluginTask;use pocketmine\Server;

    class 
    getdata extends AsyncTask{

    public function 
    __construct(){
    }

    public function 
    onRun(){

    $list file_get_contents("http://samcraft.samueljh1.net/LeaderboardSystem.php");
    $this->players explode("|",$list);

    $this->leaderboard = [];

    foreach(
    $this->$players as $player){

    if(
    $player != null){

    $data explode(",",$player);
    $pname $data[0];
    $pscore $data[1];
    $pkills $data[2];
    $pdeaths $data[3];
    $this->leaderboard[strtolower($pname)] = array($pname,$pscore,$pkills,$pdeaths);

    }

    }

    }

    public function 
    onCompletion(Server $server){

    $server->getPluginManager()->getPlugin("SkyWars-Leaderboard")->processData($this->leaderboard);

    }

    }
    If I cut out everything in the onRun() function after the $this->players = explode("|",$list); part,
    the array from
    PHP:
    $list file_get_contents("http://samcraft.samueljh1.net/LeaderboardSystem.php");
    $this->players explode("|",$list);
    is returned.

    For some reason i cant foreach in the async, but i can in the Main thread. Any suggestions?

    p.s i cant do it in the main thread as it will create WAAY to much lag.

    The leaderboard has 1000s of players stored, so it causes constant lag.

    Look here for the lolz: http://samcraft.samueljh1.net/LeaderboardSystem.php

    Its so big it reaches the browsers width limit like 40 times lmao
  2. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    The explode is OK. However, unthreaded objects and arrays cannot be shared between different threads. You should do the explosion in the main thread, or if you want to, save players in $this->setResult($players) instead.
  3. Samueljh1H8sLag
    Offline

    Samueljh1H8sLag Active Member

    Joined:
    Jun 20, 2015
    Posts:
    168
    Minecraft User:
    Samueljh1_
  4. iJoshuaHD
    Offline

    iJoshuaHD Notable Member Plugin Developer

    Joined:
    Nov 7, 2013
    Posts:
    1,167
    Plugins:
    4
    Minecraft User:
    iJoshuaHD
    why dont you do this:
    Code:
    public function onRun(){
    $this->data = unserialize(file_get_contents("http://samcraft.samueljh1.net/LeaderboardSystem.php?mode=serialize"));
    }
    
    public function onCompletion(Server $server){
    $server->getPluginManager()->getPlugin("SkyWars-Leaderboard")->processData($this->data);
    }
    
    on your processData function, do the stuff from there.
  5. Samueljh1H8sLag
    Offline

    Samueljh1H8sLag Active Member

    Joined:
    Jun 20, 2015
    Posts:
    168
    Minecraft User:
    Samueljh1_
    Thats what i did rofl
  6. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Unserialize the stuff in onCompletion not onRun! Arrays cannot be shared between threads, only serialized (string) can!
  7. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Also don't unserialize things and pass them to setResult, because setResult will serialize it back.
  8. Samueljh1H8sLag
    Offline

    Samueljh1H8sLag Active Member

    Joined:
    Jun 20, 2015
    Posts:
    168
    Minecraft User:
    Samueljh1_
    It worked when i unserialize in onrun
  9. iJoshuaHD
    Offline

    iJoshuaHD Notable Member Plugin Developer

    Joined:
    Nov 7, 2013
    Posts:
    1,167
    Plugins:
    4
    Minecraft User:
    iJoshuaHD
    base in your code, you did foreach stuff onRun() function. do those on the Main thread.

    well how did this miraculously worked for me then? hmmm?

    onRun() function....
    Code:
    $res1 = $db->query("SELECT * FROM MF_Factions ORDER BY powers DESC LIMIT 5");
    $log = [];
    while($res = $res1->fetch_assoc()){
           $log[] = array($res['faction_name'], $res['powers']);
    }
    $this->log = $log;
    
  10. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Well idk then, maybe only objects.
  11. iJoshuaHD
    Offline

    iJoshuaHD Notable Member Plugin Developer

    Joined:
    Nov 7, 2013
    Posts:
    1,167
    Plugins:
    4
    Minecraft User:
    iJoshuaHD
    base on my experience, when you try to pass objects in AsyncTask, and you do not use it onRun() function, its okay.

    for example:
    public function __construct($data, $obj){
    $this->data = $data;
    $this->obj = $obj;
    }

    public function onRun(){
    $this->log = $this->data;
    }

    public function onCompletion(Server $s){
    if($s->getPlayer("Steve") === $this->obj){
    //do stuff
    }
    }
  12. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Objects are actually memory addresses to structures. Different threads understand the memory addresses differently, but they are still the same memory address.

Share This Page

Advertisement