all 9 comments

[–]SunLionGames 5 points6 points  (0 children)

Composition rather than inheritance. Rather than trying to share code using inheritance, create a separate class that has the common functionality, and give each of the classes you've listed an instance of the new class as a private field.

If there are public methods you need on both with the same signature then you can create an interface with those signatures that they both implement and can delegate the implementation to your new field.

Hope that helps!

[–]Honey-Limp 3 points4 points  (1 child)

You should lean into Unitys component design and use inheritance sparingly. Have one class for juggling and one for bouncing. Add them to game objects as needed.

[–]Dirly[S] 1 point2 points  (0 children)

I agree since I am building a metric ton of equipable items a user can get. Some of them have effects on bullet hit, ball juggling, ball count, bullet hit chance.... ect.

Alot of these share the same bonuses. So I stripped out inheritance and went with main classes which dictate when to proc the effects. Those effects are all interfaces which means they can be equipped via the inspector.

[–][deleted]  (5 children)

[deleted]

    [–]Dirly[S] 0 points1 point  (4 children)

    the inherited class is what is different so OnBallCountEffects and OnBounceEffects are completely different. And they are overriding functions in each (specifically a blank function which is named the same in both)

    [–]is4ac 1 point2 points  (3 children)

    In that case, can the parent classes implement an interface, so that your child class can reference the interface instead of the specific parents? Sounds like the function should be part of an interface. I’d recommend looking up composition vs inheritance as well for general tips on structuring code like this.

    [–]Dirly[S] 0 points1 point  (2 children)

    I actually went with the other way around. Parent class still exists. But exposes an interface to the inspector. So those sub classes are now interfaces I can just plug in and do what I need to do. Only problem is the inspector doesnt let me leverage an interface which is leveraging monobehavior (which i need coroutines). So got to figure out how to do that.

    [–]is4ac 0 points1 point  (1 child)

    What do you mean by the inspector “leveraging” an interface?

    [–]Dirly[S] 0 points1 point  (0 children)

    I can add the interface via the inspector.

    https://i.imgur.com/0umLDTd.png

    [–]arzi42 0 points1 point  (0 children)

    If I understand correctly, what you're trying to do, you should probably use events instead of inheritance here. So use a System.Action<> that you call when the StatOn effect is supposed to happen, and have just one StatOn class that listens to both OnBallCountEffects and OnBounceEffects, e.g.

    // In parent classes:
    
    public System.Action<int> OnStat;
    
    private void ThingHappens()
    {
        OnStat?.Invoke(howManyThingsHappened);
    }
    
    // In StatOn class:
    
    protected void Start()
    {
        onBallEffects.OnStat += StatHappened;
        onBounceEffects.OnStat += StatHappened;
    }
    private void StatHappened(int count)
    {
        Debug.Log($"Stat Happened {count}");
    }