More flexible greeting

I fancied researching a slightly different approach to how the greeting presents itself in this lecture. I’m a C# novice but generally familiar with development concepts.

Instead of hard-coding the name inside the greeting, I wanted to see if I could pull the current logged in username from the computer being used.

And it was incredibly easy so thought I would share it.

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Hacker : MonoBehaviour
{
	void Start()
	{
		string greeting = "Welcome " + Environment.UserName + "!";

		ShowMainMenu(greeting);
	}

	void ShowMainMenu(string greeting)
	{
		Terminal.WriteLine(greeting);
		Terminal.WriteLine("");
		Terminal.WriteLine("What would you like to hack into?");
		Terminal.WriteLine("");
		Terminal.WriteLine("1) Your local library (easy)");
		Terminal.WriteLine("2) Your work computer (medium)");
		Terminal.WriteLine("3) GCHQ (hard)");
		Terminal.WriteLine("");
		Terminal.WriteLine("Enter your selection:");
	}

	void Update()
	{
		
	}
}

This should already be fairly similar to your code at the end of this lecture but with a small change.

Instead of a greeting variable that looks like:

string greeting = "Welcome Chris!";

We want to pass in the real username of the user playing the game.

To get that we can get the value of the UserName property which is stored in the System.Environment class which should be available on Windows or macOS. That is done simply like this:

System.Environment.UserName

We also need to be familiar with the concept of string concatenation (which will be covered later, I’m sure). Much like many programming languages, this simply involves joining together two or more strings using a concatenation operator which in C# is simply the + character.

So if we still wanted to hard-code the name into the greeting we could do this with multiple string parts:

string greeting = "Welcome " + "Chris" + "!";

Which produces the same variable value as:

string greeting = "Welcome Chris!";

So now we know how to concatenate strings, we can replace "Chris" there with the previously mentioned username property:

string greeting = "Welcome " + System.Environment.UserName + "!";

In doing that, running the code should produce something like this:

The code is fairly naive and doesn’t handle things like the username being empty or containing weird characters or being too long and such issues, but thought it was pretty cool and worth sharing :slight_smile:

2 Likes

That was kind of you to share Chris! Thank you. Did you have to import the class (or did you already explain that???) :slight_smile: -Matt

2 Likes

As far as I understand, the System namespace is available automatically as part of the C# framework so it doesn’t need to be explicitly imported.

However, I think I realised that part way through writing the above post so I’m going to show the that now below as the full Hacker class in my original example does import it, and therefore that does allow us to use Environment.UserName rather than System.Environment.UserName.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Hacker : MonoBehaviour
{
	void Start()
	{
		string greeting = "Welcome " + System.Environment.UserName + "!";

		ShowMainMenu(greeting);
	}

	void ShowMainMenu(string greeting)
	{
		Terminal.WriteLine(greeting);
		Terminal.WriteLine("");
		Terminal.WriteLine("What would you like to hack into?");
		Terminal.WriteLine("");
		Terminal.WriteLine("1) Your local library (easy)");
		Terminal.WriteLine("2) Your work computer (medium)");
		Terminal.WriteLine("3) GCHQ (hard)");
		Terminal.WriteLine("");
		Terminal.WriteLine("Enter your selection:");
	}

	void Update()
	{
		
	}
}

I’m a few lectures ahead now and of course @ben later tells us not to bother with the greeting at all. Which is fair enough but I felt that was a shame so as an alternative (and the course hasn’t covered this yet up to lecture 20) I decided to keep the greeting but return it from a function that can be called.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Hacker : MonoBehaviour
{
	void Start()
	{
		ShowMainMenu();
	}

	void ShowMainMenu()
	{
		Terminal.WriteLine(GetMenuGreeting());
		Terminal.WriteLine("");
		Terminal.WriteLine("What would you like to hack into?");
		Terminal.WriteLine("");
		Terminal.WriteLine("1) Your local library (easy)");
		Terminal.WriteLine("2) Your work computer (medium)");
		Terminal.WriteLine("3) GCHQ (hard)");
		Terminal.WriteLine("");
		Terminal.WriteLine("Enter your selection:");
	}

	void Update()
	{
		
	}

	string GetMenuGreeting()
	{
		return "Welcome " + System.Environment.UserName + "!";
	}
}

The function call is probably unnecessary now I think about it, but I was intrigued about the function return type stuff so decided to do it like that just to make sure my understanding was correct.

1 Like

It never hurts to experiment with functions anyway. I know that is something I’ve been doing. Refactoring is a concept I am exploring further. I like efficiency, so appreciate the ways to make code cleaner. Thanks for the follow up example code too!

2 Likes

Amazing! how did u discover it? my main problem when i get an ideia is to find the correct comands to make it happens. were did u find the comand System.Environment.UserName? were can i find this things?

Privacy & Terms