• Advertisement

VladimirMarenus

Member
  • Content count

    8
  • Joined

  • Last visited

Community Reputation

107 Neutral

About VladimirMarenus

  • Rank
    Newbie
  1. Attempting a "Simple" Vector-Based Inventory Test

    [quote name='Khatharr' timestamp='1352890119' post='5000857'] 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. [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img] 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. [/quote] 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...
  2. Attempting a "Simple" Vector-Based Inventory Test

    [quote name='Khatharr' timestamp='1352888631' post='5000848'] What do you mean by turning it into a class? [/quote] 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 [img]http://public.gamedev.net//public/style_emoticons/default/rolleyes.gif[/img] ). 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.
  3. Attempting a "Simple" Vector-Based Inventory Test

    [quote name='Stroppy Katamari' timestamp='1352854817' post='5000730'] 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. [/quote] 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!
  4. Attempting a "Simple" Vector-Based Inventory Test

    [quote name='KnolanCross' timestamp='1352828361' post='5000622'] ...The main advantage of a vector over a list is that you can directly accesss an item... take advantage of it =D [/quote] Oh, my God. That made it so much easier. I ended up with: [CODE] 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; } else { 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; } else { cout << "Invalid Item type" << endl; } } break; case 2: cout << "Why do you have to be so difficult? Pick 1!" << endl; break; [/CODE] 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!
  5. Attempting a "Simple" Vector-Based Inventory Test

    I think I see what you mean about the ItemType- going to give that a whirl! I'll let you know how it goes.
  6. Attempting a "Simple" Vector-Based Inventory Test

    [quote name='KnolanCross' timestamp='1352821074' post='5000600'] How is the aItem struct/class implemented? [/quote] The implementation is mostly to teach myself vectors. At the end of it all, I want a simple program that will add an item to a vector, show a list of items, allow the player to pick one, execute the correct code for the item selected (not simply by looking at the choice, as these items are consumable- choice 3 won't always be a healing potion, for example), and then delete the item after it is used. I am attempting a larger project, but didn’t want my difficulties with this bit of code muddling up with difficulties from the rest of the code! So I have this running as a self-contained solution. Once I’m comfortable with managing vectors, I’ll design a couple more “simulations”, and then hold my breath and pray as I turn it into actual game code and merge it with my tiny project! I realize that I'm probably making a mountain range out of a molehill... but I'm trying to learn as I go!
  7. Attempting a "Simple" Vector-Based Inventory Test

    [quote name='Álvaro' timestamp='1352813298' post='5000561'] `selection' is set to 0 at the beginning of the code, and then you have a `switch(selection)'. That doesn't seem right... `selection' can only possibly 0 at that point. [/quote] Sorry, missed a line! see edit- "cin >> selection;"
  8. I am having an issue getting a vector-based inventory system to work. I am able to list the items in the inventory, but not able to allow a user-selected item to be accessed. Here is the code: [CODE] struct aItem { string itemName; int damage; bool operator==(aItem other) { if (itemName == other.itemName) return true; else return false; } }; int main() { int selection = 0; aItem healingPotion; healingPotion.itemName = "Healing Potion"; healingPotion.damage= 6; aItem fireballPotion; fireballPotion.itemName = "Potion of Fiery Balls"; fireballPotion.damage = -2; aItem testPotion; testPotion.itemName = "I R NOT HERE"; testPotion.damage = 9001; int choice = 0; vector<aItem> inventory; inventory.push_back(healingPotion); inventory.push_back(healingPotion); inventory.push_back(healingPotion); inventory.push_back(fireballPotion); 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; switch (selection) { case 1: cout << "Which item would you like to use?" << endl; int a = 1; 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; [/CODE] ^^^^ Everything above this line, works. I assume that my problem is the if statement, but I cannot figure out where I am going wrong in my syntax, or if there is a better way to do what I am doing. [CODE] if (find(inventory.begin(), inventory.at(choice), healingPotion.itemName) != inventory.end()) cout << "You used a healing potion!"; else cout << "FIERY BALLS OF JOY!"; break; case 2: cout << "Such a jerk, you are." << endl; break; } [/CODE] I need for the player's choice to affect the message displayed. Here's a sample output of the 1st snippet: [CODE] Item 1: Healing Potion Item 2: Healing Potion Item 3: Healing Potion Item 4: Potion of Fiery Balls MAKE YOUR CHOICE. Choice: [/CODE] From there, the player can type 1-4, and what I would like is for the number (minus 1, to reflect the vector starting at zero) to be passed to the find, which would then determine (in this small example) if the item at inventory[choice - 1] is a healing potion. If so, display "You used a healing potion!" and if it is not, to display "Fiery balls of joy". Obviously, to turn this into a "real" inventory system, it'll need much more tweaking- this excercise is to help me learn how to search vectors... next will be removing the item once it is used (really looking forward to that, too... ) Any advice you can offer would be awesome. Thanks for slogging through all this!
  • Advertisement