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

The best way to set blocks in a large area without getting lag

Discussion in 'Development' started by Levi, Jun 8, 2018.

  1. Levi

    Levi Skeleton

    Messages:
    955
    GitHub:
    captainleviftw
    What is the best way to set blocks in a large area without getting lag?
     
  2. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    PocketMine's SubChunkIteratorManager class can help cut down on many method calls.

    I used it on a decent system in the following picture xe.JPG
    That's 15.2 million blocks in almost a minute!

    P.S. There's no asynchronous-magic involved.
    Also P.S, Compared to XenialDan's MagicWE and MagicWE2, SubChunkIteratorManager is capable of performing way too fast. That very area takes more than 20 minutes for MagicWE.

    Why is it soo fast?
    It cuts down on block updates, updating cached blocks in Level class, notifying ChunkLoaders about block changes and much more.
    It's about executing just the required code in an expensive loop.
    Code:
    //Level->setBlock()
    for(loop 15 million blocks){
        setBlockInChunk(block);
        checkNearbyBlocks(block);
        scheduleBlockUpdates(block);
        updateCachedBlocks();
        foreach(players in chunk as player){
            sendBlockUpdateTo(player)
        }
    }
    
    //Using SubChunkIteratorManager
    for(loop 15 million blocks){
        setBlockInChunk(block);
    }
    updateCachedBlocks();
    foreach(changedChunks as chunk){
        foreach(players in chunk as player){
            sendBlockUpdateTo(player);
        }
    }
    
     
    Last edited: Jun 9, 2018
  3. Levi

    Levi Skeleton

    Messages:
    955
    GitHub:
    captainleviftw
    why foreach players in chunk?
     
  4. Levi

    Levi Skeleton

    Messages:
    955
    GitHub:
    captainleviftw
    anyway i think you did purposely send thatt code in that stlye
     
  5. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    All the players in the chunk (and also the players who can see the chunk) are sent UpdateBlockPacket which sends them the block graphic.

    It's an example code of things that Level->setBlock() executes vs. things that SubChunkIteratorManager executes.

    I'm working on a public WorldEdit plugin that uses PocketMine's SubChunkIteratorManager class.
     
  6. Levi

    Levi Skeleton

    Messages:
    955
    GitHub:
    captainleviftw
    where did you get the sendBlockUpdate?
    PHP:
    for($i =0$i <= 15000000$i++){
               
            }
    how do you get players in chunk?
     
  7. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    "Players in chunk" is not accurate. It should be "players using chunk", i.e. players who will receive changes in that chunk. For example, players very far away or players from another world will not see the chunk.
     
    Muqsit likes this.
  8. Levi

    Levi Skeleton

    Messages:
    955
    GitHub:
    captainleviftw
    theres a method to check that?
     
  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.