GetIsObjectValid(object)

From NWN Lexicon
Jump to navigationJump to search

Determines whether an object is valid.

int GetIsObjectValid(
    object oObject
);

Parameters

oObject
The object to check the validity of.


Description

Returns TRUE if oObject is a valid object, otherwise FALSE.


Remarks

This function is a widely used utility function to check the return values for functions that return objects. While looping through objects NWN functions usually return invalid objects to signify that there are no more objects left. This function can be used to test that condition.

Most of the time, this will be the same as comparing oObject to OBJECT_INVALID (which is slightly faster). However there are several ways to generate an object ID which references a not valid game reference (eg: StringToObject, or GetLocalObject), so it is generally recommended that you use this function.

OBJECT_INVALID versus GetIsObjectValid

Related discussion from #nwnx Discord

--HA-- Can there be a difference in result between comparing OBJECT_INVALID and calling GetIsObjectValid or would it always be a bit better to use OBJECT_INVALID?

clippy They are very different. object is actually a numerical ID of the object. OBJECT_INVALID is a numerical constant that objects will never have (0x7f000000). Comparing against OBJECT_INVALID is only comparing against that particular constant. GetIsObjectValid() actually tries to look up the object by ID and return whether or not it exists in the world. For example, if you do SetLocalObject(oModule, "someitem", oSomeItem), and then destroy that item..

object o = GetLocalObject(oModule, "someitem");
o != OBJECT_INVALID
GetIsObjectValid(o) == FALSE

So GetIsObjectValid() is always 'safer', but 'slower'

Version

1.61

Example

void main()
{
    //Loop through all PC's in a module:
    object oPC=GetFirstPC();

    while (GetIsObjectValid(oPC))
    {
        SendMessageToPC(oPC, "You're a player, aren't you? ;-)");
        oPC = GetNextPC();
    }
}

See Also



 author: Tom Cassiotis, editor: Lilac Soul