(sub)Class compare && Instance compare

Discussions about Coding and Scripting
Post Reply
User avatar
Barbie
Godlike
Posts: 2802
Joined: Fri Sep 25, 2015 9:01 pm
Location: moved without proper hashing

(sub)Class compare && Instance compare

Post by Barbie »

By trying to enable multiple teams in MH and walking through the code I found the following UT99 stock code which confuses me:

Code: Select all

class Translocator extends TournamentWeapon;

var TranslocatorTarget TTarget;
[...]

function bool HandlePickupQuery( inventory Item )
{
	if ( Item.IsA('TranslocatorTarget') && (item == TTarget) )
	[...]
}
If the condition item == TTarget is True then the condition Item.IsA('TranslocatorTarget') is always true so that the clause can be reduced to item == TTarget. Is the original clause just redundant or did I run into a pitfall here?
"Multiple exclamation marks," he went on, shaking his head, "are a sure sign of a diseased mind." --Terry Pratchett
User avatar
Wormbo
Adept
Posts: 258
Joined: Sat Aug 24, 2013 6:04 pm
Contact:

Re: (sub)Class compare && Instance compare

Post by Wormbo »

Reference equality implies type equality. If you really only care about whether you see the same thing, you don't have to check its type at all, because you already know it.

Also, a word about that IsA() function: Don't use it.
It doesn't provide any compile-time type-safety or sanity validation whatsoever. If you want to check an instance's type, do a typecast-null check (i.e. "TranslocatorTarget(Item) != None"), because then the compiler can already tell you when there's a typo in the class name or when your check can never work out as the specified type and the variable's type are unrelated to each other. Also note that IsA() is a function call like any other and thus can bring up Accessed none errors at runtime, unlike a typecast operation.
User avatar
Barbie
Godlike
Posts: 2802
Joined: Fri Sep 25, 2015 9:01 pm
Location: moved without proper hashing

Re: (sub)Class compare && Instance compare

Post by Barbie »

Wormbo wrote:IsA() function: Don't use it.
It doesn't provide any compile-time type-safety or sanity validation whatsoever.
Yes, I've seen things like this in foreign code:

Code: Select all

if(Killer.IsA('ScreptedPawn') && Other.IsA('ScreptedPawn'))return;
Wormbo wrote:If you want to check an instance's type, do a typecast-null check
That's a nice alternative, thanks for the hint.
"Multiple exclamation marks," he went on, shaking his head, "are a sure sign of a diseased mind." --Terry Pratchett
JackGriffin
Godlike
Posts: 3774
Joined: Fri Jan 14, 2011 1:53 pm
Personal rank: -Retired-

Re: (sub)Class compare && Instance compare

Post by JackGriffin »

Depending on the usage this check works very well also

Code: Select all

  if(Item.Class==Class'Whatever')
Also you'll find

Code: Select all

if(Killer.IsA('ScreptedPawn') && Other.IsA('ScreptedPawn'))return;
in my MH controllers. I know it's a misprint from when I copied and learned from UTJMH. The original coder was Japanese and I just liked it so much I left it in. It's certainly not a valid class.
So long, and thanks for all the fish
Post Reply