Guest User

Untitled

a guest
Apr 5th, 2018
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.57 KB | None | 0 0
  1. //  **** Entity.cs ***
  2. public abstract class Entity
  3. {
  4.     public int networkID;
  5.  
  6.     // This is important for network communication - see overrides in Player.cs and
  7.     // usage in Network.cs
  8.     abstract void ReplicateVarFromServer(int idx, object value);
  9.     abstract void InvokeFunctionWithArg(int idx, object value);
  10. }
  11.  
  12.  
  13. // ***  Player.cs ***
  14.  
  15.  // Attribute Not used in this example but
  16. // it could simplify the code auto-gen later
  17. [NetworkAware]
  18. public partial class Player : Entity
  19. {
  20.     [NetworkVar(0)] // This field is mapped to index 0 - will be explained later
  21.     public float Health;
  22.  
  23.     // =========
  24.     // Shared code example
  25.     // =========
  26.     public void DoDamage(float ouchie)
  27.     {
  28.         if (Network.AreWeServer)
  29.             Server_DoDamage(ouchie);
  30.         else
  31.             Network.InvokeCommand(networkID, 0, ouchie);
  32.         /*
  33.             ServerCommand takes 3 arguments:
  34.                 - networkID tells us which instance of Player type we need
  35.                 - 0 is index of cached server method (as indicated by attribute parameter)
  36.                 - ouchie - (perhaps an object[]) data to pass as arguments
  37.         */
  38.     }
  39.  
  40.     // ========
  41.     // Server side code - this never runs on Client!
  42.     // ========
  43.     [ServerCommand(0)]
  44.     private void Server_DoDamage(object ouchie)
  45.     {
  46.         if (!Network.AreWeServer)
  47.             throw new WTF_Exception("You have no power here!");
  48.  
  49.         //Then, perform task and replicate new stuff to everybody else
  50.         Health -= ouchie;
  51.  
  52.         Network.ReplicateVar(networkID, 0, Health);
  53.  
  54.         /*
  55.             Similar to Server Command, this takes 3 arguments:
  56.              - networkID - again, instance identified
  57.              - 0 - field slot to change
  58.              - Health - value to assign to the slot
  59.         */
  60.     }
  61. }
  62.  
  63.  
  64. /*
  65.     After the above code has compiled, we would ideally generate the following part
  66.     of a partial class. This is autogenerated boilerplate to keep as much
  67.     networking crap as possible out of the main Player.cs for readability.
  68.  
  69.     This is also as seperate file because if auto-gen ever craps it's pants,
  70.     which it frequently does, we can simply nuke the folder with all auto-gen
  71.     files in it, so the solution properly compiles.
  72.  
  73.     This should not be too difficult to generate due to our attributes, and
  74.     powerful magic of reflection. Slow, but powerful.
  75. */
  76.  
  77. // Player_NETWORK.cs
  78. // Autogenerated!!
  79. public partial class Player
  80. {
  81.     // This is invoked by client-side Network!
  82.     public override void ReplicateVarFromServer(int idx, object value)
  83.     {
  84.         if (idx == 0)
  85.             Health = (float)value;     
  86.     }
  87.  
  88.     // This is invoked by server-side Network!
  89.     public override void InvokeFunctionWithArg(int idx, object value)
  90.     {
  91.         if (idx == 0)
  92.             Server_DoDamage(value);
  93.     }
  94. }
Add Comment
Please, Sign In to add comment