OBJECT INVALID

From NWN Lexicon
Jump to: navigation, search

OBJECT_INVALID is the engine constant for an invalid object reference. This is used when an object is first referenced, eg:

object oPC; // This reference is OBJECT_INVALID
 
object oObject = GetObjectByTag("INVALID_TAG"); // If it fails to find an object tagged INVALID_TAG it returns OBJECT_INVALID
 
// Loops you need to check for if an object is invalid else it will cause a Too Many Instructions error
object oObject = GetFirstObjectInShape(SHAPE_SPHERE, 5.0, lTarget);
while(GetIsObjectValid(oObject))
{
    // Do stuff to oObject...
 
    // Get next one that is then checked with the above GetIsObjectValid call
    oObject = GetNextObjectInShape(SHAPE_SPHERE, 5.0, lTarget);
}

There are two main tests for object validity, GetIsObjectValid and != OBJECT_INVALID:

if(GetIsObjectValid(oObject)) { }
if(oObject != OBJECT_INVALID) { }

They differ slightly since GetIsObjectValid will check if the object really exists. This is important for several reasons; if you have set an object local with SetLocalObject it might be it returns a value that isn't OBJECT_INVALID but the object was destroyed or has left (eg if a PC) and therefore only GetIsObjectValid works.

The use case of SQL and ObjectToString/StringToObject also will require the use of GetIsObjectValid since you can pass any string into StringToObject and it won't know if it is valid or not.

There are very very minor performance differences when using GetIsObjectValid, so in most scripts it is recommended just to use it to cover all edge cases and not to prematurely optimise.