Issues with abstract members

Hello,

I am creating some overly complicated system and running into some issues.

Very simplified issue:

abstract class A : Monobehaviour
{
    abstract int valueA;
}

class B1 : A
{
    override int valueA = someValue;
    static int defaultValue = someValue;
}

class B2 : A
{
    override valueA = someValue;
    static int defaultValue = someValue;
}

class C
{
    Text text; //Some UI stuff
    System.Type type; //This is either B1 or B2 (or any other script derived from A)
    text.text = ((A)type).valueA; //<- Can't convert System.Type to A (its abstract)
    text.text = ((A)GetComponent(type)).valueA; //Object not yet instantiatiated - can't getComponent
    text.text = ((B1)type).defaultValue; //Can't do because I do not know if it is B1 or B2. Need to cast to A
}

My issue is as explained in comments. Anyone have any suggestions how to go around this?

I could instantiate a temporary object, get the default value and destroy the object but that will be very slow.
I could also create some script that holds all the default values I want. But then I need to remember to write the data there after I create yet another script that derives from A (and there will be a lot of them)

Idea behind the system I am making is that player gets to look at some descriptions of B1, B2… and then select which component will be instantiated based on those descriptions.

Thanks for any solutions!

You’ve identified the problem… now simplify it!

Interfaces can let you give disparate types of objects a similar … interface.

In your case you might have an interface that can:

  • return the description
  • create the thing if called

Using Interfaces in Unity3D:

Check Youtube for other tutorials about interfaces and working in Unity3D. It’s a pretty powerful combination.

It’s kinda rare but nothing in your code makes any sense ^^. It’s littered with syntax and spelling errors. I get that it supposed to represent some kind of pseudo code. However nothing you wrote makes sense.

  • Fields can not be abstract or virtual, at all. So you can’t override them. Maybe you meant to implement a property?
  • Assuming a property, the actual backing field can only be initialized once by a field initializers. You can not re-initialize a field in a derived class. The only way would be a constructor to do that.
  • The System.Type class is part of the reflection system of C# / .NET. An instance of the System.Type class contains data that describes a type but is not related to that class in the OOP sense in any way. So you can never cast a System.Type instance into a different type
  • You can not extract the value of a field initializer without creating an instance of the class. That’s just not a thing
  • You can read a static field of the class itself, but only by using reflection. So you could use reflection to get the “defaultValue” of your class.
  • You try to get an int value from the class and assign it to the text property of your UI text. This doesn’t work without a ToString

To get the value of the static defaultValue, you would need to do this:

        System.Reflection.FieldInfo field = type.GetField("defaultValue", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static);
        int val = (int)field.GetValue(null);

This would be a more “sane” setup:

public abstract class A : MonoBehaviour
{
    public abstract int valueA { get; set; }
    public static int GetDefaultForType(System.Type aType)
    {
        System.Reflection.FieldInfo field = aType.GetField("defaultValue", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static);
        return (int)field.GetValue(null);
    }
    public static int GetDefaultForType<T>() => GetDefaultForType(typeof(T));
}

public class B1 : A
{
    public override int valueA { get; set; } = defaultValue;
    static int defaultValue = 42;
}

public class B2 : A
{
    public override int valueA { get; set; } = defaultValue;
    static int defaultValue = 5;
}

You could use val = A.GetDefaultForType<B1>() to get the default value. However in your case since you’re dealing directly with a System.Type instance, you would probably do:

val = A.GetDefaultForType( type );

One issue I see right away is “System.Type” variable. On line 22 you’re trying to convert a “Type” into a “A”. This is sort of like trying to write the word “Car” on a piece of paper and then treating that piece of paper as if it’s a vehicle. System.Type describes a type, it is not that type. In this task you will probably rarely if ever need to use a System.Type variable.

Another challenge you will have is that static variables/methods cannot be inherited. This is annoying, but it’s a fact of life in C#. For this reason you will probably want to replace your static methods with a “factory pattern” - a second class which has a list of the objects you want to be able to use.

It’s either that, or you store the relevant default values outside of the class itself. Those are the two options.

You could do individual mini-factories per type. This would give you two parallel class hierarchies. (For convenience of reference you could nest the minifactory as a subclass of the regular class; for simplicity I’m not doing that below, but that’s how I’d do it in a project). So like:

public abstract class Factory<T> where T : A
{
   public abstract string GetDescription();
   public abstract T CreateDefault();
}

public class A {
    abstract int valueA { get; }
}

public class B1 : A {
   public override int valueA => 2;
}

public class B1Factory : Factory<B1> {
   public override string GetDescription() {
      return "Bee One";
   }
   public override B1 CreateDefault() {
      return new B1();
   }
}

Now in your class C, you can have a collection of these factories (and these could possibly even be automatically created by getting all derived classes programmatically, there’s some magic sauce available on google to do that). The factories are tiny and easily iterated over to present this list to the user, and when they choose one, you call that factory’s CreateDefault to make the actual object.

Thanks for the help! Reflection worked well - just a few extra lines and no need to create any extra scripts. And while I can’t force classes to implement static default value, it is very easy to tell which ones are missing it.

The code I wrote was meant to just give a general idea of what is happening. The code inside my project is obviously different. So I’ll just ignore points 1,2,3 and 6. Point 4 is my question just rephrased in a single sentence. And 5 is the answer - thanks!

Make sure to add tons of comments to this.
Because currently it’s only you and god who knows how that construct works - and in half a year it will only be god :stuck_out_tongue: (meme)

There should not be any other programmers looking at this. But I am adding comments to confusing code anyway :smile:

With regard to comments and readable code, it is vital that you consider “yourself six months from now” to be a wholly separate person. You will forget everything you’re in the middle of right now and wonder what you were thinking.

And it gets worse with age :slight_smile:

Eventually you don’t remember what you were thinking when you started writing the function five minutes ago.

“Wait… what is this float d argument for?!”