Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // **** Entity.cs ***
- public abstract class Entity
- {
- public int networkID;
- // This is important for network communication - see overrides in Player.cs and
- // usage in Network.cs
- abstract void ReplicateVarFromServer(int idx, object value);
- abstract void InvokeFunctionWithArg(int idx, object value);
- }
- // *** Player.cs ***
- // Attribute Not used in this example but
- // it could simplify the code auto-gen later
- [NetworkAware]
- public partial class Player : Entity
- {
- [NetworkVar(0)] // This field is mapped to index 0 - will be explained later
- public float Health;
- // =========
- // Shared code example
- // =========
- public void DoDamage(float ouchie)
- {
- if (Network.AreWeServer)
- Server_DoDamage(ouchie);
- else
- Network.InvokeCommand(networkID, 0, ouchie);
- /*
- ServerCommand takes 3 arguments:
- - networkID tells us which instance of Player type we need
- - 0 is index of cached server method (as indicated by attribute parameter)
- - ouchie - (perhaps an object[]) data to pass as arguments
- */
- }
- // ========
- // Server side code - this never runs on Client!
- // ========
- [ServerCommand(0)]
- private void Server_DoDamage(object ouchie)
- {
- if (!Network.AreWeServer)
- throw new WTF_Exception("You have no power here!");
- //Then, perform task and replicate new stuff to everybody else
- Health -= ouchie;
- Network.ReplicateVar(networkID, 0, Health);
- /*
- Similar to Server Command, this takes 3 arguments:
- - networkID - again, instance identified
- - 0 - field slot to change
- - Health - value to assign to the slot
- */
- }
- }
- /*
- After the above code has compiled, we would ideally generate the following part
- of a partial class. This is autogenerated boilerplate to keep as much
- networking crap as possible out of the main Player.cs for readability.
- This is also as seperate file because if auto-gen ever craps it's pants,
- which it frequently does, we can simply nuke the folder with all auto-gen
- files in it, so the solution properly compiles.
- This should not be too difficult to generate due to our attributes, and
- powerful magic of reflection. Slow, but powerful.
- */
- // Player_NETWORK.cs
- // Autogenerated!!
- public partial class Player
- {
- // This is invoked by client-side Network!
- public override void ReplicateVarFromServer(int idx, object value)
- {
- if (idx == 0)
- Health = (float)value;
- }
- // This is invoked by server-side Network!
- public override void InvokeFunctionWithArg(int idx, object value)
- {
- if (idx == 0)
- Server_DoDamage(value);
- }
- }
Add Comment
Please, Sign In to add comment