Archived

This topic is now archived and is closed to further replies.

Game Design - Tracking Items/Equipment/Data

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello Everyone! I''m still teaching myself C++ and learning to design games. I have a question on how to track items/weapons/armor/equipment... I''m planning on using OOP, and I''ll have classes setup something like what follows... This is going to be a simple DOS Console ASCII graphics game for the sole purpose of learning how it''s done. class Creature class Player : Public Creature class NPC : Public Creature class MOB : Public Creature class Item class Weapon : Public Item class Armor : Public Item class Tile (keeps track of each ASCII tile on the map) If I have Player Bob; what would be a good way to track what item Bob is using? I would have Bob.weapon = ... But how would I implement this with pointers and a list of items? or is there a better way? I was planning on keeping a 2d array (possibly a linked list after I learn them better) to track my items, weapons, armor, npc''s, monsters, etc. If I have a list like.. Item_List = {Longsword, 1d8, Med, +1tohit}, {Shortsword, 1d6, Small, 0} How would I do this? Any help you could give would be much appreciated ~~~~~~~~~~~ Chris Vogel ~~~~~~~~~~~

Share this post


Link to post
Share on other sites
To keep track of what weapon a player is using, simply create a pointer to a Weapon object. To store the players inventory, create an array of pointers to Item objects.

class Player
{
public:

void SetWeapon( Weapon* WeaponToEquip );
//...

private:

Weapon* Current_Weapon;
Armor* Current_Armor;
Item* Inventory_Items[100];

};

Remember to use a -> (not a .) when accessing an object through a pointer

Share this post


Link to post
Share on other sites
Thanks, that''s pretty much what I figured, but Pointers still don''t settle right with me. I''ve been fighting with em for 2 weeks and they are winning.

I understand what you posted perfectly, but I couldn''t of come up with it on my own and had it work for the life of me. I''ll get this eventually.

~~~~~~~~~~~
Chris Vogel
~~~~~~~~~~~

Share this post


Link to post
Share on other sites
A pointer points to the address of a value. Like int x=5; x stores the value 5 but if you had something like:

int x = 5;
int *y;
y = x; // y is NOT equal to 5, y stores the address of x, which
// has the value 5
cout << y << endl; // does not print 5, but some address
// where x is
cout << &y << endl;// the & gets the value at the address


So you can use pointers if you want to keep track of data, regardless of where or what it is, just by pointing to it. If you said
Weapon* currWeapon;
currWeapoon = longsword;
currWeapon points to longsword, you could just as easily change it to dagger, or shortsword or whatever. Let me know if you want more information.


-David

Share this post


Link to post
Share on other sites
Unless I''m misunderstanding something the above post is flawed.
the & operator is used to get the address of a variable. for example:
int x = 5;
int *y; // y is a pointer to an int. it can store the address of an int.
y = &x; // y = address_of(x) therefore y points to x.

to get the value that y points to (the number 5) we use the * operator.
for example:
cout << "y points to the value" << *y;
if we were to use:
cout << y;
we would get the address of x.

Pointers store the address in memory of another variable.
They are used so you don''t need to pass an entire variable around, instead you just pass the pointer of it around, which may be mucb smaller. The value of the variable can then be accessed using the * operator on the pointer.

Hope that clears things up
Toby


Gobsmacked - by Toby Murray

Share this post


Link to post
Share on other sites
You''re right. My mistake, I''ve mostly been using Perl recently and my C++ has suffered for it. Thanks for clearing it up.


-David

Share this post


Link to post
Share on other sites