Terminal Hacker Menu Page + Some Tweak

Hi everybody! im Alfredo Ramos from Buenos Aires Argentina, im a .NET Web and Software Developer for last 25 years, mostly for business software, im currently self-employed with my own micro-company along my wife (Aurora ITS). Im taking the Complete Unity 3d Game Developer course to validate my self obtained knowledge by mostly experimenting and asking google, and to rapidly catch-up with most recent game developing technologies. I want to show you my Menu Page, first version with all assets and scripts right out-of-the-box, you can see some displacement on lines of exactly 40 characters width:

After some reverse - engineering i found what Text GameObject was too narrow to allow the 40 character configured as Terminal width, however the script was cutting the lines at 39 character so it never get the 40.
So i felt free to do some changes to DisplayBuffer.cs for fix that, this was the original code (lines 47 to 71):

    private string Wrap(int width, string str)
    {
        string output = "";
        int column = 1;
        foreach (char c in str)
        {
            if (column == width)
            {
                output += '\n';
                output += c;
                column = 1;
            }
            else if (c == '\n')
            {
                output += '\n';
                column = 1;
            }
            else
            {
                output += c;
                column++;
            }
        }
        return output;
    }

First problem i see was at line 53, when took action when the current column is equal to width (in this case setted to 40) they pass that character to next line, leaving that line with only 39 character as you can see in the next screenshoot:

So fixed that simply adding 1 to width part of comparison, so now they will cut at correct ammount of characters when the user input some large string.

Besides, when printing lines of exactly 40 characteres (same length of terminal width setting) the script was adding extra empty line, this one was a little bit tricky to catch but finally i discovered what the logic of if / else if / else blocks was incorrect because first of all it was checking for column position independently of what character was it, so if that character was a newline “\n” passed by WriteLine method, this behaviour was adding an extra newline “/n” after that. So simply swapped [if] and [else if] blocks to check first of all if that character is a new line, and then the column index, and problem solved, so this is my tweaked code (dont want to be disrespectful calling it “fixed code”) DisplayBuffer.cs lines 47 to 71:

    private string Wrap(int width, string str)
    {
        string output = "";
        int column = 1;
        foreach (char c in str)
        {
            if (c == '\n')
            {
                output += '\n';
                column = 1;
            }
            else if (column == width + 1)
            {
                output += '\n';
                output += c;
                column = 1;
            }
            else
            {
                output += c;
                column++;
            }
        }
        return output;
    }

And finally my Menu Screen is finished:

I hope this help to anybody,

thanks for so amazing course!

Cool man!

1 Like

Privacy & Terms