<x:document xmlns:x="http://local/" class="gameprog" site="xenon" title="Multiple Inheritance in RPGs">
<address>
Author: <a href="/~amitp/">Amit Patel</a>
</address>
<x:section>
<pre class="simple">
From: amitp@Xenon.Stanford.EDU (<a href="/~amitp/">Amit Patel</a>)
Newsgroups: rec.games.design
Subject: Re: OOP and multi-class characters
Date: 4 Aug 1997 22:32:16 GMT
Organization: Computer Science Department, Stanford University.
      </pre>
<p>
<a href="mailto:mzjxbt@echidna.stu.cowan.edu.au">Pauldo</a> wrote:
<br/><em>
How would you implement multi-class characters in an RPG
game using object-oriented C++ ?
</em>
</p><p>
I used to think that this was something to do with multiple
inheritance.  However, I've recently changed my mind, so I'll
explain why.  (Note: this doesn't direclty involve RPGs until
the end.)
</p>
<p>
An object's class is .. a classification.  It's a way to group
similar objects together.  However, not all classifications
should be classes.  For example, there's a large set of people
on this planet.  Some of these people are currently in school,
so they can be classified as 'students'.  However, this isn't
a permanent classification -- it changes from year to year, as
new people enter school and other people graduate (or drop
out) and leave school.  If I say that Amit is a Student,
that's not a statement about the True Nature of Amit.  It's a
statement about what I'm doing with my life right now.
</p>
<p>
Now the question is: should there be a Student class?  Perhaps
a Teacher class as well .. and then Teacher and Student can
derive from Person.
</p>
<p>
Let's start with Amit being a Student, and let's assume
Student has fields like 'student id' and 'date enrolled'.
</p>
<p>
What happens if I'm a graduate student, and I decide to teach
a class?  Should Amit inherit from both Student and Teacher?
You could use multiple inheritance here.  Does the language
let you change classes?  (Some do, and some don't; C++
doesn't.)  How do you initialize the 'Teacher' part of the
existing Amit object?
</p>
<p>
What happens once the class ends, and I am no longer a
Teacher?  I have to change classes once again.  What happens
to the fields of Amit that were part of the Teacher class?  (I
suppose they disappear.)
</p>
<p>
Now suppose I take a class at a different school.  I now have
<em>two</em> student IDs, and two 'date enrolled's.  Where does this
information go?  Can I inherit from Student <em>twice</em>?
(That depends on the language; C++ lets you but doesn't let
you generalize to N students at run-time.)  How do I refer to
these fields and methods unambiguously?
</p>
<p>
Finally, I graduate and I'm no longer a student.  Should my
student records be destroyed?
</p>
<p>
After thinking about this example for a while, I decided that
Amit should not be an instance of 'Student', nor should he be
an instance of 'Engineer' or 'Killfile Member' or these other
"temporary" classifications.  Amit is-a Person.  For a while,
he may also be in some other categories like Student, but it's
not some essential part of his being.
</p>
<p>
So .. here's a different design:
</p>
<p>
Amit is-a Person.  Person has no subclasses.
</p>
<p>
Instead, there is an abstract Role class, which subclasses
Student, Teacher, etc.  Every person has-a list of Roles that
he or she is in.
</p>
<p>
When I enroll in school, I create a new Student role and add
it to my list.  When I teach a class, I create a new Teacher
role and add it to my list.  When I stop teaching, I remove
that role.  When I enroll at a different school, I create a
second Student role for my list.  When I graduate, I can empty
my list.
</p>
<p>
This design for students and teachers gives you more
flexibility than the original one, but it may be a little
harder to use.  With the second design you can have many roles
at once, and you can enter or leave a role without changing
your object's class.
</p>
<p>
For an RPG, I would use a similar role system.  A Player
wouldn't have subclasses Magician, Thief, Ninja, Knight, and
so on.  Instead, each player would have a list of roles (or
perhaps only one role).  When you start playing, you might not
have a character class.  Then you can walk to the Thieve's
Guild and sign up; that would add a Thief role to your list
(or set your current role to a Thief role).  Later, you may
want to become a magician, so you'd add a new role.  Or maybe
you suffer from amnesia, and you lose all your Thief
abilities.  You may have some kind of system where you can
switch roles but you can't play several at the same time; in
that case, you can detach the role objects and attach
different ones.
</p>
<p>
Earlier I said that Amit isn't fundamentally a Student.  He's
currently a student but that may not always be the case.
However, he's fundamentally a Person.  You may want to
consider player races to be fundamental.  Then a Player would
have subclasses Dwarf, Human, Elf, Orc, etc., and each player
could also fill one (or more) roles.
</p>
<p>
This design has a few advantages:
</p>
<ol><li> It works in C++ (i.e., it works without fancy language
features like changing an object's class, having repeated
superclasses, or using multiple inheritance)
</li><li>  It allows easy addition and removal of roles.
</li><li> It allows playing a role multiple times.  (For example,
you may be a Thief in two different guilds.)
</li></ol><p>
It suffers from:
</p>
<ol><li> In some ways it's more complicated than the
inheritance-based design.  For example, methods on the Player
class may have to be forwarded to one or more roles.
</li><li> It seems unintuitive at first (to many people).  There's
a book called <em>Mathsemantics</em> that deals with some of
these issues about an object being fundamentally one thing
vs. an object playing a role (called an 'event' in that book).
[The author seems to dwell on 'passengers' on airplanes not
being the same as 'persons', since you can be a 'passenger'
many times but a 'person' just one.  Anyway, although it had
nothing to do with OO design, I learned a <em>lot</em> of
design by reading it.]
</li></ol><p>
This probably fits into the general inheritance
vs. composition design decision.  Inheritance tends to be
easier to program, but composition tends to be more flexible.
</p>
<p>Okay, I'm done ranting.  <code>:-)</code></p>
<address>
- <a href="/~amitp/">Amit</a>
</address>
<hr/><p>
<em>Postscript added 3 July 1998, updated 13 February
2000</em>: After a while I discovered that other people have
thought about this too.  One of the online references you may
want to read is <a href="http://www2.awl.com/cseng/titles/0-201-89542-0/roles2-1.html">a
paper about roles and design patterns</a>; another is <a href="http://ootips.org/role-object.html">a web page about
roles and design patterns</a>.
</p>
</x:section>
</x:document>
