Skip to content

Script not working

TheTinmanTheTinman Member Posts: 72
edited October 2023 in Builders - Scripting
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();

}

}

Comments

  • ProlericProleric Member Posts: 1,286
    The problem is GetNearestObject.

    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.
  • TheTinmanTheTinman Member Posts: 72
    edited October 2023
    Ok. I switched lines 9 and 23 to GetFirstObjectInArea and GetNextObjectInArea and it destroyed all the objects in the area. I understand why it did. How do I write the script to check that the objects to sort through are only hostile creatures?
  • meaglynmeaglyn Member Posts: 149
  • TheTinmanTheTinman Member Posts: 72
    I just got it working. With Prolerics explanation and a look through the nwnlexicon.
    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!
  • ProlericProleric Member Posts: 1,286
    edited October 2023
    I'd be surprised if that compiles. GetFirst/NextObjectInArea doesn't take a second parameter.

    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);
        }
    
  • TheTinmanTheTinman Member Posts: 72
    I tested it just before I posted. It ran fine. Destroyed all hostile creatures and left all non hostile untouched.
  • meaglynmeaglyn Member Posts: 149
    The second parameter is new in EE.
  • MelkiorMelkior Member Posts: 181
    meaglyn wrote: »
    The second parameter is new in EE.

    That's useful to know, thanks.
Sign In or Register to comment.