So, I’m following along and have attached my code below. It works fine, and I’m getting the hang of the terminal hacker ideas and concepts, but I am a little confused by the use of variables with functions as parameters.
For instance:
void RunMainMenu(string input)
This confuses me a bit and I just need a different explanation of what is going on here. The way I see it, we refactored by creating this function “RunMainMenu” and having “(string input)” states that the function will run and specifically check the string “input” and pass that value along… is that correct?
Any and all help is appreciated!
Code is below
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Hacker : MonoBehaviour
{
// Game state
int level;
string levelName;
enum Screen {MainMenu, Password, Screen}
Screen currentScreen = Screen.MainMenu;
// Start is called before the first frame update
void Start()
{
ShowMainMenu();
}
void ShowMainMenu()
{
Terminal.ClearScreen();
Terminal.WriteLine("Press one of the following number keys to hack:");
Terminal.WriteLine("1 - Baby Mode");
Terminal.WriteLine("2 - Man Mode");
Terminal.WriteLine("3 - Sage Mode");
}
// this only decides how to handle user input, not actually do it
void OnUserInput(string input)
{
if (input == "menu") // we can always go straight to the main menu
{
ShowMainMenu();
currentScreen = Screen.MainMenu;
}
else if (currentScreen == Screen.MainMenu)
{
RunMainMenu(input);
}
}
void RunMainMenu(string input)
{
if (input == "1")
{
level = 1;
levelName = ", \"easy mode\"";
StartGame();
}
else if (input == "2")
{
level = 2;
levelName = ", \"med mode\"";
StartGame();
}
else
{
Terminal.WriteLine("Please choose a valid option");
currentScreen = Screen.MainMenu;
}
}
void StartGame()
{
Terminal.WriteLine("You have selected level "+ level + levelName);
currentScreen = Screen.Password;
}
// Update is called once per frame
void Update()
{
}
}