Hello,
I'm working on a C# game in a console application. I tried to move my "commands" that you can use into separate class files, and now they produce a StackOverflow error. Rough.
I have 4 Files: Program.cs (Handles the Game loop and method input and processing), BasicCommands(Holds 5 Methods for commands), SpeechCommands(Same as Basic except specifically for speech strings) and Character (character constructor/object for the player character and NPCs).
namespace DigitalLove
{
public class Program
{
public List<string> validWords = new List<string> { "look",
"move",
"check status",
"hallway",
"dance room",
"bathroom",
"punch bowl",
"talk",
"the scrawny boy",
"the tall boy",
"the girl",
"take",
"cup of punch",
"the blond boy",
"Rock",
"Blues",
"Forte",
"Roll"
};
public List<string> TalkingResponse = new List<string>
{
"uh...hi.",
"of course not.",
"yes",
"no",
"just here to have a good time.",
"looking for people",
};
public string[] Locations = { "Dance Room", "Bathroom", "Hallway", "Punch Bowl" };
public string Location,
response,
PlayerName,
HeldItem;
public Character Rockman;
public Character Blues;
public Character Forte;
public Character Roll;
public Character Player;
public bool HoldingItem;
SpeechCommands Command = new SpeechCommands();
BasicCommands Basic = new BasicCommands();
static void Main(string[] args)
{
Program Game = new Program();
Game.Game();
}
public void Game()
{
Console.WriteLine("Press enter to begin!");
Console.ReadKey();
Console.Clear();
Console.WriteLine("What is your name?");
PlayerName = Console.ReadLine();
while (PlayerName == "")
{
Console.WriteLine("Please enter a name!");
PlayerName = Console.ReadLine();
}
Console.WriteLine("Thank you, " + PlayerName + ". Good luck!");
Console.WriteLine("Press enter to continue. . .");
Console.ReadKey();
Console.Clear();
Location = Locations[0];
Rockman = new Character();
Blues = new Character();
Forte = new Character();
Roll = new Character();
Player = new Character();
Player.CharacterName = PlayerName;
Player.Location = Location;
Rockman.CharacterName = "Rock";
Rockman.Location = Locations[0];
Rockman.MetTrigger = false;
Blues.CharacterName = "Blues";
Blues.Location = Locations[0];
Blues.MetTrigger = false;
Forte.CharacterName = "Forte";
Forte.Location = Locations[1];
Forte.MetTrigger = false;
Roll.CharacterName = "Roll";
Roll.Location = Locations[3];
Roll.MetTrigger = false;
Console.WriteLine("You stand near the edge of the dance, shying away from everyone else who seems to be having a very good time." + "\n" +
"You would love to join, but no one has approached you yet! You feel awkward trying to approach anyone, despite the numerous amount of people in the same position.");
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine("Command List: Look, Move, Check Status");
Console.ResetColor();
GetInput();
}
public string GetInput()
{
var Test = true;
while (Test)
{
response = Console.ReadLine().ToLower();
if (validWords.Contains(response))
{
Test = false;
ProcessInput(response);
}
else
{
Console.WriteLine("I'm sorry, I do not understand.");
}
}
return response;
}
public void ProcessInput(string response)
{
switch (response)
{
case "look":
Basic.LookCommand();
break;
case "move":
Basic.MoveCommand();
break;
case "Move":
Basic.MoveCommand();
break;
case "check status":
Basic.CheckStatus();
break;
case "hallway":
Basic.MoveCommand();
break;
case "dance room":
Basic.MoveCommand();
break;
case "bathroom":
Basic.MoveCommand();
break;
case "punch bowl":
Basic.MoveCommand();
break;
case "talk":
Basic.TalkCommand();
break;
case "the scrawny boy":
Basic.TalkCommand();
break;
case "the tall boy":
Basic.TalkCommand();
break;
case "the girl":
Basic.TalkCommand();
break;
case "take":
Basic.TakeCommand();
break;
case "cup of punch":
Basic.TakeCommand();
break;
case "the blond boy":
Basic.TalkCommand();
break;
}
}
public string GetTalkInput()
{
var Test = true;
while (Test)
{
response = Console.ReadLine().ToLower();
if (TalkingResponse.Contains(response))
{
Test = false;
ProcessTalkInput(response);
}
else
{
Console.WriteLine("I'm sorry, I do not understand.");
}
}
return response;
}
public void ProcessTalkInput(string response)
{
switch (response)
{
case "uh...hi.":
Basic.TalkCommand();
break;
case "of course not.":
Basic.TalkCommand();
break;
case "yes":
Basic.TalkCommand();
break;
case "no":
Basic.TalkCommand();
break;
}
}
}
}
Where is the exception happening? Why is it happening? I don't know much about the stack overflow exception. The call stack in Visual Studio only says "external code".
What I have tried:
I've tried cutting back on how many times I call the methods in the other responses.