Aller au contenu

Photo

GetNearestObject - specifying more than one type


  • Veuillez vous connecter pour répondre
4 réponses à ce sujet

#1
henesua

henesua
  • Members
  • 3 883 messages
 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)


#2
WhiZard

WhiZard
  • Members
  • 1 204 messages
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.

#3
henesua

henesua
  • Members
  • 3 883 messages
Thanks. I did not know that you could specify more than one object type at a time to look for.

#4
FunkySwerve

FunkySwerve
  • Members
  • 1 308 messages
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 .


#5
Lightfoot8

Lightfoot8
  • Members
  • 2 535 messages
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.