Author Topic: GetNearestObject - specifying more than one type  (Read 282 times)

Legacy_henesua

  • Hero Member
  • *****
  • Posts: 6519
  • Karma: +0/-0
GetNearestObject - specifying more than one type
« on: November 26, 2011, 01:25:44 am »


                When using GetNearestObject is it ossible to specify more than one type of object to look for?

I have run across this in a script:
GetNearestObject((OBJECT_TYPE_TRIGGER | OBJECT_TYPE_DOOR), OBJECT_SELF, Num)

would this also work?
GetNearestObject((OBJECT_TYPE_TRIGGER + OBJECT_TYPE_DOOR), OBJECT_SELF, Num)

               
               

               
            

Legacy_WhiZard

  • Hero Member
  • *****
  • Posts: 2149
  • Karma: +0/-0
GetNearestObject - specifying more than one type
« Reply #1 on: November 26, 2011, 03:12:56 am »


               The + would work.  However OBJECT_TYPE_TRIGGER + OBJECT_TYPE_TRIGGER would return a completely different object while the bit-wise or (|) would render the trigger object as expected.  What is being used is a binary number whose binary digits (bits) are being used to represent the checking of an object type (1 means check for it, 0 means do not check).  Thus all OBJECT_TYPE data is a different power of two, thus a different binary digit.

Example: 16 (which is 10000 in binary) acknowledges only one object while 15 (1111 in binary) acknowledges four objects.
               
               

               
            

Legacy_henesua

  • Hero Member
  • *****
  • Posts: 6519
  • Karma: +0/-0
GetNearestObject - specifying more than one type
« Reply #2 on: November 26, 2011, 03:19:55 am »


               Thanks. I did not know that you could specify more than one object type at a time to look for.
               
               

               
            

Legacy_FunkySwerve

  • Hero Member
  • *****
  • Posts: 2325
  • Karma: +0/-0
GetNearestObject - specifying more than one type
« Reply #3 on: November 26, 2011, 05:07:57 am »


               Actually, so long as the numbers involved are single bits, as object types are, both the | and the + will yeild the same result. The + isn't good coding practice, though, since it relies on that hidden assumption.

Funky
               
               

               


                     Modifié par FunkySwerve, 26 novembre 2011 - 05:08 .
                     
                  


            

Legacy_Lightfoot8

  • Hero Member
  • *****
  • Posts: 4797
  • Karma: +0/-0
GetNearestObject - specifying more than one type
« Reply #4 on: November 26, 2011, 05:32:10 am »


               Just to expand on what Whizard said a little.  If you look in NWScript.nss you can see what the OBJECT_TYPE_* constants are equal to. 

int    OBJECT_TYPE_CREATURE         = 1;
int    OBJECT_TYPE_ITEM             = 2;
int    OBJECT_TYPE_TRIGGER          = 4;
int    OBJECT_TYPE_DOOR             = 8;
int    OBJECT_TYPE_AREA_OF_EFFECT   = 16;
int    OBJECT_TYPE_WAYPOINT         = 32;
int    OBJECT_TYPE_PLACEABLE        = 64;
int    OBJECT_TYPE_STORE            = 128;
int    OBJECT_TYPE_ENCOUNTER        = 256;
int    OBJECT_TYPE_ALL              = 32767;

So his check for object type 15,    would be OBJECT_TYPE_CREATURE | OBJECT_TYPE_ITEM | OBJECT_TYPE_TRIGGER | OBJECT_TYPE_DOOR    each one of them being represented by one of the first 4 bits:
1 | 2 | 4 | 8  = 15;

The method can also be used in writing your own custom functions.  

simple example.

void DestroyAllObjectsInAreaByType(  int nObjectFilter)
{
  object oObject =GetFirstObjectInArea();
  while(GetIsObjectValid(oObject)) 
 {
    if (GetObjectType(oObject) &  nObjectFilter)  DestroyObject(oObject);
    oObject =GetNextObjectInArea();

 }
}

with the function above
DestroyAllObjectsInAreaByType(  15 );

would destroy all creatures, items, triggers and doors.