Author Sticky: Post Working Scripts Here
Jacmac Posted: Friday, 28 June 07:12PM
1. _bm062_autolight (Automatic day/night lighting)

2. This script will automatically turn the lights on or off designated placeable light objects depending on time of day. See the script comments for details.

NWScript:
/*
Filename:
_bm062_autolight
Author:
Brian McMahon
brian@net70.com
Purpose:
This script will automatically turn on/off designated placeable light objects when
day/dawn - night/dusk changes occur. It will also set the correct light setting
on designated placeable light objects upon entering an Area.
Requirements:
Any area with day/night cycling and one or more placeable light objects, such as a
torch bracket, lamp post, shaft of light, etc. Light objects to be automated must
have a tag in the format of AutoLight_xx, where xx is a number sequence beginning
with 00. The sequence is critical; if the sequence is broken, the command will
break with the last valid object/tag in the sequence. In other words if you setup
an area and then delete one of the lights in the middle of the sequence, you must
resequence the remaining objects. A better way is to delete the last object and
move/change the one you were going to delete into the last object place. It goes
with out saying, but I'll say it anyway, the range limit is 00-99. Also, the
static box must be UNCHECKED on all of the AutoLight_xx objects you create!
Usage:
This file is an include file, it should be saved only, not compiled. Errors will
be generated if compiled because there is no main() function here.
The command "_BM062_AutoLightsStateReset()" should be added to an Area Enter Event
script, for example:
//#include "_bm062_autolight"
//
//void main(){
//
//    _BM062_AutoLightsStateReset();
//
//    ... other code in Area Enter Event, if any ...
//
//    }
The command "_BM062_AutoLights()" should be added to an Area Heartbeat Event script,
in the same manner as the example above.
Known Issues:
There is a bug in the way the NWN engine is dealing with placeable light objects,
specifically with the Illumination property. For some reason, illumination can
come from deactivated objects under certain conditions. This behavior is not due
to this script, in fact, the behavior can be easily replicated with no script at
all in a 'clean' test module. Hopefully Bioware will fix this soon.
FAQ:
Q. What is with all the leading underscores?
A. That is how I sort (and I literally mean SORT) custom work from built-in
stuff.
Q. What is the BMxxx in the names all about?
A. Uniqueness, I actually took the trouble to assume you or someone else might
have used a name like just "_AutoLights". Edit it out if you hate it.
Q. Why is this an include file instead of a normal event script?
A. When you get tired of cut and pasting work from many sources into a single
event script and all the trouble it can cause, you will have answered your
own question.
*/

void _BM062_AutoLightsStateReset(){
    object oFirstLight = GetObjectByTag("_AutoLight_00");
    SetLocalInt(oFirstLight, "NightSet", 0);
    SetLocalInt(oFirstLight, "DaySet", 0);
    }
void _BM062_AutoLights(){
    int x;
    object oPlaceableLight;
    //oFirstLight used to store current lighting state so that the constant
    //heartbeat events don't cause constant unnecessary execution of the
    //bulk of the code.
    object oFirstLight = GetObjectByTag("_AutoLight_00");
    object oCurrentArea = GetArea(oFirstLight);
    //*********** NIGHT / DUSK CODE *************
    //If it's Night and the lights haven't been turned on yet, time to turn them on.
    if ((GetIsNight() == TRUE || GetIsDusk() == TRUE) && GetLocalInt(oFirstLight, "NightSet") == 0){
        SetLocalInt(oFirstLight, "NightSet", 1);
        SetLocalInt(oFirstLight, "DaySet", 0);
        for (x=0;x < 100;x++){
            //Tack on '0' if no tens digit
            if (x < 10) oPlaceableLight = GetObjectByTag("_AutoLight_0" + IntToString(x));
                else oPlaceableLight = GetObjectByTag("_AutoLight_" + IntToString(x));
            //Check if object exists. If not, break out of the loop, there are no more.
            if (GetIsObjectValid(oPlaceableLight) != TRUE) break;
            //Activate light object.
            AssignCommand(oPlaceableLight, PlayAnimation(ANIMATION_PLACEABLE_ACTIVATE));
            SetPlaceableIllumination(oPlaceableLight, TRUE);
            //Set for compatiblity with on/off script, not used by this script.
            SetLocalInt(oPlaceableLight, "NW_L_AMION", 1);
            }
        //Recompute area lighting after all animations and illuminations are set.
        RecomputeStaticLighting(oCurrentArea);
        }
    //********** DAY / DAWN CODE ************
    //If it's Day and the lights haven't been turned off yet, time to turn them off.
    if ((GetIsDay() == TRUE || GetIsDawn() == TRUE) && GetLocalInt(oFirstLight, "DaySet") == 0){
        SetLocalInt(oFirstLight, "NightSet", 0);
        SetLocalInt(oFirstLight, "DaySet", 1);
        for (x=0;x < 100;x++){
            //Tack on '0' if no tens digit
            if (x < 10) oPlaceableLight = GetObjectByTag("_AutoLight_0" + IntToString(x));
                else oPlaceableLight = GetObjectByTag("_AutoLight_" + IntToString(x));
            //Check if object exists. If not, break out of the loop, there are no more.
            if (GetIsObjectValid (oPlaceableLight) != TRUE) break;
            //Deactivate light object.
            SetPlaceableIllumination(oPlaceableLight, FALSE);
            AssignCommand(oPlaceableLight, PlayAnimation(ANIMATION_PLACEABLE_DEACTIVATE));
            //Set for compatiblity with on/off script, not used by this script.
            SetLocalInt(oPlaceableLight, "NW_L_AMION", 0);
            }
        //Recompute area lighting after all animations and illuminations are set.
        RecomputeStaticLighting(oCurrentArea);
        }
    }