- Home /
Interface vs Base Class [C# Code Architecture Problem]
I'm trying to create a simple object selection system for my game. I want each object that should be able to be selected in-game to inherit from the basic selection behavior/functionality.
The first thing that comes to mind is a class or a interface.
So my second thought is, I know for a fact that the selectable objects will always have a boolean value which holds the status of the selectable, wether it's selected or not. I also know that it will have two overridable functions: OnSelected and OnDeselected. I know that both those functions will have a basic functionality; the ability to toggle the boolean value on or off.
Basically, all selectable objects in my game would always have this functionality in them:
class/interface Selectable {
bool selected;
OnSelected()
{
selected = true;
]
OnDeselected()
{
selected = false;
}
}
With that in mind I decided a class would be best; it can be inherited and can have base functionality, so that I don't have to rewrite the basic functionality for every single selectable object in the game, because that would be a pain in the a**, especially if I would like to add more functionality to the selectables.
After a while of coding I come to the realization that I will need most, but not all of the selectable objects to also be MonoBehaviors. Only one problem, a class can only inherit from a single class in C#, unlike C++ it doesn't support multiple-inheritance. Since not all selectables need to have MonoBehavior functionality I also can't just inherit that in the Selectable class either.
So now I'm thinking of using a interface instead, however I quickly rememeber that interface can only define functionality, not implement it. Eg I could define a function called OnSelected, but I would be unable to implement it in the interface so that all the inherited objects will be guaranteed to have at least that functionality implemented. This means that I would have to rewrite that functionality (which I know that all of the selectable objects will have) for every single selectable in the game.
And now I'm here, wondering if there's any other good way of solving this problem without having to rewrite a bunch of functionality that I know should always be there.
Sorry for the massive amounts of text, didn't even realize I wrote this much... lol
Answer by Hellium · Dec 05, 2017 at 10:43 PM
Unity is a component oriented game engine, and you should make the most of this. You should not worry about having multiple components on a same GameObject.
This is why I suggest you having an independant component with events so that your other components can react when your object gets selected / deselected.
using UnityEngine;
using UnityEngine.Events;
public class Selectable : MonoBehaviour
{
[SerializeField]
private UnityEvent onSelected;
[SerializeField]
private UnityEvent onDeselected;
public bool Selected
{
get ; private set ;
}
public virtual void OnSelected()
{
Selected = true;
if( onSelected != null )
onSelected.Invoke();
}
public virtual void OnDeselected()
{
Selected = false;
if( onDeselected != null )
onDeselected.Invoke();
}
}
Oh of course, I always seem to fail at this component thing. Ins$$anonymous$$d of using UnityEvents, do you think using Send$$anonymous$$essage would work since the functions will always be on the same GameObject just in a different component? Can't test at the moment because I'm getting a weird stackoverflow error when trying to use Send$$anonymous$$essage and I feel like a UnityEvent isn't needed in my case. :P
Edit: Stack overflow was caused by using the same function name in Send$$anonymous$$essage like the function Send$$anonymous$$essage was called from. Everything seems to be working. Thanks for the help :)
I advise against using Send$$anonymous$$essage
. While this is very convenient in your case, refactoring and debugging it is a big pain, because it relies on the function name (string). Because of this, you don't know who called the function using your IDE (With the "Find references" feature), and rena$$anonymous$$g the function name will require to change the string inside the Send$$anonymous$$essage
function. But it's up to you ! ;)
Ins$$anonymous$$d, while this is less convenient, I would use the events or a new Interface so that the Selectable
component will call GetComponent<ISelectionTarget>().OnSelected()
for instance.
Oh I see. Ended up using a GetComponents version of the interface solution since I want to be able to call several ISelectionTargets on a single GameObject:
ISelectionTarget[] targets = GetComponents<ISelectionTarget>();
foreach (ISelectionTarget target in targets)
{
target.OnSelected();
}
Again, thank you for the help :)
Your answer
Follow this Question
Related Questions
An OS design issue: File types associated with their appropriate programs 1 Answer
Is there an elegant way to having single-player class methods within a multiplayer class? 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Is it ok to use nested classes, to cache and access components? 1 Answer