3316 words on this page!
Although I've done a small amount of coding, there is still a lot that I do not know and that I am not too confident about. I have never used C# before, however, having used python before in secondary school, this gives me a slight advantage since programming languages follow roughly the same principles as each other.
Key Phrases
Alphanumeric characters - Any character that can be pressed from a key on a keyboard.
Null - Something contains no value.
Variables
String - multiple alphanumeric characters put together.
Integer - any whole number
Bool - (AKA Boolean) true/false, yes/no statements
Float - number including decimals
Char - (AKA character) is just a single alphanumeric character.
C# Terms
Public - Can be accessed anywhere.
Private - Limits the accessibility.
== - Equal to
!= - Not equal to
> - Greater than
< - Less than
>= - Greater than or equal to
<= - Less than or equal to
return - Stops the program
|| - used as OR
&& - Used as AND
Our first lesson saw us begin to use C# with the help of the website .NET Fiddle, which is where we write up our Programs. We used C# to create a simple bit of code that would ask for the user's name and age, which would be one of the first things you'd do when playing any game. Below is the explanation for the code:
Here was the result of the program in the console window.
I think next time I do something like this, I could include some more text like an intro to make it seem more like a proper program. It would also be good to include some if statements to broaden the responses to the questions, but we haven't learnt that yet in C#.
Line 1 is used to get useful functions/commands such as Console which are obtained from the system library, as a namespace. At the end of the line, I added a semi-colon, which we do almost everywhere to tell the computer that we have reached the end of the line.
Line 3 is used to create/declare your program, which I call Program.
You then add an opening brace to define the start of a block of code. On lines 5 and 6, I define two variables: the name of the user stored as a string in "playerName", and the age of the user stored as an integer in "playerAge". At this point, they have not been assigned a value, so are considered null. These must always be inside the class to be accessed anywhere, but outside of the method, in this case, Main.
On line 8, I declare a method, which I call Main. This is the start of my executable program, and I create another brace to signify the start. These must always be declared inside of a class. The brackets, or parentheses, are used to add any extra conditions.
Starting from line 10, I then start to properly write the code. I will be using Console commands since we are displaying text in the console window. The WriteLine is used to display text in the console which can be useful to give the user instructions on what to do next, like inputting their name which is exactly what line 11 is for. Any time you want to have some specific text outputted, you must add quotation marks to declare the string. For declaring a char, you would use single quotes, which is different from other languages since they serve the same purpose.
For Line 11, I first stated the variable that I would be changing the data of. The equals sign is used for assigning a value to this variable. The ReadLine command is used to store data from the user's input(s). The reason for having information sent to the console beforehand is that the user could think the program crashed, or they could input the wrong value, leading to a crash.
Then, on line 12, I implement the new data stored within the variable back into a WriteLine command. The + is used to put the pre-made string with the variable that now includes the data from the user's input.
I then went on to do this again for the user's age but came across an issue. Since the user inputs a string, the program will not allow you to just use ReadLine. You have to add Convert.ToInt32(), and then include the Console.ReadLine() inside of it as an extra condition to convert the string to an integer.
To finish it off, I then just added some closing braces for the method and program and the code is complete.
In this lesson, we began by trying out adding if statements to our code. As was explained, this is one of the most important things to learn in code since you use it for basically everything.
To do it, you first need to add a line that starts with if and add some parentheses that include the conditions for this specific if statement inside. You then go down a line, add an open brace, and then go down another line and write your code inset within a new box that will be used if this if statement is true. I add the closing brace at the same time as adding the opening brace to avoid forgetting it later on.
If I want to include more statements, you can either use else or else if. If you are having multiple statements, you will need to use else if which just acts as another if statement. However, in the end, you need to have an else statement if the specific data does not meet the requirements of the other statements.
To start the rock paper scissors project, I created a little introduction so the user knows what they are entering into. I then added a line that requires you to press enter so the user isn't being overwhelmed by too much at once, which is something I will keep adding throughout.
I then started to add a section that would ask the user for their name. I had to create a string variable within the program that I called playerName. I then just had it return the name, greeting the player.
I then started the age verification which I would go on to use if statements for. I once again created a variable, but this time as an integer. I made sure to use the convert command to convert the inputted string to an integer. I then started the if statement by checking if the user was 18 years old or over. I later realised that I would have to start with the largest value, since if someone is 50 or over, the initial if statement would still be true first, leading to the text for anyone over 50 being the same as anyone over 18. To fix this, I just had to swap around the statements. Since this rock, paper scissors game requires you to be 18 or over, you will be told you are too young if neither of the first statements return as true. I then added some text around the sections so it is easy to identify what each part of code is doing in the program.
This is an example of an array we define a variable which is a string, but we have to include square braces so the program knows it stores multiple pieces of data (an array). You then make the name for it and then write that it's equal to a new string. The information put within these square braces is how much data can be held within this variable. In this case, I put 3, so it can store 3 pieces of data. I then add braces with the data inside, separated by commas. I created another variable which holds integer data, which is what I will use to choose what is displayed.
I then get the user to input a number, which I convert to an integer using Convert.ToInt32, and use this as the number in the array that will be outputted. Because I asked for a number 1-3 and arrays start at 0 (so, in this case, it would be 0-2), I just minus one from the variable. This can also be done by writing the line: choice1 --; which just subtracts one from the variable's data. Because this is a 1D array, whatever number I input will be the output.
For a 2D array, it's basically using rows and columns. You start off creating the new variable the same, but the first difference is that you add a comma within the first square braces. In the next square braces, you put the amount of data in the first address you want (basically rows), and then add a comma before adding the next number which represents the amount of data within the second address (basically columns). You can then create the data within the array variable by writing it out like a 1D array, but instead with braces around all of the data, and with commas separating each group of data.
Here, we went searching around to try to find a way to convert the first character of a string to an uppercase letter. Since, although there is a way to make all characters uppercase or lowercase (using .ToLower or .ToUpper), there isn't an easy way to do it with just the first character. Searching the internet for a solution, we found someone that found a way, which I altered slightly so it would be better for me.
In simple terms, the user inputs their name into a string variable. This string variable then gets taken to a different class and converted into a character array, which stores each of the character's from the string in it's own section within the array. The first character in the array gets changed to an uppercase letter, and the array is then returned to the class as a string that it had been originally called from.
Going into a little more detail, the class that we will be making this lovely conversion in is called before the class that we will be using for most of the things. This is so the software actually reads this and can be later referred to this class, since otherwise, trying to call to a specific method within this class without the computer having read it first would just not do anything. Within this static (so it can't really be altered with) class, I declare a method that I call ToUpperFirstLetter. This is important, since wanting to call to this method will require me to write ToUpperFirstLetter(). Within the method, it first opens an if statement that will return the string as nothing if the user doesn't enter anything (null). If that if statement is false, it will go on to declare a new character array called letters. This array is then equal to the source input (the user's input) and converted into a character array. The first character in this array is then converted to uppercase using the .ToUpper command. The method then returns the new data as a string of these characters.
You gradually regain consciousness...
Throbbing pain overwhelms your thoughts...
<You> Where am I?
A warm light source flicks on and diverts your attention to the humanoid silhouette towering over you.
<Unknown> So, you're finally awake.
<You> Whe-
<Unknown> We begin now
The figure carelessly crouches down and stares deeply into your soul.
<Unknown> It's a game of life or death.
<Unknown> But first, let me ask a few questions...
<Unknown> Before anything else, what's your name?
<Unknown> (playername), generic, no wonder you did what you did.
The figure bows their head before speaking again.
<Unknown> How old are you?
>= 50 - <Unknown> You look decent for that age, but why wait so long.
18-49 - <Unknown> Still got a good few years in you then.
Other - <Unknown> That is... interesting.
15-17 - <Unknown> Couldn't have just waited a few years, could you (playername)?
<Unknown> I hate having to do this, but it's better for you.
<You> (playerresponse)
<Unknown> Please be quiet, it only makes it harder for me.
The figure assertively raises their arm, outstretched with a familiar pew pew, pointed directly at your face.
They look away, scrunching their eyes before a deafening bang spreads through your mind.
Your lifeless body ragdolls to the floor with an expression of pure remorse.
Simulation ended.
<= 14 - <Unknown> Some of you nowadays are insane...
<Unknown> Almost doesn't seem as bad if you have the guts to do that now.
The figure outstretches their arm and you hear a click and a bang before being able to see what it is.
Simulation ended.
<Unknown> You must now make your choice.
<Unknown> I'm serious when I ask you this.
<Unknown> Will it be rock [1], paper [2] or scissors [3]?
!= 1|2|3 - <Unknown> (playername), I told you, take this seriously.
<Unknown> I just thought it would make one of our last moments a bit more entertaining.
<Unknown> What did you choose? I chose (choice)...
<You> I chose (choice).
win - <Unknown> So, you have won, but at what cost...
<Unknown> (playername), you are now the guard, or whatever this is.
<Unknown> You must decide if you, or the latest, greatest sinner deserves life.
<Unknown> Whoever lives, chooses the next person's fate.
<Unknown> I couldn't bare the guilt of my previous actions any longer, so I made a game select my fate.
<Unknown> Thank you for finally releasing me from this curse, and I wish you luck
The figure lies back, gets into a comfortable position, before slowly fading into a cloud of dust and smoke.
A blood-curdling scream engulfs the silent atmosphere.
<Voice> AHHHH, WHERE AM I
Realisation flows through you - you or them...
lose - <Unknown> Again... Really?
<Unknown> So you have lost but won in another way.
<Unknown> Maybe the next ####### will release me.
<Unknown> But for now, goodbye (playername), and I hope you find peace.
To start using Unity, we had to load up the Unity Hub. When opened, we had to sign into Unity - not using our college accounts. A pop-up would then say that we had to install Unity, however, we already had a version installed, so we just had to find the file location of the .exe file which was the install of Unity 6. The version installed is 6000.0.15f1, which is a beta version so has some issues. It's important to know what version I use at the college because if I want to use Unity at home, it needs to be the same version so I can use projects in both places.
Once that was all ready, we had to go to the Projects tab in the hub and click New Project. We went to Core, selected Universal 3D, gave our project a name and I chose to locate my project in the temporary workspace. Any other time that I use Unity, I should save it to my hard drive so it can be accessed at home, but also just so it isn't taking up the computer's storage. Once clicking Create Project, it will load up Unity in the new instance you have created.
We then were told about the basics of the UI: Hierarchy, assets, scene, inspector and the camera viewer (called Game). To start, we right-clicked in the hierarchy window, 3D Object and clicked on the model we wanted to create. I used a plane as the main baseplate, some spheres and cylinders. I did later also use cubes for the table.
In the Inspector window, I made sure the collider was checked for each object since this allows it to collide with other objects. Adding the Rigidbody component will give the object gravity, and so objects can now interact with each other. You can change some settings such as the mass, but I haven't needed to so far.
I then went on to add a Materials folder inside the Assets folder. I then added Materials inside of this folder, which I made a specific colour. To use these in the project, you just have to drag them onto the object that you want them on.
The first thing we learnt today was how to import models that I have made in 3DS Max, along with attaching the textures that go along with them. Getting the model is as simple as dragging it into our assets bar, and then dragging it into the scene. To texture the models, I have to also import the texture files into the assets bar. I did this specifically in a file so I could easily find them. I then had to create a material that I dragged onto the model, and then I could start adding the maps that I imported. You have to make sure to click the lock icon at the top of the screen above the Inspector though because otherwise, the window will change as soon as you deselect the material you want to work on.
Once all of the materials have the correctly assigned maps, you can drag the materials onto the designated parts of the model and it should be ready to use.
The first thing I needed to do with the model was to add the rigidbody component to the group that contained all of the models for the paint can inside. This would make it so the whole group would have the same gravity, so none of the parts should get loose and fall off.
Next was getting the collider to work. For this, we had to put a mesh collider on each of the models that made up the paint can, and we had to click the box to let Unity know we wanted a convex collider instead of a concave one, since otherwise it didn't work and gave us an error.
The paint can now has gravity and a hitbox. I accidentally left in another component which wasn't needed that made it balance on the edge of the can, so I need to remember to check all of the components if something does go wrong.