By definition, Variables are a representation of a value, known or unknown, that has the potential to change. Translation - The value assigned to a variable can change. In nwnscript a Variable is used to represent all sorts of data types. The most common is a numeric value, others include objects, effects, strings and so on. Variables are specific only to the script that is running at that time.
All variables must be declared before they can be used. In other words, you must tell the script compiler you are going to use a variable, what type it is, and what value it is. As the old southern farmer would say "I do declare!" Note that by default any variable declared in nwnscript without a value is 0. A variable declaration is constructed by this method;
{VARIABLE_TYPE} {VARIABLE_NAME} [VALUE]
VARIABLE_TYPE = Any of the nwnscript data types. A Data type is just that - a type of data that will be held in the VARIABLE_NAME. In nwnscript, the data types are as follows.
action
const (or constant, yes a constant is a variable too)
effect
event
float
int (Integer or number)
itemproperty
location
object
string
struct (or structure)
talent
vector
Notice that I left 2 types out, the command and void types. The command type is a void-returning function and the void type is a null data type and doesn't really return anything. These will be explained a bit clearer later. :)
VARIABLE_NAME = Pick a name, any name. Call it Sam, read, or Martha if you want - doesn't really matter. Just something that makes sense to you to represent the value. For example, many scripters us "oPC" as a variable name to represent the Player Character Object variable. Many Noobs just starting to learn make the mistake of thinking that "oPC" is always the Player character - this is not the case. a variable name of "oPC" could be anything and likewise, the name could just as easily be "oChocolate".
VALUE = Any value that matches the VARIABLE_TYPE. If you are declaring a integer (int) variable, then the value must be numeric. If declaring a object type, then the value must be a object, so on and so on.
Lets look at a few variables;
int I = 1;
int is the VARIABLE_TYPE, the letter I is the VARIABLE_NAME and 1 is it's value.
object oPC = GetEnteringObject();
object is the VARIABLE_TYPE, oPC is the VARIABLE_NAME and GetEnteringObject() is it's value. How is that a value? you might ask. If you look up the GetEnteringObject() function in the script editor or in the Lexicon, you will see the word "object" in front of the function. This means that the function returns that data type when it is called, in this case an object. More on that later. A variable can be declared without a value, for example: int i; Here we have a VARIABLE_TYPE, a VARIABLE_NAME, but no VALUE. By Default, the value is 0.
When Giving a variable a value, you are assigning or "Defining" the variable. It is important to know the difference between these to words. Declare: "I declare the integer I is equal to 1" Define: "I Am going to define the variable I as 1" A variable can only be defined once within a given scope but they can be defined many many times. Copy & Paste the following script into the script editor and save the file.
void main()
{
int I = 1;
int I = 10;
}
The script won't compile and you will get a ERROR: VARIABLE ALREADY USED WITHIN SCOPE error. This is because you declared the same variable twice within the same set of curly brackets, these will be discussed more in the next section. Now copy this script and save;
void main()
{
int I = 1;
I = 10;
I = 12;
}
This one will compile just fine, and what does I equal at the end of the script? - 12. Notice I don't have "int" in front of the other two definitions, 10 and 12. This is because the compiler has been told that I is a variable so I can change or define the value of I without declaring it over and over again.