# Classes in Classes?

This topic is 4878 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Yo. Say if you have a shooter game, where you have several weapons with different effects. So you have to decide how to manage this in a fairly clean way. Well, would it work to use a class that also uses another class? I do not mean a derived class. Here is an example: There's a standard 'player' class. This will include animation, interation, and what have you. Now you have a weapons class. it also controls the animations and effects of the gun. Now you have the projectile class. This gives the bullets an effect as they impact each surface. So, is it possible to manage the shooter game that way? If not, how else can this be acomplished?

##### Share on other sites
I do believe that would be inheritance. Someone else should fill you in with enough information.

// EDIT: I'm new to C++, or fairly new, so I can't answer.

##### Share on other sites
Well i have never written an FPS, but i'll throw some ideas around.

I would have a player class that holds the information about what gun etc. Then i would have a collection of these players in something like a 'game analyser' class. This would look through all the entities in the game and depending on what ever stimulai, like input, ai etc, alters the players attributes, like if the up key is pressed change the location coords. Have this collection referencable by a renderer so that it can do all the transformations and display the entities.

Typically in professional engines, the 'player', be it AI or human, would become a client to an in-game server. Then the server would 'refresh' each client, i would have thought.

Hope this helps, although im not knowledgable with FPS.

ace

##### Share on other sites
The gun should be a separate class; a gun is not a player. The player should contain a reference to one or more weapons.

##### Share on other sites
What you are describing is roughly "composition", that is objects that contain other objects as componant parts. It's not true composition, but yes, it would work for a decent model. If you compartmentalize each different object, you have more control over changes and a more flexible design overall. If you have different classes for players, weapons, projectiles (not all weapons have projectiles, now do they?), etc, you can subclass those things to create new objects in the future.

##### Share on other sites
Well, I have no idea how to actually invoke animations, or any type of graphical elements, except with the Visual Studio Template stuff. It's only because Direct-X is so freakin hard. All the tutorials move fast-pace and rarely explain line by line what the code means or how it works.

Anyways, I'll look into other methods for the battle system later tonight.

##### Share on other sites
hl1 did it they way you explained above

CBaseWeapon is derived from CBaseEntity ....
class Canyweapon: public CBaseWeapon
{
public:

CBaseEntity * m_pOwner;
}

so you have the actual entity and you have a pointer to the owner *the player that carries the weapon

in order to animate the player you tell them which animation to play

....

its actually pretty simple

each entity has a method that sets up the animations to play *interpolate between current and last frame to make the animation smooth*
you usually animate with 30 FPS

just check out the hl1 sourcecode

##### Share on other sites
Well, I would go something like the above route as did what Half-Life 1 and Half-Life 2 did (you need to have HL2 in order to get the SDK). I'm most likely going to have a basic object/entity for the game and then devrive everything from that. This is also somewhat like what the Unreal engine (Unrealscript) does; everything (usually) is devrived from a base object.

As for affects on the weapons, there is any number of methods that you could do. You could have an array of objects "IAffectors" that can be added or removed from an object. There could really be any number of ways to do something like this:
class IAffector {  public:    virtual void DoAffect(void)=0; // pure virtual  private:    int m_iFlags;};class CAffectors : public std::vector<IAffector*> { ..};class CHandgun : public CBaseWeapon {  ...  private:    CAffectorList m_Affectors;};Somefunc() {  for( list of affectors ) {    if( p->flag & whatever ) {      p->DoAffect()    }  }}

You would make each affect a class and have a specific "DoAffect" for each; also flags could be, let's say, "IS_NPC", "IS_PLAYER", "IS_RUNNING", etc. Just something I would think about. If you wanted to get a bit more in depth, you might want to look into a scripting language that you could script events (or affects, spells, etc) on objects. I'm looking into Angelscript right now, I believe you can find that somewhere on Angelcode.com.

Hope that helped.

##### Share on other sites
I think you can just use a function pointer to establish a different firing effect for a different gun. So you would only have one gun class, but each instance of that class would recieve a pointer to a function that would be called when the fun fires and one that governs how the gun is drawn. I've never tried anything like that, but I think the theory is sound. Anyone care to refute?

The simple method would just be to set your firing method as a virtual function and derive a class for each different weapon type. Although this might be a good route if you're going to have multiple actors weilding these guns. Using the above method, you would have to instantiate a gun and then point it to the function(s) that handle the gun specifics. If you use inheritence, you can just create an instance of that gun type. I think I like the inheritence idea better, now that I think about it.

1. 1
Rutin
19
2. 2
3. 3
JoeJ
16
4. 4
5. 5

• 26
• 20
• 13
• 13
• 17
• ### Forum Statistics

• Total Topics
631700
• Total Posts
3001783
×