C++ arrays do exactly what their name implies. They allow us to handle whole arrays of data in one simple structure. Certainly, there is quite a bit to learn about arrays but actually they are very straight forward as we we will see.
[widgets_on_pages id=”udemy_advert_cpp_1″][widgets_on_pages id=”udemy_code_details”]
What exactly, is an array?
If a variable is a box in which we can store a value of a specific type, like int, float or char, then we can think of an array as a whole row of boxes. The row of boxes can be of almost any size but can be of only one type.
Declaring an array
We can declare an array of 10 int type variables like this:
int aBunchOfInts [10];
Now we have an array called aBunchOfInts that can store 10 int values. At the moment, however, it is empty.
Accessing the elements of an array
To add values to the elements of an array we can use syntax we are already familiar with combined with some new syntax, known as array notation. In this next code, we store the value of 99 into the first element of the array:
aBunchOfInts [0] = 99;
[widgets_on_pages id=”bcgp_cfgp_gpp”]
To store a value of 999 at the second element we write this code.
aBunchOfInts [1] = 999;
We can store a value of 9 at the last element like this.
aBunchOfInts [9] = 9;
Note that the elements of an array always start at zero and go up to the size of the array take away one. Similarly to ordinary variables we can manipulate the values stored in an array. The only difference is that we would use the array notation to do so. In this next code, we add the first and second elements together and store the answer in the third.
aBunchOfInts [3] = aBunchOfInts [0] + aBunchOfInts [1];
Arrays can also interact seamlessly with regular variables, like this perhaps.
int a = 9999; aBunchOfInts[4] = a;
Initializing the elements of an array
[widgets_on_pages id=”udemy_advert_cpp_2″][widgets_on_pages id=”udemy_code_details”]
We can more quickly add values to the elements like this example that uses a
float array.
float myFloatingPointArray [3] {3.14, 1.63, 99.0};
Now the values 3.14, 1.63 and 99.0 are stored in positions 1, 2 and 3 respectively. Remember that when using array notation to access these values we would use 0,1 and 2.
There are other ways to initialize the elements of an array. This slightly abstract example shows using a for loop to put the values 0 through 9 into the uselessArray array.
for(int i = 0; i < 10; i++){ uselessArray[i] = i; }
So what do these arrays really do for me?
The real benefit of arrays in our game code was hinted at the start of the tutorial. Arrays can hold objects (instances of classes).
Imagine that we have a ZombieEnemy class and we want to store a whole bunch of them. We could do so like this:
ZombieEnemy horde [5] {zombieEnemy1, zombieEnemy2, zombieEnemy3... etc};
The horde array now holds a load of instances of the ZombieEnemy class. Each one is a separate, living(kind of), breathing, self-determining Zombie Enemy. We could then loop through the horde array each pass through the game loop moving the zombies, checking if their heads have met with an ax or if they have managed to catch the player.
The best way to get a feel for this is to see it in action.
hey john. when will you upload the new tutorials?
Hi There,
Really sorry but events got the better of me. I had to start a new project a month earlier than expected and it messed up all my plans. I should have posted an explanation. Sorry about that.
Unfortunately there won’t be much new stuff for a couple of months but when this latest C++ game book is done I am going to take a break from writing books and concentrate on the website.
Really sorry for lack of communication!
Thanks for the heads up. This website has been quite helpful! Can’t wait for the next series!
I will be as quick as possible.
In the next tutorial, will you explain how to create a window on the desktop along with the game? Or is there a dev program you will use to show the game? I’d recommend showing how to create a window, as I’ve been wondering how to do that for months.
This tutorial sets up your development environment: http://gamecodeschool.com/sfml/setting-up-visual-studio-and-sfml-development-environment/
Then this tutorial opens a window and does a bit of drawing: http://gamecodeschool.com/sfml/building-your-first-sfml-game-project/
Good luck and have fun!
Hello john,
it’s me again was just wondrring if you could link me to where i can buy your book (if its got anything different to the tutorials).
Hi George,
The tutorials on this site can be considered an excerpt from the book so there is some overlap. I like to think the book is much wider and deeper. If you look on the publisher’s site here. https://www.packtpub.com/game-development/beginning-c-game-programming you can click on each chapter to see the main headings and decide for yourself if there is enough content to justify the price. You can also see some screenshots of the three projects here. http://gamecodeschool.com/books/beginning-c-plus-plus-game-programming/
Thanks for your question,
John
cheers was thinking would make learning a lot easier