• 11
• 27
• 9
• 20
• 31

# items for rpg

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

## Recommended Posts

hey i'm using dev c++ to make a text base rgp and i was wondering what would be the best way to store items like gold and potions using an array ect.. and also how can i use it in combat situations ?

##### Share on other sites
The optimal way would be to use a heterogeneous list of inventory:

// Main game class that handles the main game mechanics and such.class game{	/* ... */};// Game objectclass entity{protected:	game	&m_game; // This object's local instance of game (in case there are multiple instances of "game")	public:	entity( game &g ) : m_game( g ) { }	virtual ~entity() { }		virtual bool step( double dt ) = 0; // Advance this object's logic	virtual void draw( const camera &cam ) = 0; // Draw this object using "cam"};// This class contains the common properties between the player and monstersclass player_base : public entity{protected:	int hp, mp, sp; // etc...	public:	player_base( game &g, /* ... */ ) : entity( g ) { }	virtual ~player_base() { }		int &get_hp() { return hp; }	};// The player, who moves around accomplishing goalsclass player : public player_base { /* ... */ };// Rats you occasionally battle in some grass class npc_giant_rat : public player_base { /* ... */ };// This class contains the common properties between all times of itemsclass item : public entity{protected:	player_base	*m_owner; // Who's carrying this item, 0 if it's laying out in the world	public:	item( game &g, player_base *owner = 0 ) : entity( g ), m_owner( owner ) { /* ... */ }	virtual ~item() { /* ... */ }		virtual const char *get_name() const = 0; // Returns the specific name of the item	virtual const char *get_description() const = 0; // Returns the description of what this item does		// If any of these three functions return true, the 	virtual bool consume( player_base *target ) const { return false; }  // Uses the item, target specifies who to use it on	virtual bool equip( player_base *target ) const { return false; }  // Modifies the target's stats	virtual bool unequip( player_base *target ) const { return false; } // Unmodifies the changes to the target's stats		virtual bool is_equipable() const = 0; // Ask if this item can be equipped, such as a weapon	virtual bool is_consumable() const = 0; // Ask if this item can be consumed, such as a potion};// Sample item that recovers a target's hpclass item_potion : public item{protected:public:	item_potion( game &g, player_base *parent = 0 ) : item( g, parent ) { }	~item_potion() { }		const char *get_name() const { return "Potion"; }	const char *get_description() const { return "Recovers +12 HP"; }		bool consume( player_base *target )	{		if ( m_parent ) 		{			m_parent->get_hp() += 12;		}						return false; // destroy item	}		void draw( const camera &cam ) {}	bool step( const double dt ) { return false; }		bool is_equipable() { return false; }	bool is_consumable() { return true; }};

Oops, sorry got carried away; I've got to run now, sorry for any mistakes, but now you can:

std::list<item*>  m_items;// OR, if you the list to take ownership of the objects:// boost::ptr_list<item> m_items;m_items.push_back( new item_potion( /* ... */ ) );m_items.push_back( new item_sword( /* ... */ ) );

EDIT:
Cleaned up the code a bit.
And documented it.
And fixed a small pointer issue.

[Edited by - _fastcall on August 15, 2008 2:01:18 PM]

##### Share on other sites
yeah its no compiling for me i must be doing something wrong?

##### Share on other sites
Quote:
 Original post by idonoyeah its no compiling for me i must be doing something wrong?

I hope you didn't copy and pasted that code... I have no idea how your code works or its coding style, how am I supposed to know if my code will compile for your project? [smile] That's why I posted in pseudo-code; its not supposed to work -- you have to fill in the /* ... */ yourself. Sorry if I failed to mention that.

But, the general idea is there: Use inheritance so you can store a heterogeneous list -- a list containing multiple-types -- of items. Create a base class to generalize the common properties of every item. (Such as the item's name, its description, can it be equipped, and etc.)

##### Share on other sites
i was actually using it as an example, but i did not fully understand the logic of the code you wrote i spent some time today trying to understand them

##### Share on other sites
// Potion structurestruct _Potion{   string name; // Potion name   int effect; // How much HP it heals or what not   // Extra Things   /* ....... */};

##### Share on other sites
The potion structure type might work, but then you need a list of potions, a list of weapons, a list of ___. That might be what you need, although for a good, dynamic, single object type list the inheritance model would be preferred.

I don't know what else I can add that the others haven't covered (and believe me I was looking!). If you are trying to understand it, don't look at the methods right away, look at the classes and how they're inherited so you have an understanding of their layout, then look at only a few methods and how the inheritance comes into play, etc... Just break it down piece by piece starting at the highest level and drilling down.

##### Share on other sites
Hi,

_fastcall: Don't you think that your entity class in too dependent of your game framework ?

BioX : You should try out your idea if you don't understand _fastcall concept.

##### Share on other sites
Quote:
 Original post by _fastcallBut, the general idea is there: Use inheritance so you can store a heterogeneous list -- a list containing multiple-types -- of items. Create a base class to generalize the common properties of every item. (Such as the item's name, its description, can it be equipped, and etc.)

I agree completely with the general idea; listen to _fastcall.

However, in my RPG engine, my hierarchy only goes up to class item; there is no generic class entity. The completely generic hierarchy, where everything is an entity, _could_ be too generic, depending on how your system works.

For instance, in my engine, there's no need to step() or draw() a potion, which just sits in inventory until used, whereas that's certainly important for a character. If my hierarchy included a generic "entity", I would either have some useless step() code, or some useless code to call step() all over the place.

Of course, there are drawbacks; if I ever decide that a character should be able to place another character in it's inventory, I'll have some tricky special case code to write!

Anyways, I definitely agree with the concept _fastcall is putting forward, but do carefully consider your game design when setting up your class hierarchy.

Thanks,
Geoff

##### Share on other sites
Quote:
 Original post by gdunbarFor instance, in my engine, there's no need to step() or draw() a potion, which just sits in inventory until used,

I considered the case that an item might be laying out in the world, without an owner.

Quote:
 Original post by lollan_fastcall: Don't you think that your entity class in too dependent of your game framework ?

What do you mean? I don't see why it shouldn't be... If there was the existence of more than one game class (such as save states), then we wouldn't want an entity spawning its children across a different game state, would we? [smile] In my point of view, I'd rather have a local reference to the entity's creator rather than a GetCurrentGameState() function or a global game* pointer. If you have an alternative solution, I'd love to hear it.

Quote:
 Original post by idonoi did not fully understand the logic of the code you wrote i spent some time today trying to understand them

You don't have to use the code, if you don't want to / don't understand it. There's always more than one solution. I just prefer the object-oriented-programming approach.

OOP is difficult to learn at first, but once you learn it; you'll never program without it. We'll be glad to help you out -- tell us which parts you do and don't understand about the code.