Jump to content
  • Advertisement
Sign in to follow this  
Timmygyu

newbie-need help printing function-class name to screen

This topic is 4979 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

Hi, I am deveploping a text based rpg-puzzle game, sorta like zork. I am writing a function that lists the players current items, this is obviously a case where I need to test every item to see if its in the players possesion. I have my function evaluate what items are currently possesed by the player by testing all item to see if their amount is more than 0. I need to tell it to print the item name to the screen if its amount is more than 0. The only way I can think of is to make a long list of if...else statement, which would be about 3 pages long. Is there a keyword, function, procedurer, or alternitive way I can acomplish this? Also, is this an incredibly stupid question? Thanks, Tim BTW I'm programing in C++, not C

Share this post


Link to post
Share on other sites
Advertisement
Could you post your code? I don't see why you would need 3 pages of if's unless you didn't use arrays which is just sillyness.

Share this post


Link to post
Share on other sites
If you're using C++, create an Item class and a Player class. In the Item class, have a std::string called ItemName. In the Player class have a std::list called Inventory. Create functions in this class to add items, remove items, get items, etc. You did say you're using C++ so there's not much of a reason to do it through nested if statements/reference counting.

Share this post


Link to post
Share on other sites
Quote:
Original post by Timmygyu
What part of the code would you want me to post?


Your item class/struct, were the items are defined and were you are attempting to print them.


#include <fstream>
#include <cstdlib>
#include <string>
#include <vector>

using namespace std;

class ItemData{
private:
string Name;
unsigned long OtherData;
public:
string GetName(){return Name;}
void SetName(string ItemName){Name=ItemName;}
ItemData::ItemData(){Name="";OtherData=0;}
ItemData::ItemData(string ItemName){Name=ItemName;OtherData=0;}
};
vector<ItemData> ItemVtr;

int main(){
ItemData TmpItemData("Sword of 1337");
ItemVtr.push_back(TmpItemData);
for(long i=0;i<ItemVtr.size();i++){cout << ItemVtr.GetName() << "\n";}
char c;cin >> c;
return 0;}



Something like that? I really can't help without any code.

EDIT: Sorry about my bad coding style [wink]

[Edited by - Scet on March 2, 2005 2:00:12 PM]

Share this post


Link to post
Share on other sites
If it were me, I'd make each item a structure, and give them each a unique ID. The structure can contain info such as the item name, weight, and anything else you want. You can then have an array of those structures, and use the unique ID as an index. Then you just need to store a list of integers for the player, where each integer is the item index.

Assuming you're using the STL for string and vector (since you said you're using C++, but you can use char*'s and your own list type if you like), an example:

struct Item
{
string strName;
int nWeight;
// Any other attributes you want
};

// You could use this, or a std::pair<int,int>. This is for clarity
struct InventoryItem
{
int nItemID;
int nQuantity;
};

const Item[] g_theItems =
{
{"Sword", 10},
{"Shield", 15},
{"Hamburger", 1}
}

// The player then has a list of items like this:
vector<InventoryItem> m_theInventory;

// And then, to print all your items:
cout << "You have" << m_theInventory.size() << " items in your inventory:" << endl;
for(size_t i=0; i<m_theInventory.size(); ++i)
{
cout << g_theItems[m_theInventory.nItemID].strName << ": " << m_theInventory.nQuantity << endl;
}



Or something like that anyway.

EDIT: Beaten by Scet

Share this post


Link to post
Share on other sites
Hi,

I'm gonna try out some of what you suggested. Thanks for the answers and i'll try to get on tomorrow.

Thanks,
Tim

Share this post


Link to post
Share on other sites
Well, for those of us who are lazy (including me), you can use the typeid operator. This returns the name of the class directly, so that could be a potentially downfall also.
Exmaple:
class Timmy;
...
Timmy tinyTim;
cout << typeid(tinyTim).name(); //Would print "class Timmy"
...

You could try and reformatting the name by cutting out the first 6 characters, but if you want better results, you might want to try the array ideas mentioned above.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!