Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
So I'm attempting to create a text-based adventure game, and have finally hit the biggest problem for me personally, the inventory. Now the best way I've thought to do this (in theory) is to find a way to make a variable (Weapons, armor, etc.) that can hold multiple types (Buy/sell value, defense, attack), and have a container of these variables/objects (the inventory). Any ideas on which methods to look at for this? I know there is most definitely a way to do so but I figure other humans would help more than computers at the moment. Thanks in advance!

What I have tried:

Google, a very large tome of C++ knowledge, and geeks for geeks.

I also tried vectors, but I'm not sure how to let it accept class objects.
Posted
Updated 12-Sep-23 9:26am
v4

Google is the place to start: writing an adventure game inventory - Google Search[^] - loads of ways to think about there! Find something that fits into the rest of your game first, or think about how they are working on the whole app.
 
Share this answer
 
STL vectors can hold any basic or class based type, even other vectors. But your descriptionsuggest you need a collection of class objects. So first you need to decide which of these objects you need to keep together.

So define the character types that will be in your game, then for each one define their properties. Use inheritance where appropriate to avoid duplication. You can find some greate tutorials on C++ at Learn C++ – Skill up with our free tutorials[^].
 
Share this answer
 
A possible solution is inheritance of objects. You may create a base class like Weapon with some functions and member variables like name, power and distance. This gives you the possibility to store it in some array or container. Than derivee some special classes for the individual weapon.

But best is, when you also visit some Learn C++ tutorial to understand the language and its usage.

Tip: use class design to better separate every task and make the code more readable.

Good luck.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900