Quick Question: IsA or Casting?

  • Two Factor Authentication is now available on BeyondUnreal Forums. To configure it, visit your Profile and look for the "Two Step Verification" option on the left side. We can send codes via email (may be slower) or you can set up any TOTP Authenticator app on your phone (Authy, Google Authenticator, etc) to deliver codes. It is highly recommended that you configure this to keep your account safe.

GuruKnight

New Member
May 7, 2004
53
0
0
Which would be better if you have an actor and want to see if its a certain object?

if(myObj.IsA('MyObject'))


OR

if(MyObject(myObj) != none)

Is there any difference? I think the second would be better because you find 2 things from it. 1 is that its your object and the 2 is whether it is none or not. Does the first method also does both these things?
 

Winged Unicorn

From wishes to eternity
The first method (IsA) just checks if the given Object has a certain class among its parent classes:

Code:
inline UBOOL UObject::IsA( class UClass* SomeBase ) const
{
	guardSlow(UObject::IsA);
	for( UClass* TempClass=Class; TempClass; TempClass=(UClass*)TempClass->SuperField )
		if( TempClass==SomeBase )
			return 1;
	return SomeBase==NULL;
	unguardobjSlow;
}


The second method (dynamic cast) is definately safer, because it first makes sure that the pointer to the given Object is valid, then it internally calls the first method (IsA) itself:

Code:
template<class T> T* Cast( UObject* Src )
{
	return Src && Src->IsA(T::StaticClass()) ? (T*)Src : NULL;
}


Therefore, use the second method any time you can. Use the first one only when all you have is a class name and not an actual reference.
 
Last edited: