Variables and Data Types
Most programming languages such as C++ contain variables. A variable can be thought of as a place to store data. Data can be a number, a persons name, a date, or any other bits of information. In this section you will be introduced to some of the major kinds of variables you can use with C++. For the NWN2 game, variables are used for holding information about all of the games characters, creatures, items, and what they have done, as well as their individual characteristics. As you can see, variables play an important role, not only in the NWN2 environment, but in other environments as well. Declaring Variables
Declaring Variables
In C++ you must declare any variable you create. To declare a variable, you must declare its type and its identifier. Some of the most common data types used are: • string - Represents any keyboard character. • int - Represents a whole number such as 15. • float - Represents a decimal number such as 15.87. An identifier is a word you make up that identifies the variable you are creating. An identifier can be any word that starts with an underscore like MyValue or a letter of the alphabet. An identifier cannot be one of reserved words used by C++, such as main, string, int, and float.
Variable Declaration Examples
The program below declairs two variables of the type string:
void main( ) { string MyString; string AnotherString; } |
The above program compiles without errors. Here are some facts about this program: • The program starts with the void main( ) • The beginning of the body of the program starts with the opening brace {. • The first declared variable is of datatype string. • The identifier for the first datatype is MyString. • The first variable declaration ends with a semicolon ; • The second variable declaration meets the same requirements as the first declaration with the exception of its identifier which is AnotherString. • The program closes with the closing brace }
A Few Important Points about Variables
Variable identifiers are case sensitive. This means that MyString is not the same as myString or MYSTRING, at least as far as C++ is concerned. Note also that all of the C++ keywords must be in lower case (the main and string).
More Declaration Examples
The next program shows three variables being declared, each one of a different data type.
void main( ) { string Character; int HitPoints; float MyLocation; } |
The above program contains three variable declarations each of a different data type. Following each data type is the variable identifier such as Character, HitPoints, and MyLocation. Note that each of the three declarations end with a semicolon.
Assigning Values
We are now able to declare variables but we have not assigned them any values. In C++ the assignment operator is the = symbol. In C++ the = does not mean equal; it means assigned. The following program assigns values to three declared variables:
void main( ) { string Character, int HitPoints; float MyLocation; Character = "Moon Elf"; HitPoints = 18; MyLocation = 128.54; } |
Now each of our three variables have been assigned values to hold. Note that the variable Character which is of data type string has its value enclosed in double quotes. It is a rule in C++ that variables of type string must have their assigned values enclosed in double quotes. Note that both of the numeric values HitPoints and MyLocation do not have their assigned values inside double quotes. It is a rule in C++ that number values are never enclosed in double quotes.
Where do we go from here?
In the next section, we will see how we can now use variables in our NWN2 game environments.
|
Nwn2Scripting provides material for training only. We do not warrant the correctness of its contents.
The risk from using it lies entirely with the user.
While using this site, you agree to have read and accepted our
terms of use and
privacy policy.
Copyright 2008 by Adamson House, Ltd. All Rights Reserved.
Questions or Comments:
EMail Webmaster
Donations are to Adamson House, Ltd who maintains this site.
All donations go the the improvement of this site.
|
|