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

My AsyncTask doesn't (really) work

Discussion in 'Development' started by moska, May 8, 2017.

  1. moska

    moska Baby Zombie

    Messages:
    105
    GitHub:
    supermaxalex
    Hey everyone,

    So, I did a AsyncTask because there are many blocks to set on what I'm doing.
    When the task is executed, everything is okay, no error, but, nothing happens.
    I did some var_dump & echo and the code got executed correctly.
    I don't know if it need an onCompletation() or anything else, but if it's that, I want to know how to do/use it.

    PHP:
    class ResetTask extends AsyncTask{
        
    //$placed contains Vector3(x, y, z), same for $breaked
        
    public function __construct(Level $levelIdbool $place, array $placedbool $break, array $breaked){
            
    $this->levelId $levelId->getId();
            
    $this->place $place;
            
    $this->placed $placed;
            
    $this->break $break;
            
    $this->breaked $breaked;
        }

        public function 
    onRun(){
            if(
    $this->place == true){
                foreach(
    $this->placed as $p){
                    
    $this->getFromThreadStore("generation.level{$this->levelId}.manager")->setBlockIdAt($p->x$p->y$p->zBlock::SANDSTONE);
                    
    $this->getFromThreadStore("generation.level{$this->levelId}.manager")->setBlockDataAt($p->x$p->y$p->z2);
                }
            }

            if(
    $this->break == true){
                foreach(
    $this->breaked as $b){
                    
    $this->getFromThreadStore("generation.level{$this->levelId}.manager")->setBlockIdAt($b->x$b->y$b->zBlock::AIR);
                }
            }
        }
    }
    Thanks !
     
  2. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    1. There are multiple async workers on a server. Did you execute this on all workers? https://github.com/pmmp/PocketMine-MP/blob/master/src/pocketmine/level/Level.php#L390
    2. The data in the thread store generator do not get automatically exported to the level reference in the main thread. Eventually, someone has to copy the changes to the main thread.

    As far as I understand, you're trying to change blocks in an AsyncTask in the hope that it reduces lag. However, this is most likely not going to work. I am not sure where the lag of changing a lot of blocks comes from, but it should either be from editing the block data and block cache data or from creating packets to send to players, both of which are executed in the main thread, so setting blocks from other threads is not going to work anyway.
    On the other hand, you have to serialize blocks to pass to the worker thread, and someone has to unserialize the changes from the worker thread or otherwise import your changes somewhere, both of which will further increase the load.
     
  3. moska

    moska Baby Zombie

    Messages:
    105
    GitHub:
    supermaxalex
    Tested this, but nothing happens too
    So it's useless to use an AsyncTask for this ?
     
  4. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    I already told you. Nothing will happen if you don't fetch the results.
    Using my restaurant analogy again. The customer asks the waiter for a cup of water, but instead of taking it directly (this may take some time and stop the waiter from serving indeed), the waiter calls his son to come to the restaurant to hand him the cup of water. Yes, eventually the cup of water will come to the customer, but he has to make the phone call and take the cup of water from his son. Do you think this will be faster than directly fetching the cup of water?
    The problem you're having right now is that the water is ignoring the cup of water his son handed to him. But even if he does not ignore it, the service won't be any better, would it?
     
    HimbeersaftLP and jasonwynn10 like this.
  5. SkySeven

    SkySeven Baby Zombie

    Messages:
    145
    GitHub:
    SkySevenMC
    I have the same error with my AsyncTask, but i don't understand how to fix it...
     
  6. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    If you don't know what you're doing, don't do it :rolleyes:
     
  7. SkySeven

    SkySeven Baby Zombie

    Messages:
    145
    GitHub:
    SkySevenMC
    I know what i am doing but its not Working ;(
     
  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.