Variables in C++ are similar to Lua, but have a lot more options and a little more detail. Here is how to declare an integer variable in C++: int MyVariable = 3;
If we don't assign a value to the variable, the value will be a random number (whatever was left in memory): int MyVariable;Print(MyVariable);
This is called an uninitialized variable, and it is one potential source of mistakes to watch out for.Binary Data
Before we get into data types, we need a quick explanation of binary data. All digital data is stored in binary format (as opposed to analog). Binary format means that everything can be broken down to a series of ones and zeroes. In computer science, a bit is a single value that can either be one or zero. A byte is made up of eight bits, and is the smallest piece of information we can store. Therefore, an 8-bit value consists of a single byte, while a 16-bit value consists of two bytes. A 32-bit value means that four bytes are used, and a 64-bit value uses eight bytes.Integers are stored in binary form by using each bit to multiply a value by two. Think of a game of "20 questions", where you get to ask 20 yes or no questions to guess what object in a room the other player is thinking of. The easiest way to win the game is to divide the room in half and ask "is it on this side of the room?" Each turn, you divide the remaining space in half until you have only a few objects to guess.This is similar to how integers are stored in binary. The first bit answers the question "is the number smaller than 128?" The second bit answers "is the number smaller than 64?", and so on.So when we refer to "32-bit" or "64-bit" values, that is what we mean.Binary data is a very efficient way to store numbers. A single byte can store 256 different values. Two bytes can store 65,536 different numbers, and four bytes can store a whopping 4,294,967,295 different values!
Data Types
C++ is a statically typed language, while Lua is a dynamically typed. This means that the type of a variable in C++ must be defined by the programmer in their code. Unlike Lua, we will declare what type of data each variable is when it is first declared.Let's look at the most common data types in more detail.Float
The number represents any real number, and can be positive or negative. Numbers can be integer or decimal values:float f = 0.5;
Strings
Strings are a sequence of characters that form a bit of text. These can be used for the name of a player, or to store some dialog that will be displayed onscreen when you talk to a character in the game.The C programming language has a data type called "string" but C++ adds a more powerful version in the standard template library. Therefore, when we use strings in C++ we will declare them with the std::string data type.std::string name = "Smashy the Clown";
You can concatenate (add) two strings values together like this:std::string a = "Hello, ";std::string b = "how are you today?";Print(a+b);// prints "Hello, how are you today?"
Booleans
Boolean are a very simple data type that can only have two values, true and false. These are used for "state" values that only have an on/off value.bool playerIsReady = true;
Short
A short value is an integer that only uses 2 bytes (16 bits) to store its value. Consequently, these have a smaller range than 32-bit int values:short n=1;
Char
A char value is an integer that only uses one byte (8 bits) to store its value. Consequently, these have a smaller range than 32-bit int values:char n=1;
Signed and Unsigned Integers
Int, short, and char variables can be signed or unsigned. By default all integer variables are signed, but we can use the unsigned key word to make it so they only can hold positive numbers:unsigned char n = 255;
This is used to shift the resolution of the variable over to the positive side, so we can hold bigger numbers than would otherwise be possible. For example, integer RGB color values are normally unsigned char values, with a range from zero to 255 possible values.You can explicitly declare a variable to be signed, but there is really no reason to since this is the default behavior for any compiler we are using:signed int n = -1;
In practice, you will probably only need to use signed 32-bit integers for most things. It's usually not worth using smaller-resolution values to save space since it won't make any real difference in your game.Double
The only difference between doubles and floats is that doubles use 64 bits to store their value, and floats only use 32. Thus, doubles have more precision than floats and can store biggere numbers:double f = 0.5;
However, you are unlikely to require doubles in most of your code, since the range of 32-bit floats is quite big.64-bit doubles have nothing to do with 64-bit applications, and can be used in both 32 and 64-bit programs.Pointer
Pointers are an address that points to a block of memory. They may use 32 or 64 bits, depending on whether the program is being compiled as a 32 or 64-bit application. An uninitialized pointer will point to a random memory address until you assign a value. This can cause problems that are difficult to track down, so you should always, always, always initialize your pointers when they are declared:Now a pointer can work with any data type. We can have pointers to integers, strings, floats, or anything else. A general-purpose pointer with no data type can be declared with the void key word:void* MyPointer = NULL;
NULL is a special value that means "nothing". Unlike in Lua, the C++ NULL value actually equals zero. If you want a NULL memory address that cannot be compared to an integer, use the nullptr data type.void* MyPointer = nullptr;
Don't worry about pointers too much, because we will come back to this idea later in more detail.Auto
The auto key word is a new feature in C++11 that chooses a data type based on the value that is assigned to it. The code below will declare an int variable:auto n = 3;
Can you guess what type of variable f is in the example below?auto f = 3.0;
If you guess float, you are close but it's actually a double. You can make a float value by adding "f" at the end of the number:auto f = 3.0f;
The auto key word is particularly useful when we have very long variable names we don't want to type out.Converting Data Types
We can convert many values to different data types in C++. Sometimes this is done automatically, but often times we need to explicitly force conversion of one data type to another.Integers and Floats
int n = 1float f = float(n);
In fact, conversion between floats and integers is automatic:int n = 1float f = n;
When we convert a float to an integer value, the value will be rounded down, not rounded to the nearest integer:float f = 0.9;int n = f;Print(n);// prints "0"
If you want to round a float value to the nearest integer, use the Leadwerks command Math::Round:float f = 0.9;int n = Math::Round(f);Print(n);// prints "1"
Integer math and floating point math are two different things. If your program comes across an equation with an integer and a float, the integer will be converted to a float before the operation is performed:int a = 5;float b = 2;float f = a / b;Print(f);// prints "2.5"
However, if the operation only involves integer values the result will be rounded down, even if it is being assigned to a float variable:int a = 5;int b = 2;float f = a / b;Print(f);// prints "2"
You can force the operation to use floating point math by converting one or both integers to floats:int a = 5;int b = 2;float f = float(a) / float(b);Print(f);// prints "2.5"
Integers and Booleans
Integers can be converted to boolean values. Zero will be equal to false and any non-zero value will be equal to true:int n = 1;bool b = n;Print(b);// prints "true"
int n = 0;bool b = n;Print(b);// prints "false"
Converting to and from Strings
The easiest way to convert an integer to a string is to use the Leadwerks String() function:int n = 3;std::string s = String(n);
The Leadwerks String() function can also be used to convert a float to a string:float f = 0.5;std::string s = String(f);
You can also convert a boolean value to a string:bool b = true;std::string s = String(b);
To convert a string to an integer, use the String::Int() function:std::string s = "3";int n = String::Int(s);
To convert a string to a float, use the String::Float() function:std::string s = "0.8";float f = String::Float(s);
Conclusion
You're starting to build some pretty advanced knowledge that's good not only for games, but computer science in general! (Now would be a good time to brag to your parents about how smart video games are making you.) If any of this wasn't clear or you don't understand something, feel free to let us know in the forum.