Script not working
TheTinman
Member Posts: 75
This script compiles fine but does not work. Intended to destroy all enemies when the PC exits an area.
void main()
{
object oPC = GetExitingObject();
object oCreature;
oCreature = GetNearestObject(OBJECT_TYPE_CREATURE);
while(GetIsObjectValid(oCreature))
{
if (GetIsEnemy(oPC, oCreature))
{
DestroyObject(oCreature);
}
oCreature = GetNextObjectInArea();
}
}
0
Comments
Always ask - what object is running this script?
In this case, it's the area.
So OBJECT_SELF - the default second parameter - is the area.
Since the area has no location, GetNearestObject will return OBJECT_INVALID.
If you specify oPC as the second parameter, it will work, but then you have to change GetNextObjectInArea to GetNearestObject, passing a parameter to get the 2nd, 3rd... instance until there are no more.
Alternatively, start with GetFirstObjectInArea. Test that the object is a creature before testing GetIsEnemy (for good order). Then GetNextObjectInArea is the correct way to continue the lóop.
https://nwnlexicon.com/index.php/GetObjectType
void main() { object oArea = OBJECT_SELF; object oPC = GetExitingObject(); object oCreature; oCreature = GetFirstObjectInArea(oArea, OBJECT_TYPE_CREATURE); while(GetIsObjectValid(oCreature)) { if (GetIsEnemy(oPC, oCreature)) { DestroyObject(oCreature); } oCreature = GetNextObjectInArea(oArea, OBJECT_TYPE_CREATURE); } }The lexicon is hard for me to understand at times. lol!
Try removing OBJECT_TYPE_CREATURE from both, then change the test to
if (GetObjectType(oCreature) == OBJECT_TYPE_CREATURE) if (GetIsEnemy(oPC, oCreature)) { DestroyObject(oCreature); }or if you prefer
if ((GetObjectType(oCreature) == OBJECT_TYPE_CREATURE) && GetIsEnemy(oPC, oCreature)) { DestroyObject(oCreature); }That's useful to know, thanks.