Hi, This is Sam BTW Stoopid bans made teh forum lag 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
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.
Example? I just decided to do it all on the web api script (with serialize on the end) look: http://samcraft.samueljh1.net/LeaderboardSystem.php?mode=serialize and then do a http get. I then unserialize in the async. Do you think that's a good idea? It does take longer though...
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.
Unserialize the stuff in onCompletion not onRun! Arrays cannot be shared between threads, only serialized (string) can!
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;
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 } }
Objects are actually memory addresses to structures. Different threads understand the memory addresses differently, but they are still the same memory address.