Advertisement
Guest User

Example Module Class

a guest
Feb 21st, 2020
320
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.04 KB | None | 0 0
  1.     [Serializable] // MUST have this attribute.
  2.     public class ExampleModule : Module
  3.     {
  4.         #region MEMBERS
  5.  
  6.         // Add all your serializable fields.
  7.         public string Text;
  8.  
  9.         /* public string Text { get; set; }
  10.          * This auto-property would work too. Auto-properties are just wrappers for fields.
  11.         */
  12.  
  13.         // This field will not be sent across the network because it has the [NonSerialized] attribute.
  14.         // This is useful for fields that define local behavior and should not be sent as data.
  15.         [field: NonSerialized]
  16.         public int NotSentAcrossNet;
  17.  
  18.         /* public readonly int ReadOnlyField;
  19.          * public int ReadOnlyAutoProperty { get; }
  20.          *
  21.          * These readonly members would be ignored, because NetEasy can't set them.
  22.         */
  23.  
  24.         /* [field: NonSerialized]
  25.          * public Player CannotSendAcrossNet;
  26.          *
  27.          * This field MUST not be sent across the network because the Player type does not have the SerializableAttribute.
  28.          * Visual Studio will even warn you if it's not marked as NonSerialized.
  29.          * As such, you must mark it with the NonSerialized attribute.
  30.         */
  31.  
  32.         #endregion
  33.         #region MODULE METHODS
  34.  
  35.         // (Required)
  36.         // Handle the behavior for this module.
  37.         // This code is run for everyone who receives the net message (and, optionally, the person who sent the message).
  38.         protected override void Receive()
  39.         {
  40.             // This will only print nonzero if received locally from the person who sent it.
  41.             // That's because it has the NonSerialized attribute.
  42.             Main.NewText(NotSentAcrossNet);
  43.  
  44.             //
  45.             Main.NewText(Text);
  46.         }
  47.  
  48.         // (Optional)
  49.         // Lets you foolproof the module and do stuff before it's sent.
  50.         protected override bool PreSend(Node? ignoreClient, Node? toClient)
  51.         {
  52.             return base.PreSend(ignoreClient, toClient);
  53.         }
  54.  
  55.         #endregion
  56.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement