"Don't be afraid to break things" *g*

Hey all,

I just started this course (and my gamedev journey) and am having a lot of fun figuring stuff out and experimenting between each lesson. I took Ben’s advice with “don’t be afraid to break things” and attempted to have the game query the user for their name before adjusting the Main Menu with "Hello ".

This actually worked well during the initial kloogy implementation. However, when I attempted to refactor the code to clean it up a little, I am now insta-crashing unity on play button press :sweat_smile:

I could ignore it and go back to being kloogy in hopes that it’ll be explained/understood later on… but I don’t think I’ll be able to sleep at night until I figure out the why… especially since I’m actually hanging the entire editor… Can you help? :wink:

refactor reasons:

  1. I didn’t want the game checking the variable every frame.
  2. I wanted to attempt to allow this code to be reusable for other input get/sets.

Original kloogy code (which works):

public class Hacker : MonoBehaviour
{
// Start is called before the first frame update
public string gt;
public bool gotName = false;

void Start()
{
    GetNameWindow();
}

// Update is called once per frame
void Update()
{
    if(gotName == false)
    {
        foreach(char c in Input.inputString)
        {
            if (c == '\b') // has backspace/delete been pressed?
                {
                    if (gt != null && gt.Length != 0)
                    {
                        gt = gt.Substring(0, gt.Length - 1);
                    }
                }
                else if ((c == '\n') || (c == '\r')) // enter/return
                {
                    gotName = true;
                    ShowMainMenu();
                }
                else
                {
                    gt += c;
                }
        }
    }
}

// Main Menu

public void GetNameWindow(){
    Terminal.ClearScreen();
    Terminal.WriteLine("What is your name?");
}

void ShowMainMenu() {
    Terminal.ClearScreen();
    Terminal.WriteLine("Hacking Simulator v1.0");
    Terminal.WriteLine("Hello "+gt+"!");
    Terminal.WriteLine("");
    Terminal.WriteLine("1. Hack your neighbor's WIFI. \n2. Hack a streamer's Twitch account. \n3. Hack Area 51.");
    Terminal.WriteLine("");
    Terminal.WriteLine("Choose:");
}

}



Refactor Attempt (bye Unity!):

public class Hacker : MonoBehaviour
{
// Start is called before the first frame update
public string gt;
public bool gotName = false;

void Start()
{
    GetNameWindow();
    ShowMainMenu();
}

// Update is called once per frame
void Update()
{

}

// Main Menu

public void GetNameWindow(){
    Terminal.ClearScreen();
    Terminal.WriteLine("What is your name?");
    getInput(gt, gotName);
}

public void getInput(string typedIn, bool gotIt){
    do
    {
        foreach (char c in Input.inputString)
        {
            if (c == '\b') // has backspace/delete been pressed?
            {
                if (typedIn != null && typedIn.Length != 0)
                {
                    typedIn = typedIn.Substring(0, typedIn.Length - 1);
                }
            }
            else if ((c == '\n') || (c == '\r')) // enter/return
            {
                gotIt = true;
            }
            else
            {
                typedIn += c;
            }
        }
    }while(gotIt == false);
}

void ShowMainMenu() {
    Terminal.ClearScreen();
    Terminal.WriteLine("Hacking Simulator v1.0");
    Terminal.WriteLine("Hello "+gt+"!");
    Terminal.WriteLine("");
   Terminal.WriteLine("1. Hack your neighbor's WIFI. \n2. Hack a streamer's Twitch account. \n3. Hack Area 51.");
    Terminal.WriteLine("");
    Terminal.WriteLine("Choose:");
}

}

Thanks to anyone who has read through this, whether you respond or not :wink:

Cheers,

David

Hi David,

Welcome to our community! :slight_smile:

It might be that you inadvertently implemented an endless loop in the getInput method. See the do/while loop.

1 Like

Looking up unity docs on Input.inputString, it’s stated:
“Returns the keyboard input entered this frame. (Read Only)”
So you have to check on every frame Update to get all the player input, and can stop checking once the player hits Enter.
In your code you get player input only on the first frame of the script life (when Start fires), so it’s likely that the do while loop never ends (unless either \n or \r is captured gotIt remains false).
A way to get what you want should be to have a different Monobehaviour that is enabled at the start of the game, checks for input on Update and gets disabled once the players enter their name, so that its Update stops being called

1 Like

Hi Nina & albesca,

Firstly, thank you for the welcome Nina! :slight_smile: Yep, I was able to narrow it down to being the loop, I just wasn’t understanding why… but now with albesca’s additive information it all clicks now :slight_smile: Thank you two so much for taking the time to read through and respond to this, it was hurting my brain, but I’m learning! :smiley:

Thanks tons!

David

1 Like

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

Privacy & Terms