String instead of Int for level tracking?

public class Hacker : MonoBehaviour {

    string level; //game state

	void Start ()
    {
        ShowMainMenu ();
    }
    void ShowMainMenu ()
    {
        Terminal.ClearScreen();
        Terminal.WriteLine("Welcome...");
        Terminal.WriteLine("There isn't much time,");
        Terminal.WriteLine("you must hack these");
        Terminal.WriteLine("secure systems to learn");
        Terminal.WriteLine("more about the Truth.");
        Terminal.WriteLine("Hack the NYPD");
        Terminal.WriteLine("Hack the FBI");
        Terminal.WriteLine("Hack the NSA");
        Terminal.WriteLine("Make your choice.");
    }

    void OnUserInput(string input)
    {
        if (input == "menu")
        {
            ShowMainMenu ();
        }
        else if (input == "NYPD")
        {
            level = "NYPD";
            StartGame();
        }
        else if (input == "FBI")
        {
            level = "FBI";
            StartGame();
        }
        else if (input == "NSA") 
        {
            level = "NSA";
            StartGame();
        }
        else if (input == "exit")
        {
            Terminal.WriteLine("There is no exit, without Truth");
        }
        else
        {
            Terminal.WriteLine("Response Invalid");
        }
    }
    void StartGame()
    {
        Terminal.WriteLine("Entering {0} mainframe." level);
    }
}

Hey all, after watching ‘member variables to hold state’ I’ve tried to use words with string for tracking the level rather than int single digits as it fits the theme of my game better, i had got it to work, but when i went back to change something else in the ‘enumerating our game states’ tutorial its coming up with errors under WRITELINE and LEVEL on the final line. What have i missed or messed up?

Hi Jon,

You haven’t concatenated the string and the variable.

Instead of;

Terminal.WriteLine("Entering {0} mainframe." level);

…you need the following;

Terminal.WriteLine("Entering {0} mainframe." + level);

On a related note, you may want to consider have case-insensitive tests for user input also, as at the moment the user will need to enter the text exactly as you are testing for it.

This can be achieved by using either String.ToUpper or String.ToLower, as follows;

if (input.ToLower() == "menu")
else if (input.ToLower() == "nypd")

On a final note, when you copy/paste code into your posts, if you add the code formatting characters before and after it, the forum software will render your code in a much better way and it will be more readable for others.

Hope this helps :slight_smile:


See also;

Hey Rob, thank you for replying, your comments on String.ToUpper and String.ToLower are interesting! I didn’t know you could do that!

With the concatenation though, entering;

Terminal.WriteLine("Entering {0} mainframe." + level);

simply returns;

Entering {0} mainframe.FBI

How do you get the level to replace the {0}?

Also you say “You haven’t concatenated the string and the variable.” but i have the level down as a string so am i not concatenating two strings? Still not entirely familiar with the names of everything.

Thanks!

1 Like

HI Jon,

You are more than welcome.

I completely missed the {0} in your string, sorry, I assumed you were trying to concatenate like this;

Terminal.WriteLine("Hello " + "World");

…what you are actually trying to do here is known as composite formatting.

So, to answer your question, as Terminal.WriteLine requires a string to output, so we would need to build one, either within or prior to the WriteLine method call.

Examples;

Terminal.WriteLine(string.Format("Entering {0} mainframe.", level));

or

string message = string.Format("Entering {0} mainframe.", level);

Terminal.WriteLine(message);

Hope this helps :slight_smile:


See also;

Hey Rob,

Thanks for your help, I’ve managed to get both examples to work and have learnt something new!
Cheers!

1 Like

Hi Jon,

You are more than welcome, happy to help :slight_smile:

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

Privacy & Terms