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 Values

The next section on Functiosn shows you how to make your custom functiosn even more powerful through the passing of parameters. These act like information messages to make your custom functions preform different tasks.


Creating a Function for getting Values

The following program illustrates a function that can receive values.

ta_speak

/*****************************

Created by Me

May9, 2007

WIll cause txt entered by the

calling program to float over

characters head.

******************************/

//Spoken=The text to appear.

void SayIt(string Spoken)

  {

   object oPC=GetEnteringObject();

     FloatingTextStringOnCreature(Spoken, oPC);

  }

 


 

The above custom function (saved as ta_speak) has a variable of type string declard within its parenthesizes. The identifier used for this variable is Spoken. It is important to note that this variable Spoken will e used in the NWN2 function FloatingTextStringonCreature. When this function is used (called) by antoher program, the string entered for Spoken will be passed on to the body of this function to the NWN2 function FloatingTextStringonCreature. This is illustrated in the following program:

 


#include "ta_speak"

void main()

 {

    SayIt("Passed from a function.");

 }

Observe in the above program that the custom function SayIt now has a string passed to it "Passed from a function". This string will now be passed on to the custom function (ta_speak) here, in that function, it will be passed down to the NWN2 function FloastingTextStringOnCreature. The above program will now cause the text Passed from a function to appear over the head of the PC


Terminology

There is some important terminology used in C++ for functiosn that pass values. These are listed below:

  • Parameters - The variales decaled within the parenthesizes of the function.
  • Arguments - THe actual values passed (put within the parenthesizes) of the called function.

In our example, the decalred variable Spoken is a parameter for the custom function SayIt. The calling program (void main) uses the argument "Passed from a function".


Where do we go from here?

In the next section ou will see how to create custom functiosn which contain more than one parameter allowing the calling program to pass more than one argument. This feature give sthe programmer a powerful option in the creation of custom functions.

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.