Jump to content

  • Log In with Google      Sign In   
  • Create Account


Member Since 07 Sep 2012
Offline Last Active Nov 21 2012 05:15 AM

Posts I've Made

In Topic: Attempting a "Simple" Vector-Based Inventory Test

14 November 2012 - 05:05 AM

Gaah, 2 posts while I was editing. Derp.

Ah, I see. You just mean implementing it. I'm glad to hear the you're enjoying your successes. Posted Image

Edit - Occurred to mention it while I was outside - You would probably be better off placing the usage message in the item's effect processing. That way you just have to proc the item effect of the selected item instead of switching to print the usage message.

Thank you! :)

I see... and thank you for your code example- that looks much more friendly than what I'm hacking together! Got a lot to learn, but getting there...

In Topic: Attempting a "Simple" Vector-Based Inventory Test

14 November 2012 - 04:47 AM

What do you mean by turning it into a class?

Well, this standalone exercise was standalone because I realized that I had no idea how to create an inventory system- this is all written from scratch (I'm still enough of a beginner to feel like a "Master Programmer" for getting it to work Posted Image ). I actually have a very basic text RPG with some functional abilities, and wanted to isolate this painful learning experience from the working code! The end result of this is to create a class with functions that I can call from within the game code, in order to perform inventory functions without cluttering up the main .cpp.

In Topic: Attempting a "Simple" Vector-Based Inventory Test

14 November 2012 - 03:28 AM

The code is artificial, and more importantly, it doesn't do anything (except printing stuff). Thus there's very little that could be wrong with it on low level, and in the absence of context it's hard to say anything about design issues.

Just some cosmetic things I'd point out to a student:
- "a" is useless because you could be printing "index+1" instead
- any variables like "choice" should be defined as close as possible to where you actually use them for the first time
- instead of writing "choice-1" everywhere, sanitize the number once to the value you actually want (-=1)
- finally, magic numbers like "01" and "02" are generally bad; in a real program you would likely use an enum and descriptive labels like ITEM_HEALTHPOT or ITEM_FIREPOT

Which compiler are you using? If it's new enough to have some C++11 features, they can make your life easier in small (and occasionally big) ways.

Thank you very much. I realize the artificial nature. This is a glorified snippet that is mostly designed to teach me how to use and modify vectors. The suggestions I was looking for are exactly the ones you mentioned- just good housekeeping and dev habits in general. I'm learning this myself, so I'm not able to get the benefit of a teacher standing over me going "No- that won't work in six months when you're doing something better."

I'm using Microsoft VS 2012- I'm not sure what "C++11 features" are, but if they make my life easier, I'd certainly be interested in hearing about them!

EDIT: I've applied the changes you suggested, and the code reads much better.

EDIT 2: The next step for me is to turn this into a class, I suppose!

In Topic: Attempting a "Simple" Vector-Based Inventory Test

13 November 2012 - 01:04 PM

...The main advantage of a vector over a list is that you can directly accesss an item... take advantage of it =D

Oh, my God. That made it so much easier. I ended up with:

cout << "This is a test game to use inventory items. Woo!" << endl;
cout << "You're an injured fighter in a fight- real original, I know." << endl;
cout << "1) Use an Item. 2)  ...USE AN ITEM." << endl;
cin >> selection;
int a = 1;
switch (selection)

case 1:
  cout << "Which item would you like to use?" << endl;

  for( vector<aItem>::size_type index = 0; index < inventory.size(); index++ )
   cout << "Item " << a << ": " <<  inventory[index].itemName << endl;
   a+= 1;
  cout << "MAKE YOUR CHOICE." << endl << "Choice: ";
  cin >> choice;
  //This checks for an invalid response. Add in non-int
  if ((choice - 1) >= inventory.size())
   cout << "Choice out of bounds. Stop being a dick." << endl;
   if(inventory[choice - 1].itemType == 01)
    cout << "You used a healing potion!" << endl;
   else if(inventory[choice-1].itemType == 02)
    cout << "FIERY BALLS OF JOY!" << endl;
    cout << "Invalid Item type" << endl;
case 2:
  cout << "Why do you have to be so difficult? Pick 1!" << endl;

This code compiles and runs- but is it good code, in your opinion?

My next task is to figure out how to delete the item from the vector and move everything up to fill the spot. -_- That'll come later, I am still basking in the delight of this working. Thanks!

In Topic: Attempting a "Simple" Vector-Based Inventory Test

13 November 2012 - 12:10 PM

I think I see what you mean about the ItemType- going to give that a whirl! I'll let you know how it goes.