Really in nwn to script you just need an understanding of how the flow of nwn is working. Instead of telling you to go to tutorials I'll go ahead and explain basically all the "hard parts" of scripting that snagged me before I overcame them. The actual use of functions is well... Explained... By BioWare in the functions comments. Any other information then the explanations by BioWare about the functions can be found in nwnlexicon in well detailed format.
1). In order to apply an effect you need to first create an object to apply it to(which is well explained by BioWare in the functions or nwnlexicon) and then create said effect and then use ApplyEffectToObject or ApplyEffectAtLocation, Examples:
object oEnter = GetEnteringObject(); //Useful for anytime an animate thing is entering something.
effect eHeal = EffectHeal(100); //100 is just a number I stuck in there and the amount to heal.
ApplyEffectToObject(DURATION_TYPE_INSTANT, eHeal, oEnter);
Note that I first declared the object it's being applied to and then created the effect to apply to it and then applied the effect. Duration types are ultimately logical and thus I didn't go into detail as to why I chose instant(it's something that has no duration).
In order to apply something to a location we must first HAVE a location. Well declaring a location is also explained quite well by BioWare in many functions. A good example however is this:
location lSelf = GetLocation(OBJECT_SELF);
effect eExplosion = EffectVisualEffect(VFX_FNF_BLINDDEAF );
ApplyEffectAtLocation(DURATION_TYPE_INSTANT, eExplosion, lSelf);
Effects applied at locations are mostly visuals with the one exception being persistent AreaOfEffect's.
---------------------------------------------LOOPS---------------------------------------------------------------------------------------------------
As can be expected you can't always know how many things are going to be impacted by a particular script. So in order to handle any number of objects you have to use a loop.
NWN supports all standard loop methods, while, for, and do while. If you put a "return;" statement within a loop, it will cancel it. Basically the return statement tells a computer that the goal of the script has been accomplished and there is no more reason to run it.
A while loop is actually quite simple(despite HOW MANY TIMES I caused TMI errors because I couldn't comprehend the rules of it, despite how obvious the rules are). Example:
object oPC = GetFirstPC();
while(GetIsObjectValid(oPC)) //or you can use oPC != OBJECT_INVALID, not sure if there is a notable difference.
{
SendMessageToPC(oPC, "Hi!");
if(GetName(oPC) == "Smarty")
{
SendMessageToPC(oPC, "Smarty!");
}
oPC = GetNextPC();
}
The condition put into the brackets of the "while" statement are it's basis for how many times the loop should itinerate. In this case the while is going to keep looping until oPC becomes invalid.(runs out of PCs). Any statements put inside of a while loop will be repeated everytime the loop makes it's loop. The MOST IMPORTANT RULE WITH A WHILE LOOP is to make sure the condition in the brackets EVENTUALLY becomes wrong(you eventually want there to be no more PCs left in this situation.
A for loop is much simpler, but less universal because it has a set number of times you will make it loop. For instance to perform the function we did with the while would require having the server's onenter/onexit events setting a local variable that lets us know how many PCs are in the server before we perform the for loop. Thus a for loop is mostly used by programmers simply for allowing them to repeat an action multiple times without typing it ALL THOSE TIMES(Yay laziness!). NWN has one difference in it's for loop in that the initializing variable must be declared outside of the loop. Example of for:
int i;
int nMonkey = 2;
for(i=0; i<=nMonkey; i++)
{
if(i>=2)
SendMessageToAllDMs("OMG THERE IS"+IntToString(i)+" MONKEYS HERE!!!!!!!");
}
The i=0 is the initializing variable. This is the starting point of the for loop. The i<=nMonkey is saying that as long as nMonkey is greater in value then i that we will execute this script. The final part which is i++ simply means
i = i+1 and essentially this is increasing the starting number by 1 each time. The goal is that eventually the condition in the middle of the for's brackets will become wrong. The i++ is done at the completion of each loop.
The Do While loop I'm actually a bit rusty on myself because it's never really been something I've needed to use >.>, but still I'm quite certain I know how it works. Essentially it's a while loop that performs each block of code FIRST and THEN checks the condition. Example:
object oPC = GetFirstPC();
do
{
SendMessageToPC(oPC, "Got your nose!");
oPC = GetNextPC();
}
while(oPC != OBJECT_INVALID); //Note the semicolon there.
Same mechanics as while, but it executes first and then checks if it should continue.
If you want to know more then just let me know and I can explain some further possible snags for you.
Modifié par Highv Priest, 11 mars 2013 - 06:36 .