Okay, so I've run into another weird problem that I'm not sure how to solve. So as part of the text adventure I've been writing to help me learn C++, I built a function that displays the player's inventory and lets them select things from it and embedded it in a larger function that shows their stats, location, and character portrait. Those functions are as follows:
Inventory function:
Code:
void inventory (int healingpotion, int lightningbottle, int lockpick, int halfhealingpotion, int inventorychoice, int healthstat, int spikedhorseshoes)
{
cout << "You are carrying: (Select an item to use it, or type \"9\" or an integer not tied to an inventory object to exit.) \n \n";
if (healingpotion == 1)
{
cout << "1. Healing Potion (Sets health to 8!) \n";
}
if (halfhealingpotion == 1)
{
cout << "2. Half Healing Potion (Sets health to 5!) \n";
}
if (lightningbottle ==1)
{
cout << "3. Lightning Bottle \n";
}
if (lockpick == 1)
{
cout << "4. Lock Pick \n";
}
if (spikedhorseshoes == 1)
{
cout << "5. Spiked Horse Shoes\n";
}
cout << "Selection? ";
cin >> inventorychoice;
if (inventorychoice == 1 && healingpotion ==1)
{
healthstat = 8;
cout << "You feel a strong surge of energy and a distinctly itchy feeling as your wounds knit themselves together. You feel energized, you feel great! You look at your biggest wound, the one you're REALLY worried about... You sigh. Well, at least you're a healthy cripple.\n\n";
healingpotion = 0;
cout << healthstat;
cout << healingpotion;
cout <<halfhealingpotion;
}
if (inventorychoice == 2 && halfhealingpotion ==1)
{
healthstat = 5;
cout << "You feel a weak surge run through you and a distincly itchy feeling as your wounds start to knit themselves together. Start being the operative word. You still feel like garbage, but you think you can move now.\n\n";
halfhealingpotion = 0;
cout << healthstat;
cout << healingpotion;
cout <<halfhealingpotion;
}
}
The function it's embedded in:
Code:
void breathedeep (int weapon, int healthstat, int spiritstat, int strengthstat, int dexteritystat, int playerrace,int healingpotion, int lightningbottle, int lockpick, int inventorychoice, int halfhealingpotion, int north, int east, int up, int room, int spikedhorseshoes)
{
cout << "You take a deep breath, close your eyes and look inward. You have a brief moment of deep awareness. \n\n Health: "<< healthstat << "\n Spirit:" << spiritstat << "\n Strength: " << strengthstat << "\n Dexterity: " << dexteritystat << "\n\n";
if (playerrace == 1)
{
cout << "UNICORN\n.................~()~().../\.....\n..................(~0000/ /............\n..............(~0/ _ \............\n.............(~0/ Q \__.....\n............(~0/ _D\........\n...........(~0/ ___/.........\n..........(~0/ \__/...................\n____________/ /.........................\n |..........................\n |...........................\n |..........................\n |..........................\n |.........................\n \.......................\n \.......................\n________| |_____ \.......................\n........| |......\ \......................\n........| |.......\ \ .........................\n\n";
}
else if (playerrace == 2)
{
cout << "PEGASUS\n..................~()~()......\n..................(~000000............\n..............(~0/ _ \............\n.............(~0/ Q \__.....\n............(~0/ _D\........\n...........(~0/ ___/.........\n..........(~0/ \__/...................\n____________/ /.........................\n|||||||||>> |..........................\n|||||||||>> |...........................\n|||||||||> |..........................\n||||||||| |..........................\n||||||||| |.........................\n||||||||| \.......................\n||||||||| \.......................\n||||||||| |_____ \.......................\n--------| |......\ \......................\n........| |.......\ \ .........................\n\n";
}
else if (playerrace == 3)
{
cout << "EARTH PONY\n..................~()~()......\n..................(~000000............\n..............(~0/ _ \............\n.............(~0/ Q \__.....\n............(~0/ _D\........\n...........(~0/ ___/.........\n..........(~0/ \__/...................\n____________/ /.........................\n |..........................\n |...........................\n |..........................\n |..........................\n |.........................\n \.......................\n \.......................\n | |_____ \.......................\n--------| |......\ \......................\n........| |.......\ \ .........................\n\n";
}
//I'll need to fix the ascii art above, it's a great idea, but I have to figure out how to get the spacing right and make the \ character show up.
if (weapon == 0)
{
cout << "You're completely unshod, and unarmed.\n\n";
}
if (location( north, east, up, room) == 5)
{
cout << "You are in some sort of clinic. It is tidy and neat, but doesn't appear to have much in the way of medical supplies. Everything is worn down, but obvious efforts have been made to maintain what little is there. A mare in an orange vest resides here, looking watchful. There is also a heavy metal cabinet tucked away in a corner. It has a very sturdy chain lock.\n\n";
}
inventory (healingpotion, lightningbottle, lockpick, halfhealingpotion, inventorychoice, healthstat, spikedhorseshoes);
}
(Yes, I know the ascii art is silly.)
I found out that in a certain part of my text adventure it was possible for a player to progress and be pronounced healed even when they hadn't drunk a healing potion from their inventory and just opened their inventory and exited instead. To fix this, I built a loop:
Code:
while (healingpotion ==1 || halfhealingpotion ==1)
{
cout<< "\n 1. Breathe Deep. \n 2. Do the exercise, but keep an eye open to make sure she doesn't pull anything.\n";
cin >> tutorialchoicefive;
while (tutorialchoicefive > 2 || tutorialchoicefive < 1)
{
cout << "Choose an option: ";
cin >> tutorialchoicefive;
}
switch (tutorialchoicefive)
{
case 1:
breathedeep (weapon, healthstat, spiritstat, strengthstat, dexteritystat, playerrace, healingpotion, lightningbottle, lockpick, inventorychoice, halfhealingpotion, north, east, up, room, spikedhorseshoes);
break;
case 2:
breathedeep (weapon, healthstat, spiritstat, strengthstat, dexteritystat, playerrace, healingpotion, lightningbottle, lockpick, inventorychoice, halfhealingpotion, north, east, up, room, spikedhorseshoes);
cout << "Your cracked eyelid lets you catch a glimpse of the mare sitting next to you... who does absolutely nothing. She sticks her tounge out when she sees you peeking.\n\n";
break;
}
cout << healthstat;
cout << healingpotion;
cout <<halfhealingpotion;
}
You may notice the cout statements at the end of the inventory function and the loop. I put them there to test a theory I had and sure enough, they print out a sequence of 800210. Basically if the player drinks the potion I want the sequence to be 800, and the function modifies the variables to those values at least within itself.
However, outside of the function the variables remain unmodified, causing my loop to keep repeating itself and a whole host of problems down the road in my program as the player sits on a healing potion and is horribly wounded as opposed to patching themselves up.
If I could make it so that the variable defining statements in the function affected the variables more globally (most importantly, in main()) then things would be awesome, but I'm just not sure how to do that.
So, once again, I seek advice from more experienced programmers, is there anything I can do to make this work? I would be sorely grateful for any help I could get.