THE HANDY VANDAL'S ALMANAC
Game Design Resources for Half-Life
Home : Topics : Menu : Reviews
Almanac 2 : Xen Rebels

Site no longer actively maintained ... HV

"Too Clever For Your Own Good"
 

USING CLASSES IN C++ PROGRAMMING
See also: Coding - Mods - Programming - Monsters
Updated March 25, 2002 - 12:42 PM

 

Classes: Glossary of Terms

A class is a template from which you create objects. The class combines data and functions into a single entity.

An object is an instance of a class -- like a variable, but more complex.

Data members are variables that are part of a class.

Methods are functions that are built into a class (and which are therefore built into all objects created from that class).

Inheritance is the process of deriving a class (the derived class) from another class (the base class). All data and methods from the base class are available to the derived class. The derived class can override data and methods inherited from the base class. The derived class can also introduce data and methods not found in the base class.

Encapsulation is the principle of combining data and functions into a single unified entity, the object. An object may have any number of data members and functions, but this complexity is encapsulated within an object, for ease of use.

C++ is a complex language ... the above glossary is merely an introduction.

 

 

Classes in the Half-Life SDK -

The SDK is made up of numerous classes. Here's a quick introduction to the CBasePlayer class:

Open up HL.DSW (server code) in Visual C++ (VCPP), and find the code for the CBasePlayer class definition, which looks like this:

class CBasePlayer : public CBaseMonster
{
...

Examine the various elements of the above code:

class = we are defining a new class.

CBasePlayer = the name of the class (the "C" prefix is a widely-used convention, but not required)

: public CBaseMonster = derived from class CBaseMonster

{ ... } = everything between curly braces belongs to CBasePlayer.

Except for top-level classes, all classes are derived from other classes. Let's trace the inheritance of the CBasePlayer class.

In VCPP, right-click on CBaseMonster, and select "Go To Definition" from the pop-up menu. VCCP displays the CBaseMonster definition:

class CBaseMonster : public CBaseToggle
{
...

Notice how CBaseMonster derives from CBaseToggle ... the definition for CBaseToggle looks like this:

class CBaseToggle : public CBaseAnimating
{ ...

CBaseAnimating, in turn, derives from CBaseDelay ...

class CBaseAnimating : public CBaseDelay
{ ...

Similarly, CBaseDelay derives from CBaseEntity

class CBaseDelay : public CBaseEntity
{ ...

CBaseEntity is the top-level class, from which all other entity classes are derived. It starts like this:

class CBaseEntity
{ ...

In this hierarchal class scheme, the top-level class contains the simplest, most general definitions. Each of the derived classes adds another layer of complexity.

When hacking the SDK, you can leverage existing code by deriving new classes from existing classes. Don't re-invent the wheel ... borrow a copy of the wheel, and make it better!

 
Using Classes in C++
Handy Vandal's Almanac Logo

"Handy Vandal" and "Handy Vandal's Almanac" copyright 2008 by Karl Gregory Jones
Almanac: Half-Life : Half-Life 2
Xen Rebels
karljones.com
Contact the Handy Vandal

Half-Life © 1998-99 Sierra On-line and Valve L.L.C. All rights reserved. Half-Life and the Half-Life logo are trademarks of Sierra On-Line. Valve and the Valve logo are trademarks of Valve L.L.C. Half-Life images, textures, music, sound effects, and other graphic or audio content © 1998-99 Valve L.L.C. All rights reserved.