Page 4 of 4 FirstFirst ... 234
Results 31 to 33 of 33
  1. #31
    Join Date
    Nov 2006
    Location
    Vancouver, WA
    Posts
    236
    Enum, I am not sure about "N" for Null
    Code:
    namespace MMOFramework.Base
    {
        public enum ActorKind : byte
        {
            None, //Null
            Persistant, // Id set in stone, object save to database that does not reload
            Transient, //Character, NPCs something that get a new Id everytime it reloads
            Scene, //Buildings things that are constructed in Unity with a component that says it is a Scene Actor
            ServerSide //Server side
        }
    }
    
    
    namespace MMOFramework.Base
    {
        public struct ActorId : IEquatable<ActorId>
        {
            public readonly ushort Id;
            public readonly ActorKind Kind;
    
            [JsonConstructor]
            public ActorId(ushort id, ActorKind kind) : this()
            {
                Id = id;
                Kind = kind;
            }
    
            public override string ToString()
            {
                var kindText = Kind == ActorKind.Persistant ? "P" : Kind == ActorKind.Scene ? "X" : 
                               Kind == ActorKind.ServerSide ? "S" : Kind == ActorKind.Transient ? "T" : 
                               Kind == ActorKind.None ? "N" 
                return $"{kindText}{Id}";
            }
    
            public bool Equals(ActorId other)
            {
                return Id == other.Id && Kind == other.Kind;
            }
    
            public override bool Equals(object obj)
            {
                if (ReferenceEquals(null, obj)) return false;
                return obj is ActorId && Equals((ActorId)obj);
            }
    
            public override int GetHashCode()
            {
                return base.GetHashCode();//Missing code
            }
        }
    }
    Last edited by Matroblend; 05-27-2017 at 05:47 PM.
    The only way to fail is to give up or die...

  2. #32
    Join Date
    Aug 2010
    Posts
    160
    Considering that the enum only has those you could just use any non-conflicting error case character, but that is what the 'None' is. It is the default case since it is backed by byte which should be 00000000 by default. I would end it early in this case.

    Code:
    var kindText = Kind == ActorKind.Persistant ? "P" : Kind == ActorKind.Scene ? "X" : 
                               Kind == ActorKind.ServerSide ? "S" : Kind == ActorKind.Transient ? "T" : "N";

  3. #33
    Join Date
    Nov 2006
    Location
    Vancouver, WA
    Posts
    236
    Thanks again am385 for the rescue that does close the KindText var error.

    I must have been getting close as the conditional operator (? returns one of two values depending on the value of a Boolean expression. Following is the syntax for the conditional operator.

    condition ? first_expression : second_expression;

    I was having a hard time trying to figure out what the second_expression should be if I was using all the enums.

    The good thing is that I have learned something about conditional operators and Null, that may sound basic but like all things it is not until it is.

  4. #34
    Join Date
    Aug 2010
    Posts
    160
    Yeah it is just a list of cascading "Ternary Operators" they are great for saving space when you need an "if this, else that" statement. Also look into the "Null Coalescing" operator. It is 2 ?.

    All the following are equivalent.
    Code:
    object a = b ?? c
    
    object a = b != null ? b : c
    
    object a;
    if (b != null)
        a = b;
    else
        a = c;

Page 4 of 4 FirstFirst ... 234

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •