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

Random spawn

Discussion in 'Development' started by armagadon159753, Oct 1, 2017.

  1. armagadon159753

    armagadon159753 Zombie

    Messages:
    217
    GitHub:
    armagadon159753
    how can i make that when a player spawn or respawn, it appears aleatorymant on x spawn different
    Thank for you reply ^~^
     
  2. Kyd

    Kyd Zombie Pigman

    Messages:
    678
    GitHub:
    boi1216
    There are 2 ways..

    1 Way
    You can register random spawn and then teleport player into it
    Example:
    PHP:
    /** @var array $spawns */
    public $spawns = [];

    /** @var int $count */
    public $count 0;

    //Register spawns
    public function registerSpawns(){
            
    $this->spawns["spawn1"] = new Vector3(1,1,1);
            
    $this->spawns["spawn2"] = new Vector3(2,2,2);
            
    $this->spawns["spawn3"] = new Vector3(3,3,3);
    }
    To teleport player to random spawn you can do something like this:
    PHP:
    public function selectSpawn(Player $p){
           
    $spawn $this->spawns["spawn" mt_rand(1,3)];
           if(
    $spawn instanceof Vector3){ //Check if that spawn is registred
                 
    $p->teleport($spawn);
           }
    }

    2 Way

    You can just select random x,y,z instead of saving positions
    Example:

    PHP:
    public function selectSpawn(Player $p){
            
    $x mt_rand(50,80); //Select value between 50,80
            
    $y 30;
            
    $z mt_rand(30,100); //Select value between 30,100
            
    $position = new Vector3($x$y$z);
            
    $p->teleport($x,$y,$z);
    }
     
    armagadon159753 likes this.
  3. armagadon159753

    armagadon159753 Zombie

    Messages:
    217
    GitHub:
    armagadon159753
    PHP:
    $spawnPoint rand(13);
    if(
    $this->spawnPoint == 1){
      
    $player->teleport(new Vector3(-16051, -120));
      
    $player->sendMassage("spawn 1");
    }
    elseif(
    $this->spawnPoint == 2){
      
    $player->teleport(new Vector3(-12051, -120));
      
    $player->sendMessage("spawn 2");
    }
    elseif(
    $this->spawnPoint == 3){
      
    $player->teleport(new Vector3(-15051, -120));
      
    $player->sendMassage("spawn 3");
    }
    This is correct ?
     
  4. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    yes but unoptimised
     
    armagadon159753 likes this.
  5. armagadon159753

    armagadon159753 Zombie

    Messages:
    217
    GitHub:
    armagadon159753
    Then the first exemple is the best for me !? Thank
     
  6. WinterBuild7074

    WinterBuild7074 Zombie Pigman

    Messages:
    693
    GitHub:
    winterbuild7074
    Your code looks correct.
    You can use PlayerLoginEvent if the player joins and PlayerRespawnEvent if the player respawns. If you want do a completely random spawn:
    PHP:
    /**
     * @var Player $player
     */
    $randX mt_rand(-200200); // chooses a random X position between -200 and 200.
    $randY mt_rand(0128); // chooses a random Y position between 0 and 128.
    $randZ mt_rand(-200200); // chooses a random Z position between -200 and 200.
    $randPos = new Vector3($randX$randY$randZ); // create a Vector3
    $player->teleport($randPos); // teleport the player to the random position.
     
    armagadon159753 likes this.
  7. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    something like the first way but add it like $spawns[] = ... and use $rand= $spawns[mtrand(0,count($spawns))]
     
  8. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    Y-coordinate shouldn't be a static or a random number unless that's your aim. Set Y as Level::getHighestBlockAt(x, z) + 1.
     
  9. SkySeven

    SkySeven Baby Zombie

    Messages:
    145
    GitHub:
    SkySevenMC
    no, it's not $this->spawnPoint but $spawnPoint :p
     
    armagadon159753 and jasonwynn10 like this.
  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.