Search Unity

[Solved] Composition for Player Prefab(network)

Discussion in 'Multiplayer' started by shmin, Apr 27, 2020.

  1. shmin

    shmin

    Joined:
    Mar 1, 2020
    Posts:
    2
    Hi, I need some explain how I can split my Player Pfreab class into small pieces. For example my Player class contains 20 Commands 20 Rpc and 20 TargetRpc methods, it's very hard to navigate through them.

    I want so split it by class "containers" like PlayerCommnds, PlayerRpcs, PlayerTargetRpc.

    I tryied to create field in Player class, move method into this field type and call, but it causes diffrent network errors. When I revert method back to Player it's again work great.

    What is correct way to split Player when it growth to 999 methods?

    Code (CSharp):
    1.  
    2. public class Player : NetworkBehaviour
    3.     {
    4.         public ServerCommands ServerCommands;
    5.      }
    6.  
    7. public class ServerCommands
    8.     {
    9.         [Command]
    10.         public void CmdStartGame(NetworkIdentity identity)
    11.         {
    12.              // Do magic
    13.         }
    14.  
    15.  
    16. public class GameManager
    17. {
    18.       Player findLocalPlayer = ...;
    19.       findLocalPlayer.ServerCommands.CmdStartGame(findLocalPlayer .NetworkIdentity)
    20. }
    21.  
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    These different classes need to be in their own separate files with the same filename as the new class, they all need to inherit from NetworkBehaviour, and they all separately need to be attached to the same Player GameObject prefab.
     
  3. shmin

    shmin

    Joined:
    Mar 1, 2020
    Posts:
    2
    Thank you, my fault was that I thinking only from the code side and forgot to add those scripts to Player prefab.
     
    Joe-Censored likes this.