Component based vs interface/abstract class base hierarchy?

Hi, I'm new to game programming and I come from a more traditional programming background. As such, I am used to more traditional methods of OOP, mostly using interfaces and inheritance. From what I have done so far, it seems like interfaces aren't first class citizens in Unity land.

I'm just wondering what kind of workflow everyone uses to make modular game components, and what the advantages/disadvantages are of each of the above methods of doing so. Thanks a lot!

Posted Apr 28, 2015, 2:45 PM UTC

66
kreaolProficient•

Anytime you start a new engine, code language, etc, you should rely on your own experience and perform a "basic run".

For a game engine, a basic run is to familiarize yourself with the core benefits of that engine (as you have already done), and then just jump in and make the three "core" games: Pong, Breakout/Arkanoid, Mario-Style Platformer.

I really wouldn't bother worrying about what "workflow" everyone else uses: You'll just get arguments akin to whether or not opening curly brackets should be on a new line or not ^^[they ^^shouldn't, ^^snarf]. Rather, start with those three games. There is a Pong tutorial in the sidebar, and if your code is up to par you should be able to do Breakout/Mario without the need for a full blown tutorial.

Pong teaches:

  • Basic Engine layout and interactions between said engine and code
  • Collisions
  • Physics
  • Basic Engine UI
  • Simple enemy "AI"

Breakout/Arkanoid is mainly for level progression and heavier component usage, and a platformer really exposes the scope of making games to fledgling game devs.

3
Harabeck•

That's a really complex and context dependent question. The project I'm working on has a lot of components of course, but it also has interfaces and inheritance. You just need to learn about Unity's component system and treat it as another tool in your arsenal to be used alongside what you have from your OOP background. Check the links in the sidebar for some tutorials to get started, if you haven't already.

2
apentlanderOP•

Haha yeah I know there's no real "right" answer for this, it's just that all the tutorials I've seen have been for beginners without programming experience. As a result, the way things are done aren't really conducive to a game with good modularity.

Do you think you could give a simple example of when you would choose an interface over a component?

1
Harabeck•

Well, I'm not sure I see it as interfaces versus components, they aren't interchangeable in my mind. Interfaces are for when another script will need to talk to other scripts, but different things may happen "under the hood" for each class that implements the interface. Components are best used when various objects will be doing the exact same kind of behavior.

Examples:

When to create a new component versus inheritance is pretty much the same as any other project. Does it save you from code reuse? In my current project, I have Towers and Creeps that both inherit from Pawn. Pawn handles damage and status effects, while Creep has stuff for moving that Tower doesn't need, and Tower has stuff for animating its turret.

An example of an interface I used in a previous project was for my AI. It needed to evaluate the threat of both the player and other AI (which may or not be friendly to it). As part of this, I wanted to the AI to be able to peek at the target list of the threat. So, I had both the player and AI implement an ITargeter interface. Typically, you use triggers to see what is near a moving object, and that gives you the collider of what entered the trigger, and that has a reference to the gameObject. But, now you need the component that has the information you want. As of Unity 5, you can use GetComponent<>() and pass in an interface to the generic parameter and get any component that implements it (note that in case of inheritance, you can pass in a parent class, of course). So my AI's OnTrigger can use collider.gameObject.GetComponent<ITargeter>() to see if a relevant component exists, and then work with it.

A component example: Going back to my current project, I put some code in Pawn so that Turrets and Creeps would set their sprites' order in layer based partially on the Y component of their transform.position. Our perspective is pseudo three-quarters, so anything above another object on the Y should be drawn as behind it. Later, I decided that my towers that spawn friendly creeps needed a rally point flag, but that this shouldn't interact with things as Pawns do. They don't collide, attack, or have health. Also, this particular bit of functionality only really needs to talk to the sprites, and isn't dependent on other things going on in the game. So, I took that code out of Pawn and put it in a separate component so that I could put it on both the rally flags and the Pawns. With only inheritance, my options would have been code duplication or adding layers to the inheritance tree to make room for something even simpler than a Pawn.

Let me know if you have any more questions.

2
solMachina•

Yes. State machines.

1
DarkArctic•

I've been reading this book and it shed a lot of light for me. You can read it for free online, but I ended up buying it I liked it so much.

1