C# Challenge - ASL Program
Want to introduce yourself to me?
It’s been quite some days since I wrote my last article. Well, to be honest, keeping up with daily Medium posts during exam phase while keeping up with work next to school and connected with some RL issues was a bit tough. Very tough actually. We both, me and you, know it has not quite worked out as I thought it would. I mean, just look at the non-existing posts from the last few days. Okay, enough of that now. Let’s get started with the programming task of today. To get back into it, let’s start out simple.
Let’s take a look at the task we want to solve at first:
Breaking down the problem
Looking at the task, it’s pretty easy to read out what we need to create:
- We need three variables:
- One for storing the age
- One for storing the gender
- One for storing the location - We want to keep the variables private to keep up with best practices. Because the values should be changeable inside of the Inspector, we simply need to add a [SerializeField] attribute before the variables.
- We need to check for input with the Space key on the keyboard.
If the space key was pressed, we should print out the variables.
Setting up the variables
Let’s go over the variables one more time. Age, gender and location.
- Age is usually a full number, so we need an int type here. Sure, you can also say you’re 18 and a half years old. But I never anyone saying that, have you? :-)
- The gender can either be a string or simply a char type.
A char is just a single letter, while a string can store more than just one letter. I will use the char here, as genders are shortened to just the first letter of them most of the time. - As for the location, it’s best to declare it as a string.
Having done that, the variables should look like this:
Checking for keyboard input
One last thing to do is to check if the player has pressed the space key on the keyboard or not. The solution is already in the prior sentence. We need an if statement.
As keyboards input usually do not happen directly in the first frame after the game has been started, we will add the if statement inside void Update(), as keyboard inputs can happen at every frame.
We want to print out the variables into the Console. We just need to use Debug.Log() here. We want to log the variables. To make it look a bit nicer, let’s add a small text before the variable, so the end-user can understand what the variable is about more easily.
And that’s all we need to do! If we attach the script onto the Main Camera now, we can see the fields for the data input inside the inspector.
Running the program, hence the end result, will look like this:
It just feels great to finally being able to continue the learning trip again. A simple start as a warm-up was exactly what I needed to get back into programming. Excited for more! See you aroud in the next post!