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
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?
refactor reasons:
- I didn’t want the game checking the variable every frame.
- 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
Cheers,
David