Equip Item (attach one class-instance to another)

Hi there

I am currently working on my first mildly complex test-program. It is a RPG-Combat program where I want to test some programming techniques to later implement in a proper text based RPG. So far I have been making good progress and managed to solve my problems by finding solutions online or in my books. However, I have now run into an obstacle that seems to be a little more complex, and finding a solution proofed difficult. So here I am.

I have three classes: One for the player, one for the NPC opponent, and one for the weapons the player and NPC will use. Currently, the program is capable of generating a player character based on user input and safe it, generate a NPC, either based on user input or completely random, and generating weapons either completely random, based on user input or based on weighted parameters (which in turn are based on class and skills of the corresponding character/NPC).

The problem I now have is this:

Once I generated a weapon, how do I tell the program to equip it to either the player or the NPC? Basically what I want to do is this:

1. Generate player character or NPC (working)
2. Generate a Weapon (working)
3. Equip the weapon to the player character or NPC (no solution yet)

The weapon class is quite complex in that each generated weapon is unique. There is a great number of different weapon types, each with different attack and damage types, different action point cost, etc.. Also each weapon has a quality and condition value that can range from 0 to 100 and has an impact on the weapons performance.

So I can't just tell the program what kind of weapon is equipped. It needs to know exactly which instance of the weapon class is equipped and needs access to all its values during combat.

To that end, the program is generating an unique identifier number for each weapon (stored as an integer value within the instance). However, I have not been able to figure out how to progress from there.

So, yeah, that's about it. My first real problem. Any tips anyone can give me to solve this will be highly appreciated.

If you need further information or code-snippets, please let me know.
A common approach is to have an abstract base class Weapon and and player and NPC have an instance of weapon - probably a std::unique_ptr. Once a weapon is created you pass it to player or NPC. A simple snippet:
1
2
3
4
5
6
7
8
9
10
11
12
13
class Weapon
{
public:
    ~Weapon();
};

class Player
{
public:
  void set_weapon(std::unique_ptr<Weapon> weapon);
private:
  std::unique_ptr<Weapon> weapon;
};


If you have learned about templates this would be an other option.
1
2
3
4
5
6
7
8
9
template<class Weapon>
class Player
{
public:
  void set_weapon(std::unique_ptr<Weapon> weapon);
private:
  std::unique_ptr<Weapon> weapon;
};
Here Weapon is a concept, everything that behaves like a weapon is a weapon.


I'm not sure the template option would be appropriate for the sort iof game the OP is writing. It would mean that the player's weapon would have to be known and fixed in the code, at the point where the Player object was first instantiated. It would, for example, disallow any situation where the player changed the weapon they were using.
how you manage it depends.
does your character have an inventory? Or just equipped items? Eg can he have 10 weapons and 1 equipped?

A pointer may be all you need.
you have the weapon.
NPC class has a slot for a weapon (make this a weapon *) and assign the weapon you made to the pointer and its connected. This is a simple solution and has drawbacks, but it also works with little fuss if it is all you needed.
Last edited on
Thanks for your replies so far. I will have a closer look at the suggested approaches. I have played around with templates a bit while working through one of my C++ books, but didn't really do much with them since. I'll go back to that chapter and have another look.

To clarify what I am doing:

Weapons will be generated throughout the final game. For example, if the player searches a house he may find a weapon or when the player enters a new area a NPC with a weapon may be generated. So weapons in the game are not fixed, predefined or existent when the game starts. Any weapon will be created at runtime and may also be destroyed again later.

A short breakdown of the system (the full code is really long, like 900 lines for the constructor alone, so I will not post it):

When a weapon is created, the constructor of the weapon class is called. However, the type of weapon to create is handed over to the constructor as a parameter (integer value), either through user input or as a return value from a separate function. That function may generate a completely random weapon or a weapon based on NPC skills or other parameters. Below a few examples of the constructor being called with different parameter sources:

1
2
3
4
5
6
7
8
C_Weapon *pWeapon = new C_Weapon(WeaponSelection);
// generates a new weapon based on user input

C_Weapon *pWeapon = new C_Weapon(GenerateRandomWeapon());
// generates a completely random weapon

C_Weapon *pWeapon = new C_Weapon(Enemy.ReturnWeapon());
// generates a random weapon appropriate to the corresponding NPC's skills 


The constructor has a list of all weapon types and selects the one to be initialized by looking at the handed over parameter. If it finds the weapon, it assigns an unique ID number to it and initializes it with some fixed and some random values. If it does not find the weapon, it returns a failed state.

@ jonnin
The player character (and the NPC) will eventually have an inventory and equipment slots. But that is something I will deal with in a later iteration of the program. For now my focus lies on testing the combat system, including equipping and handling of weapons. So really this is about the basic problem of equipping just the one item. I will then expand the program from there once I get this working.
ok. Just be sure that what you do NOW can be adapted to the future design...

since you already are generating pointers, storage of weapon* in the char or inventory etc may still be one way.
Last edited on
If you need further information or code-snippets, please let me know.

What member data do your classes contain? This is probably enough to get a hint about what's going on.

I'd actually prefer to see the whole program. Since it's fairly lengthy, perhaps you can provide a download link rather than pasting several thousand lines into the forum.

900 lines for the constructor alone
This is a red flag.
Well, I'm really fairly new to programming. So while of course my goal is to write code that I can then use again in later iterations, I am also sure that some things I do now are either completely wrong or at least very inefficient. I already threw out some stuff I did after realizing that there was a better way of doing it. That's the process of learning ;-)
The reason why I'm writing the program I am working on right now is to try out some of the stuff I already learned, find solutions to the problems I encounter and also just get some routine working with code. I am prepared to fail and go back every now and then.

Now to your questions:

My classes contain the usual stuff. Mainly a lot of private variables, and a few functions to do some basic stuff.

Here is an upload of the program.

http://www.mediafire.com/folder/jul8iyg4mf3a2/RPG_Test_Combat

Just be aware that it is very much a work in progress with a lot of unfinished stuff, placeholders and certainly a bunch of errors. It also contains a lot of stuff that the current program does not need (like variables for food, drink, sleep, etc.) in preparation for later iterations. Also the way weapons are initialized in the main program is really just a placeholder (the weapons are destroyed again right after initialization). I will implement this properly once I figured out how to equip them. I also know that I create objects in places where the rest of the program can't access them (for example inside an if-condition). This is something I still try to figure out (and I'm confident that I will).

About the 900 lines constructor:
Yeah I know it is a bit unwieldy. Problem is that each weapon has 19 different variables that need to be initialized and there are 29 different weapons. So just the list of weapons the constructor can pick from is 551 lines. I also use a lot of empty lines and comments to keep things organized and clear. I have seen code from more experienced programmers, which is usually a lot more compact.

I have already thought about shortening the whole thing. Since every weapon has the same 19 variables, I should be able to put that block into a function and then just hand over different parameter values to put into these variables based on what weapon needs to be initialized. I have already done something similar with attributes and skills. I first had a separate code-block for each of the 14 attributes and skills and later moved it to a function and now just hand over different parameter values based on what attribute or skill to initialize. That eliminated quite a bunch of code.

I actually like working like that. First do it the way I know how, then go back and see if I can find more efficient ways. Then next time I do the same or a similar thing, I can implement the more efficient solution from the start. I feel like this is a good learning experience for me.
Topic archived. No new replies allowed.