Difference between revisions of "GetIsObjectValid"

From NWN Lexicon
Jump to: navigation, search
 
(One intermediate revision by one other user not shown)
Line 1: Line 1:
 
+
{{DISPLAYTITLE:GetIsObjectValid(object)}}__NOTOC__
 
 
 
 
 
 
=GetIsObjectValid(object)=
 
 
 
 
Determines whether an object is valid.
 
Determines whether an object is valid.
 
 
<nwscript>
 
<nwscript>
 
int GetIsObjectValid(
 
int GetIsObjectValid(
Line 15: Line 9:
 
====Parameters====
 
====Parameters====
  
''oObject''
+
;oObject
 
+
:The object to check the validity of.
The object to check the validity of.
 
  
<br />
 
  
 
====Description====
 
====Description====
  
Returns TRUE if oObject is a valid object, otherwise FALSE. <br />
+
Returns TRUE if oObject is a valid object, otherwise FALSE.
  
<br /><br />
 
  
 
====Remarks====
 
====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. <br /><br /> Most of the time, this will be the same as comparing oObject to OBJECT_INVALID. There may be times where it isn't though, so it is generally recommended that you use this function.
+
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..
 +
 
 +
<nwscript>
 +
object o = GetLocalObject(oModule, "someitem");
 +
o != OBJECT_INVALID
 +
GetIsObjectValid(o) == FALSE
 +
</nwscript>
  
<br />
+
So GetIsObjectValid() is always 'safer', but 'slower'
  
 
====Version====
 
====Version====
Line 42: Line 55:
 
void main()
 
void main()
 
{
 
{
//Loop through all PC's in a module:
+
    //Loop through all PC's in a module:
object oPC=GetFirstPC();
+
    object oPC=GetFirstPC();
  
while (GetIsObjectValid(oPC))
+
    while (GetIsObjectValid(oPC))
  {
+
    {
  SendMessageToPC(oPC, "You're a player, aren't you? ;-)");
+
        SendMessageToPC(oPC, "You're a player, aren't you? ;-)");
  oPC=GetNextPC();
+
        oPC = GetNextPC();
  }
+
    }
 
}
 
}
 
</nwscript>
 
</nwscript>

Latest revision as of 10:55, 17 September 2020

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