NWN2 Scripting.




Simulation Training.

Introduction
Home
First Program
Seeing Results
Variables and Data Types
Comments
Functions
Data Conversion
Random Numbers
Concatenation
Arithmetic Operators
Compound Assignments
One More or Less
Precedence
Relational Operators
The Open Branch
The Closed Branch
Logical AND
Logical OR
Compound Statements
ELSE If
Switch Case
The ? Operator
The for Loop
The while Loop
The do while Loop
Introduction to Functions
Passing Values
Passing Multiple Values
Multiple Functions
Simplifying Functions
TRUE/fALSE Conditions
Return Values
Setting Global Variables
Getting Global Variables
Setting Local Variables
Getting Local Variables
WayPoints Introduction
Static Waypoint Sets
Dynamic Waypoint Sets
Dynamic WP Cycles
Input Output

Data Conversion

These are times when you may want to use a certain kind of data type, such as an int (integer) and dispaly it as a string. This is especially ture when youa re using a pre-made function which may only accept string data types in its argument.

These are ways of converting from one data type to another. Being ablet to do this gives us great opportunities for using the NWN2 pre-made functions in many different applications.


A Programming Problem

Below is an example of a pre-made function used in NWN2 that we have used before. However, this time wew want it to dispaly a number values ( of the type int).

//This program causes text to appear over an entering creature.

void main()

 {

  object oPC=GetEnteringObject();

  int Number=15:

    FloatingTextStringonCreature(Number, oPC);

 }

 


 

If we try to compile the above program, we will get an error. the error will be:

ERROR: DECLARATION DOES NOT MATCH PARAMETERS.

What the error means is that at least one of the variables you declared does not match the datatype requirements of one of the functions parameters. In thise case, you declared a datatype int and put it into the function argument where a datatype string was required.

Recall that the function prototype is:

void FloatingTextStringonCreature(string txtDisplay, object oCreature);

From the above, you can see that the first parameter requries a datatype string.


Data Conversion

The NWN2 editor has a built in function that converst an int datatype to a string. In the editor, it is shown as follows:

//Convert nInteger into a string.

//*Return value on error: ""

string IntToSTring(int nInteger);

The above tell us that this function is of datatype string,  meaning that when we use it, it iwll return a string. The identifier for this function is IntToString and its argument atkes one value of type int. The comments state that if there is an error, it will return a null string (shown by the double quotes with nothing in them ""). The following program illustrates how it is used.

 

void main()

{

obejct oPC=GetEnteringObject();

int Number=15;

string txtNumber;

txtNumber=IntToString(Number);

FloatingTextStringOnCreature(txtNumber, oPC);

}


The above program willl now compile and work correctly. The next section will show us how to use, in a very itneresting way, what has been presented here.

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.