Attempting to Refine Game Entry

Hi All,

I have a question I am struggling with. I am sure the solution is very simple, but I am not seeing it. Basically, I would like to set up Terminal Hacker to ask for a User Name, and then pass that user name to the rest of the game so that it always shows. I’ve set up a Void with the information request, but I can’t seem to store the input in my declared string, nor get the game to move onto the next menu. My code is as follows:

void Start()
    {
        CaptureUserName(greeting);
    }

    void CaptureUserName(string greeting)
    {
        CurrentScreen = Screen.Name;
        Terminal.WriteLine("Hello! Welcome to the Dashboard.");
        Terminal.WriteLine("Please enter your name:");        
    }

    //void Update()
    //{
    //    int index = Random.Range(0, level1Passwords.Length);
    //    print(index);
    //}

    void ShowMainMenu()
    {
        CurrentScreen = Screen.MainMenu;
        Terminal.ClearScreen();
        Terminal.WriteLine("Hello, " + greeting);
        Terminal.WriteLine("Welcome to the Dark Web");
        Terminal.WriteLine("Choose which system to bring down!");
        Terminal.WriteLine("1 - " + LocalElementarySchool);
        Terminal.WriteLine("2 - " + YourCityHall);
        Terminal.WriteLine("3 - " + TheFederalReserve);
        Terminal.WriteLine("Enter the line number you want to hack");
    }

Any help would be greatly appreciated.

This would be a great opportunity to learn about enums. Define various states for your game in an enum. Here is an example:

enum State
{
    StartGame,
    EnterUsername,
    EnterPassword
}

State state = State.StartGame;


// in a method method

switch (state)
{
    case State.EnterUserName:
        // what is supposed to happen?
        break;
    case State.EnterPassword:
        // what is supposed to happen?
        break;
    default:
        // Whatever your default state is supposed to be
}

Hopefully, this helps. :slight_smile:

If not, please keep watching the videos and try to realise your idea at the end of this section.

Thank you. I will play with this and report back!

you may want to consider something along these lines.

string greeting = "";

void Start()
{
        greeting = CaptureUserName();
}

string CaptureUserName()
{
    // Your code here
}

the important part here is that in order to store the user’s name, your CaptureUserName() must return something. as you have it you’re passing in an empty string called “greeting” and and not saving the user’s input to it.

I’m sorry. I think I’m still struggling a little here. I followed your suggestion and built the following code:

void Start()
    {
        greeting = CaptureUserName();
    }

    string CaptureUserName()
    {
        Terminal.WriteLine("Please enter your Username: ");
           
    }

I think where I am struggling though is in trying to capture what the user is entering in as a value, and then pushing that value into the ShowMainMenu method. I’m not sure how to tie the two together.

I tackled it slightly differently than what you recommended, but I think I came up with a solution:

void Start()
    {
        CreateUserName(UserName);
    }

    void CreateUserName(string input)
    {
        CurrentScreen = Screen.UserName;
        Terminal.WriteLine("Welcome to the Terminal.");
        UserName = input;
        UserNameCapture(input);
    }

    void UserNameCapture(string input)
    {
        if (UserName == "")
        {
            Terminal.WriteLine("Please enter your Username: ");
        }
        else if (UserName == input)
        {
            ShowMainMenu(UserName);
        }
        else
        {
            Debug.LogError("Error capturing username");
        }
    }

By using the if else statement here, it forces the user to enter the user name, and once the user name is captured, feeds it to the Main Menu and displays the user name there.

Well done! :slight_smile:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms