1

I am coding in C# in Unity3D. This is my first question on Stack Overflow. I hope to be in order.

In my game I have two empty sub-classes that work as kind of an enum extension and that are exactly the same:

public class Condition {
    public int ID { get; set; }
    public int IDAsBitMask { get { return 1 << ID; } }
    public string Name { get; set; }
    public Sprite Picture { get; set; }
}

public class PositiveCondition : Condition { }
public class NegativeCondition : Condition { }

The reason for these two empty sub-classes is, so that I can only pass members of one sub-class to a method in my HarmfulTrap class:

public class HarmfulTrap {
    int myBitMask;

    public bool HasCondition(NegativeCondition condition) {
        return (myBitMask & condition.IDAsBitMask) == condition.IDAsBitMask;
    }
}

But it seems redundant to create two sub-classes that don't declare anything. Is there a way for me to avoid this code architecture? And still be able to only pass the negative Conditions as arguments to my HarmfulTrap method? I am looking for a way that works at compiling time. No validation checks within the HasCondition method at runtime.

Thank you.

  • Knowing Unity, what you're suggesting here sounds like an over-engineered solution, to a problem that doesn't really exist. I'd personally be going back to drawing board. If you wanted to accept only items of a particular type, I'd personally be looking at adding Interfaces to your MonoBehaviours and then something like `public void HasCondition ( INegativeCondition condition )` – Milan Egon Votrubec Oct 11 '20 at 00:51
  • What do you mean with a problem that doesn't really exist? And if I use Interfaces I would still have to declare two classes that each implement a different Interface but nothing else wouldn't I? – Capricornum Oct 11 '20 at 07:51
  • It's not redundant. This solution is pretty simple, it will protect you from mixing up conditions later so my thought is: you can go ahead and use it. – obywan Oct 11 '20 at 09:01
  • That's what I was hoping for. I just wanted to make sure I am not missing something obvious. Thanks a lot obywan. – Capricornum Oct 11 '20 at 09:04
  • 1
    This is perfectly acceptable. The only thing I would add is to declare the base ```Condition``` as abstract. – Immersive Oct 12 '20 at 00:49
  • That's a great idea. Thank you. I finally understand the use of abstract. So that I cannot instantiate the base class. – Capricornum Oct 12 '20 at 20:48

1 Answers1

0

You can add a public bool like "isNegative" to your Condition class, then access that variable from your object to check if it's positive or negative.

public class Condition {
    ...
    public bool isNegative { get; set; }
}

And in your function:

public class HarmfulTrap {

   int myByteMask;

   public bool HasCondition(Condition condition) {
      if(condition.isNegative)
      {
        return (myByteMask & condition.IDAsBitMask) == condition.IDAsBitMask;
      } 
      //add else here with the default value you want to return          
   }
}
salyangoz
  • 101
  • 6
  • Thank you for the idea. I would prefer if the method "HasCondition" only allows the right arguments from the start. If indeed I use your idea I would need a way that 'condition' is checked at compile time and not once the program is running. – Capricornum Oct 10 '20 at 20:57