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

Passing Multiple Values

Functions can be created so that you can apss many values to them. In the previous example you saw how to pass one value to a custom function. In this section you will see how to pass more than one value. Doing this can be a great time saver for events that you many use several times in your modules.


Creating A Function for Getting Multiple Values

The following program illustrates a function that can receive three values.

ta_agecheck

//-------------------------------------------

// Age Check by Me

// May - 2007

// Checks a PCs age and sends a message

// depending on the age of the PC

// int AgeLimit=The age to be checked.

// sting Message1=Message to be sent if

// age limit is equal or exceeded.

// string Message2=Message to be sent if

// age limit is less than the AgeLimit

//-------------------------------------------

 

void AgeMessage(int AgeLimit, string Message1, string Message2)

{

 int Age;

 object oPC=GetEnteringObject();

 string Message;

 Age=GetAge(oPC_;

(Age > AgeLimit)?(Message=Message1):(Message=Message2);

 FloatingTextStringOnCreature(Message, oPC);

}

 


The above custom function (saved as ta_agecheck) has three variables declared within its pareenthesizes.

  • int AgeLimit is a parameter the value of which whill be passed to (Age>AgeLimit)
  • string Message1 is a parameter the value of which will be passed to (Message=Message1)
  • string Message2 is a parameter the value of which will be passed to (Message=Message2)

The above program will now cause the text from Message1 or Message2 (depending on the value of AgeLimit) to appear over the head of the PC.

 


The Calling Program

The following program will call the above custom function.


#include "ta_agecheck"

void main()

 {

  AgeMessage(50, "An old dude!", "A youngster!");

 }

 


 

The above program will call the above custom function AgeMessage( ). It does so as follows:

  • #include "ta_agecheck" = This includes the program (ta_agecheck) that contains the custom function AgeMessage()
  • AgeMessage(50, "An old dude!", "A youngsster!") = The Called function with the arguments: 50 ( an integer), "An Old dude!" (a string), and "A youngster!" (a string) as required.

As you can see, the use of custom functions called from different programs can be a great time saver and result in cleaner more consistant code.


Where do we go from here?

In the next section you will see how to create several custom functions which contain one or more parameters allowing the calling program to select which custom functions(s) it needs to use.

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.