I was getting really frustrated at first. I couldn’t figure out why I couldn’t just use void StartGame()
to retrieve input. Then somewhere in the Q and A (I don’t remember where sadly) someone said that in modern coding, you should have one function do ONE thing. At first I didn’t understand that quote very much but when I decided to see what Ben did in the video he used OnUserInput
to handle the input on the password screen. Then it all made sense. StartGame()
was used to control GAMESTATE rather than input and OnUserInput
was used to handle INPUT. I was trying to use StartGame()
to handle gamestate and user input. After I realized I could kinda skip StartGame and go back to OnUserInput, I was able to finish my code by myself and it was relieving. It made total sense and it’s nice and neat. I wouldn’t say my coding wasn’t super nice and neat but it’s still cleaner that what I was trying before. Here’s my silly code anyway.
public class Hacker : MonoBehaviour
{
//GameState
int level;
enum Screen { MainMenu, Password, Win };
Screen currentScreen;
// Start is called before the first frame update
void Start()
{
ShowMainMenu("Hello User");
}
void ShowMainMenu(string greeting)
{
currentScreen = Screen.MainMenu;
Terminal.ClearScreen();
Terminal.WriteLine(greeting);
Terminal.WriteLine("What would you like to hack into?");
Terminal.WriteLine("1: Jim's Minecraft Account");
Terminal.WriteLine("2: Taco Bell Recipe Database");
Terminal.WriteLine("3: Disney");
Terminal.WriteLine("Enter the corresponding number to make selection:");
}
void OnUserInput(string input)
{
if (input == "Menu")
{
ShowMainMenu("Hello User");
}
else if (currentScreen == Screen.MainMenu)
{
RunMainMenu(input);
}
else if (currentScreen == Screen.Password)
{
CheckPassword(input);
}
}
void RunMainMenu(string input)
{
if (input == "1")
{
level = 1;
StartGame();
}
else if (input == "2")
{
level = 2;
StartGame();
}
else if (input == "3")
{
level = 3;
StartGame();
}
else
{
Terminal.WriteLine("Choose a valid level.");
Terminal.WriteLine("Enter 'Menu' to return to the Menu.");
}
}
void StartGame()
{
currentScreen = Screen.Password;
Terminal.ClearScreen();
ShowLevel();
}
void CheckPassword(string input)
{
if (level == 1)
{
if (input == "Block")
{
currentScreen = Screen.Win;
Terminal.ClearScreen();
Terminal.WriteLine("Congrats. You may now play Minecraft.");
Terminal.WriteLine("Enter 'Menu' to return to the Menu.");
}
else
{
Terminal.WriteLine("Incorrect password. Type 'Menu' or");
Terminal.WriteLine("reenter your password:");
}
}
else if (level == 2)
{
if (input == "Livemas")
{
currentScreen = Screen.Win;
Terminal.ClearScreen();
Terminal.WriteLine("Congrats.");
Terminal.WriteLine("You may now access Taco Bell Recipes.");
Terminal.WriteLine("Enter 'Menu' to return to the Menu.");
}
else
{
Terminal.WriteLine("Incorrect password. Type 'Menu' or");
Terminal.WriteLine("reenter your password:");
}
}
else if (level == 3)
{
if (input == "Starwars")
{
currentScreen = Screen.Win;
Terminal.ClearScreen();
Terminal.WriteLine("Congrats. You may now hack Disney >:)");
Terminal.WriteLine("Enter 'Menu' to return to the Menu.");
}
else
{
Terminal.WriteLine("Incorrect password. Type 'Menu' or");
Terminal.WriteLine("reenter your password:");
}
}
}
void ShowLevel()
{
if (level == 1)
{
Terminal.WriteLine("You have chosen Jim's Minecraft");
Terminal.WriteLine("Account.");
Terminal.WriteLine("Please enter your password:");
}
else if (level == 2)
{
Terminal.WriteLine("You have chosen Taco Bell Recipe");
Terminal.WriteLine("Database.");
Terminal.WriteLine("Please enter your password:");
}
else if (level == 3)
{
Terminal.WriteLine("You have chosen Disney.");
Terminal.WriteLine("Please enter your password:");
}
}
// Update is called once per frame
void Update()
{
}
}