Functions and Includes
This scripting forum is to discuss scripting and for those needing help scripting to get help from others.

Moderators: Mermut, rdjparadis

Post Reply
User avatar
Winterhawk99
Posts: 1627
Joined: Thu Oct 08, 2009 12:00 am
ctp: Yes
nwnihof: Yes
Location: Pa.
Contact:

Functions and Includes

Post by Winterhawk99 »

NWN script is a function based scripting language. It is very similar to C++. You could even call it a bastard version of C++ if you wanted too. The driving force of the scripting language and it's center lies in the scripts functions. I wanted to try to explain functions so that everyone would become familiar with them. functions in NWN are often packaged together and defined in scripts called Includes. Includes can be a series of functions that are used in multiple scripts and or scripts used throughout the module in unrelated scripts that do not connect to each other.

Have I confused you yet? I think I have just confused myself, so lets go back to functions.

A Function defined (as I would put it) is a label for a small script you can use over and over in many ways throughout a module.

Here is an example that I will use in code later:

void ApplyRespawnPenalty(object oRespawner, int iStart = FALSE)
{
int iHD = GetHitDice(oRespawner);
string sCampaignName = "HarvestMoon";
int iPenalty = GetCampaignInt(sCampaignName, "iPen", oRespawner);
if (!iStart)
iPenalty = iPenalty + 1;

// Cap the Con Penalty
if (iPenalty > iMaxConPenalty)
iPenalty = iMaxConPenalty;

if (GetHitDice(oRespawner) >= 5)
{
effect ePenalty = SupernaturalEffect(EffectAbilityDecrease(ABILITY_CONSTITUTION, iPenalty));
DelayCommand(0.2, ApplyEffectToObject(DURATION_TYPE_INSTANT, ePenalty, oRespawner));
SetCampaignInt(sCampaignName, "iPen", iPenalty, oRespawner);
}
}


There are 4 parts to defining this funtion. The first is Void

1. Void can be a difficult to figure out. What its saying is we are defining a function that does not return an assignable value anywhere else but within the function itself. That is the best way I can explain Void.

2. ApplyRespawnPenalty, This is the name (or Label) of the function and what most people call a function. Whenever you want to use a function in script ApplyRespawnPenalty is what you will say instead of writing the whole thing out. It would be used in another script in the module like this instance

DelayCommand(1.0, ApplyRespawnPenalty(oRespawner));

3. The third part of the funtion is the Parameters within the function works. Its easy to tell where the parameters are they are always in parentheses () and come directly after the lable of the function. In this example the parameters are (object oRespawner, int iStart = FALSE). If these conditions are not met then the function will not work. And yes the example script does not have iStart within. I will explain. The parameters defined are Object oRespawner is the object you are applying the function script too if int iStart is false. In the script only oRespawner is mentioned; That is because in this instance iStart is assumed to be false unless true so that parameter is a special parameter that is optional if there is no way iStart can be true within the script used. So the Parameters are what the function effects and under what conditions.

4. The last part of defining the function is the meat of the function script. It is what the function actually does when the parameters are met. It is everything included in the brackets below the function and Parameters.
CTP team member
http://www.harvestmoonconsortium.com
Chief cook and bottle washer for Harvest Moon

Post Reply