I've been making a text adventure game in C# (to learn more about strings and such). I have about 3 random scenarios that are triggered after the 10 basic ones where you start. One of them is the monkey temple. The "menu" class has two arguments, MainArgs and takenDump. In the scenarios you can choose to take a dump, and will say "there's a poop next to you" if takenDump is true (bool). MainArgs is used to go back to the same scenario if you use an unrecognized command.
public void ConScen1()
{
string MainArgs;
bool takenDump; // Available commands are: "hit monkey talk to monkey suicide go inside"
string conchoice1;
Console.WriteLine("You arrive at a temple dedicated to the monkey god.");
Console.WriteLine("Outside is a monkey, presumably guarding the place.");
Console.WriteLine("What do you do?");
Console.Write(">");
conchoice1 = Console.ReadLine();
if (conchoice1.Contains("hit monkey"))
{
Console.WriteLine("You hit the monkey. He draws a knife.");
Console.WriteLine("He stabs you in the eye. You bleed to death.");
Console.WriteLine(" -- Game Over --");
Console.WriteLine("Press any key to start over..");
Console.ReadKey();
takenDump = false;
MainArgs = "null";
TextAdventure1.AdventureTime.Menu(MainArgs, takenDump);
}
}
The problem here is the "string MainArgs;" line. I need it to call Menu() with "null" to start over. However, the studio says that it is unused (even though it's used in the if statement). Is there any way to disable the warning or fix the issue? If I remove the line, it gives me an error about how MainArgs isn't declared (in the if statement).
The problem here is the "string MainArgs;" line. I need it to call Menu() with "null" to start over.
No, you really don't. You don't need takenDump
either. You can just change the call to:
TextAdventure1.AdventureTime.Menu("null", false);
It's pretty odd to need to pass in a string with value "null"
though...
I'd also strongly advise you to declare variables at the point of first use, so declare conchoice1
here:
string conchoice1 = Console.ReadLine();
(I'd rename it, too... along with your methods. You have odd names all over the place. Naming is important and hard.)
Note that your program is also pretty strange in the way it "restarts" - it does so by calling into itself again (I'm assuming that Menu
is some top level method which can eventually end up calling into ConScen1
. Think about what your execution stack will look like after the user has failed several times... you don't want to recurse this way. It's unclear what your program control flow is like in general, but you should be changing the state of the game and noting that higher up, rather than this approach.
See more on this question at Stackoverflow