GameDevHQ Crash Course #33 Abstract Classes

Jason Dixon
2 min readApr 18, 2021

--

When designing objects, you will occasionally see a lot of overlap in functionality, you can repeat the same methods and functions in all of the objects, or your could make a few abstract classes and then only have to do the functionality once. Or you have some functionality you want to make sure certain objects have for example you want all of enemies to have a health amount, attack class and speed variable, these can be all required by abstract classes.

To begin with an abstract class, we open up our script and between PUBLIC and CLASS we want to add abstract this turns the class into one that can be inherited by other scripts.

I also want the enemies to have an attack be standard through all the enemy classes so we make that a virtual method, we also want to force each enemy to have a update method so make update a abstract class.

We also have a bunch of variable we want to use that can only be modified by the base class and it’s inherited classes for that instead of public or private, we name those protected. private will make it so only the enemybase class can use it.

With this we have made a standardised class that all our enemies will inherit, it will save use some extra time, especially once we fill out the basic attack method and if we want to implement anything else that all the enemies are going to have identical to each other.

--

--