All of us builders who were not coders before we came to NWN struggled with variables at some point, they are really not that hard once you gain the basic concept of what they are..
If you look at variables like algebraic math expressions, like (n) or (z), then you understand that they represent something... for example: n = ? Like Ghost states above, you can name a variable anything, therefore:
wOOt = 7491023;
We must tell the script what type of variable it is first, so the script can understand what your variable name means.
Types of Variables:
int (This is a whole number 0 - 198759 for example)
float (This is a decimal number 0.2344578 or 24.219 or 9234.5)
string (This is a word or words which must be enclosed with parenthese >> "like this" )
so we define the variable by first telling the script what type of variable it is, then we give the variable a name, and then we give the answer to "What does the variable equal?"
Standard format for a variable
type name =
int / Any_Name / A Whole Number
float / Any_Name / A Decimal Number
string / Any_Name / A word or multiple words. (Even a sentence or paragraph if you like.)
so for example..
int MyInteger = 3; ( note ; is for closing the line, which is done after every function in nwscript)
float MyFloat = 2.8;
string MyString = "My string is this.";
You can tell the script what the variable equals outright or you can just declare the variable, which then in turn tells the computer to hold a slot in memory for this variable. (we will define it later)
Examples of declaring variables...
int nVar;
float fTime, fMore, fLess;
string s1, s2, s3;
The purpose of declaring a variable has multiple points, first we declare a variable because it may change multiple times through coding.. Secondly, we may declare a variable because it will be reused repeatedly by the script, therefore allocating a slot in memory allows for better performance. This is important, because if your going to use an integer say 40 times in one script, then you definitely want to declare it, but not define it at first.
When we define a variable, after it has been declared, you will only see the name of the variable followed by an equal sign, and then everything that follows can be what defines the variable..
Therefore:
int a, nTime, c; <<Declared Variables
a = 1; <<Defined Variable...
nTime = GetHour(); <<Defined Variable (I use a function as an example of an integer here)
GetHour() is a function which will tell the script what the current module hour is, which is an integer number, so we can use it to define what nTime actually equals... (which would be the current module hour when the script fires)
Also we can add variables like so..
c = nTime + a; <<< This is how we add the int a to the int nTime (module hour + 1)
or
nTime += a; << myself + another int
or
nTime = nTime + a; <<another way to add variables together.. (redefine nTime as module hour + a)
There are many uses for variables.. Also, the name for a variable must always be one whole word not multiple spaced out words, therefore (HI_thERE) would work, but (Hi There) would not work...
Variables are for the most part used to tell the script something, generally they are used to HOLD the number, decimal number, or word(s), sometimes we define a variable with a function, sometimes we get the information from an object, what ever information your using or how your using it, make sure the information your putting in the =
part, is correct for int / float / or string, for "234" does not equal 234 nor does it equal 234.0 Make sure what your put in
is proper to the type of variable your using..
Constants like this one: VFX_DUR_AURA_BLUE (For example) is an int
Therefore if you wanted to define ( int nPart ) as the belt slot of a PC's inventory, you could do it like so
int nPart = INVENTORY_SLOT_BELT; <<so nPart = the belt slot...
we could use nPart like so..
object oItem = GetItemInSlot(nPart, oPC);
Here we have told the script oItem = The item in the Belt Slot of THIS PC (We would need to define who the PC is)
Getting information from an object is just a matter of one function, and setting the variable on the object another.
object oObect =
;
//For getting the information from the object
int nStored = GetLocalInt(oObject, "THIS_VARAIBLE_NAME");
int nInt =
;
//For setting the infromation on the object
SetLocalInt(oObject, "THIS_VARIABLE_NAME", nInt);
You would natrually replace "THIS_VARIABLE_NAME" with the name of the variable your wishing to use, just make sure you write it down, if you plan on using it in other scripts...
Thats the standard for getting / setting variables on an object, the object could be a placeable object or a PC...
Ok, that's enough for now, if you need more help, just ask..
Modifié par Genisys, 28 août 2010 - 05:01 .