Hey there, Gamers and Game Makers! This week's blog is a quick update about the tutorial series. The introduction to C# series seemed to go down very well so, I've decided I'll do another series with C# but I'll base it around building a game. I'm thinking it'll be a platformer. As for tutorial series after that, I want to get a feel from you guys on what you might like me to cover. So, I'm putting a poll down below of a few topics and whichever one gets the most votes will be the next series I do after the Platformer one. If there's something other than what's on the poll, let me know below. Hey there,Gamers and Game Makers! Welcome back to the introduction to C# series. In this, the final post of introduction tutorials, we're going to take a look at loops. So, we use loops when we want to execute a section of code multiple times as long as it continues to meet a certain condition. For this example, I'm going to use our script from the Arrays tutorial as seen below. Ok, I want to print the contents of my array but I don't want to write a print command for each one as that's just time consuming and just not good practice for coding. Since I know the size of my array, I can use a For Loop to run through the array and print out each name up until it reaches the last index of the array. To do this I need a variable called index to keep track of the position of the array. Now, I don't have to declare this new variable up with my other variables. I can simply declare it within my for loop and initialize it in the loop also. It'll still work if I declare the index variable as I normally do with my other variables. The syntax of my loop would change slightly. the syntax for my for loop is as follows: I start off my for loop simply by typing the word "for" followed by a set of brackets. Inside the brackets, I declare my index variable and assign it a value of zero. I then set the for loops condition which in this case is index < 4. This means, as long as the index value is less than 4, the for loop will continue to execute. I follow this with index ++. This is our exit condition. This increments the value of index as long as it is still less than 4. Once the value of index reaches 4, the for loop knows it should no longer execute. Inside the body of our for loop, we can put what ever code we want to run. It's very much like how we did it for our If Statements. Since we want to print each name in the array without using multiple print commands, what we do is type our print command once and inside the brackets, type the name of our array followed by square brackets. Inside the square brackets we type our variable index. What this does is print the name that corresponds to the index position that is currently the value of the index variable. As you can see, when we run the script all the names of the array get printed even though we only have one print command. Ok, let's take a look at a While Loop. A while loop works similarly to that of a for loop but we can use a while loop in cases where we don't know the number of times we need it to run. Now, I'll still use the array example so we do actually know the number of times it's going to run. The syntax for a while loop is as follows: As you can see with the while loop, I've declared my index variable as I normally would at the top and inside my while loop I've simply put the loops condition which is index less than 4. In the main body of the loop I still have my print command with the array name and index variable inside the print command but I've moved my exit condition down into the body of the loop. This means the index will increment after each printout until it reaches 4 at which point the loop will know not to execute anymore. Once again, when we run the script we can see all the names of the array have been printed out using just the one print statement. Loops are a great way of running multiple calculations without having to clutter your script with excess code.
That does it for this short introduction to C# series. I hope you've found it helpful and I plan to start another series soon that will expand upon this and dive into making more complex and fun projects. Until next time! Hey there, Gamers and Game Makers! In this week's blog, we're going to continue with our introduction to C# series by looking at Arrays. Let's get started. So what exactly is an Array? Well, you can think of an Array as a type of variable but an Array contains essentially a list of information. So for example if you have an int Array then that Array will contain multiple int values. We declare an Array in the following way: You first declare the type of Array you want by declaring the variable type as normal. You then follow that with a pair of square brackets.We then follow this with the name you want to give your Array followed by an equals sign and then "new" followed by the variable type. Finally we finish with another pair of square brackets. This time however, we put the size of our Array inside these brackets. This defines how many places are in our Array. I'm going to create an Array called names. So it'll look as follows: Ok, so how do we go about adding values to the positions of our new Array? We can simply say the name of our Array followed by square brackets with the index position of our Array followed by an equals sign and finally followed by the new value. Keep in mind that an Array starts its index position at 0. So the first value of your Array will be at position 0. If I want to add my name to the first position of the Array of names, it'll look as follows: Ok, I'm going to assign values to the other positions of our Array so that it is full. Our script currently looks as follows: Now that our Array is full, let's try printing the values from various positions in the Array. So if I want to print the name held at index position two, I'd type the following: As you can see, when we run the script, the name John is printed out. This is because we are telling our script to print the name held at index position two. If you look at your initialization, you see that index position two is given the string value of "John" and that is what get's printed to the console. If I change the index position to one, we see the name "Tim" being printed because that is the name held at that index position. That does it for this quick introduction to Arrays. We can do so much more with Arrays so I encourage you to play around with them.
Until next time! |
Archives
April 2019
Categories |