Search Unity

How to spawn player AFTER a while you are connected to the server?

Discussion in 'Multiplayer' started by deadlycrow, Apr 6, 2017.

  1. deadlycrow

    deadlycrow

    Joined:
    Feb 10, 2014
    Posts:
    166
    what i mean is a custom way to spawn the player at any time you want being already connected to the server, with a popup in the screen, being able to see the world in action in the background (pretty much like the character selection screen in "CHIVALRY") so i dont find a way to do that, because everything involves the "OnServerAddPlayer" method, which is triggered automatically when you are succesfully connected to the server and the "auto create player" toggle is checked...

    SO what can i do?? maybe it is a noob question but being a long-time unity user (over 8 years using it) i am having a really hard time looking for this kind of info :/, sadly. thanks in advance!

    PD: i already have the setup of my "player selection popup" with UI system. just need a way to press one of the buttons and spawn the correct player prefab :(
     
  2. Deleted User

    Deleted User

    Guest


    Just override OnServerAddPlayer in yout custom NetworkManager;

    Code (CSharp):
    1.  
    2.     /// <summary>
    3.     /// Add Player Hook;
    4.     /// </summary>
    5.     /// <param name="conn"></param>
    6.     /// <param name="playerControllerId"></param>
    7.     public override void OnServerAddPlayer(NetworkConnection conn, short playerControllerId)
    8.     {
    9.        //wait some time with Coroutine or whatever you want
    10.  
    11.        //Spawning method part here:
    12.        GameObject Player = (GameObject)Instantiate(playerPrefab, Vector3.zero, Quaternion.identity);
    13.        NetworkServer.AddPlayerForConnection(conn, Player, playerControllerId);
    14.  
    15.     }
    16.  
    17.  
     
  3. deadlycrow

    deadlycrow

    Joined:
    Feb 10, 2014
    Posts:
    166
    thanks! but i dont totally get how this works... so what should i do when i press one of my player buttons? i have 4 differente prefabs :/
     
  4. Deleted User

    Deleted User

    Guest

    Well, when you press the button you are called ClientScene.AddPlayer(0), and also you can send a message to server with index of player, it's better to have an array of prefabs on server and spawn by needs. Try to write a simple NetworkMessage that will tell server which prefab you would like to spawn for your NetworkConnection.
     
    Last edited by a moderator: Apr 6, 2017
  5. deadlycrow

    deadlycrow

    Joined:
    Feb 10, 2014
    Posts:
    166
    well it seems like AddPlayer does not do anything :( when i press the button nothing happens.
    this is my current code:


    Code (CSharp):
    1.   public void SelectElement(int i){
    2.         curElement = i;
    3.  
    4.         ClientScene.AddPlayer(0);
    5.     }
    6.  
    7.     public override void OnServerAddPlayer(NetworkConnection conn, short playerControllerId) {
    8.  
    9.         Vector3 randomPos = new Vector3(
    10.             Random.Range(spawnPosA.position.x,spawnPosB.position.x),
    11.             Random.Range(spawnPosA.position.y,spawnPosB.position.y),
    12.             Random.Range(spawnPosA.position.z,spawnPosB.position.z)
    13.         );
    14.  
    15.         curPlayer = GameObject.Instantiate(playerPrefabs[curElement],randomPos , Quaternion.identity);
    16.         NetworkServer.AddPlayerForConnection(conn, curPlayer, playerControllerId);
    17.  
    18.     }
    so i call "SelectElement()" everytime i press one of the four UI buttons (each one with a different int number).
     
  6. Deleted User

    Deleted User

    Guest

    No no, Server doesn't know about curElement value, try to tell hime via client.Send. Second part of it it's connect to the server.
     
  7. deadlycrow

    deadlycrow

    Joined:
    Feb 10, 2014
    Posts:
    166
    well, i'd do it if know how to haha, am i currently connected i guess? i started the server as "host" so i am connected, right? so no clue how to let know the server(myself) i pressed the UI button :/
     
  8. Deleted User

    Deleted User

    Guest

    Where is your OnServerAddPlayer placed? You need to use it in NetworkManager class.
     
    Last edited by a moderator: Apr 6, 2017
  9. deadlycrow

    deadlycrow

    Joined:
    Feb 10, 2014
    Posts:
    166
    this is my full code:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using UnityEngine.Networking;
    6.  
    7. public class LevelManager : NetworkManager {
    8.  
    9.     public static LevelManager instance;
    10.  
    11.     [SerializeField]
    12.     public int curElement = 0;
    13.  
    14.     [SerializeField]
    15.     public GameObject explosionFX;
    16.  
    17.     public GameObject[] playerPrefabs;
    18.  
    19.     public Transform spawnPosA,spawnPosB;
    20.  
    21.     public GameObject selectionMenu;
    22.  
    23.     GameObject curPlayer;
    24.     bool showSelection, isConnected;
    25.  
    26.     // Use this for initialization
    27.     void Awake () {
    28.         instance = this;
    29.     }
    30.    
    31.     // Update is called once per frame
    32.     void Update () {
    33.         if (isNetworkActive)
    34.         {
    35.             isConnected = true;
    36.         }
    37.         else
    38.         {
    39.             isConnected = false;
    40.         }
    41.         //-----------------------------------------
    42.  
    43.         showSelection = isConnected;
    44.  
    45.         if (curPlayer)
    46.         {
    47.             showSelection = false;
    48.         }
    49.  
    50.         selectionMenu.SetActive(showSelection);
    51.     }
    52.     public void SelectElement(int i){
    53.         curElement = i;
    54.  
    55.         ClientScene.AddPlayer(0);
    56.     }
    57.     public override void OnServerAddPlayer(NetworkConnection conn, short playerControllerId) {
    58.  
    59.    //     Debug.Log("trying to spawn...");
    60.  
    61.         Vector3 randomPos = new Vector3(
    62.             Random.Range(spawnPosA.position.x,spawnPosB.position.x),
    63.             Random.Range(spawnPosA.position.y,spawnPosB.position.y),
    64.             Random.Range(spawnPosA.position.z,spawnPosB.position.z)
    65.         );
    66.  
    67.         curPlayer = GameObject.Instantiate(playerPrefabs[curElement],randomPos , Quaternion.identity);
    68.         NetworkServer.AddPlayerForConnection(conn, curPlayer, playerControllerId);
    69.  
    70.     }
    71.  
    72. }
     
  10. Deleted User

    Deleted User

    Guest

    Hm, try to remove ClientScene.AddPlayer, and remove comment Debug.Log("trying to spawn"); , let's see what will happen.
     
  11. deadlycrow

    deadlycrow

    Joined:
    Feb 10, 2014
    Posts:
    166
    well, of course nothing will happen if i remove that... i will only set the int to an specific value (depending of what UI button i press)... so?