I don't know if this is Technically the right place to ask this but I need help designing a function, I need a Rubber Duck if you would.

I'm working on an Enemy class for my C++ RPG and I am in need of some suggestions.

The Base of my design as far as the Battle system and Character Objects and Enemies and such is that all have an ID. A simple int as it were that I associate with Character Names via enum ie.

enum class Character {PLAYER1 = 0, PLAYER2 = 1};

and enemies like so.

enum class Enemy {WOLF = 0, GHOST = 1, LICH = 2};

I have a random number generator and a map to my enemy enum in my mob class that does two things.

  1. selects an amount. // Self explanatory how many enemies there are.
  2. Selects a random ID from the map. // The type of Monster you'll face.

My Question is, how would I design a function that will construct an enemy based on it's ID?


1 Answer 1


I suggest using the Type Object pattern, as described in Game Programming Patterns by Robert Nystrom:

Define a type object class and a typed object class. Each type object instance represents a different logical type. Each typed object stores a reference to the type object that describes its type.

Move traits common to all monsters of the same type into an instance of a breed class (like max hp or name). When creating a new monster, randomly select a breed from a collection, and then give the newly created enemy a reference to that breed.

This way, you only need to define the properties of each breed at the beginning of the game, and they could be loaded from a file instead of defined programmatically. This will minimize the amount of duplicate code you have to write for each type, reducing the likelihood of introducing errors and making it easier to read.

In addition, now you can swap the monsters type at any time, like from living into undead or into a sheep with a spell of polymorph.

  • \$\begingroup\$ So essentially make several classes defining types or breeds then, make files defining specific monsters in the breed type am I understanding that correctly?? \$\endgroup\$ Commented Aug 23, 2017 at 4:05
  • \$\begingroup\$ You only need to make one class, Breed. Breed should have a couple of properties like number of legs, name, movement speed etc. Then create instances of Breed for each type of monster and fill in those properties with values unique to that breed of monster. A spider is a breed with 8 legs, a wolf has 4 and a goblin has 2. Breed shouldn't be specified programmatically with a hierarchy of classes, but by the data each instance contains. \$\endgroup\$
    – Paraxon
    Commented Aug 23, 2017 at 18:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .