ThingCategory.Pawn vs is Pawn

Started by NoImageAvailable, June 12, 2015, 09:49:13 AM

Previous topic - Next topic

NoImageAvailable

Looking through the changes for A11 I noticed that in some places you see code like
this.currentTarget.Thing.def.category == ThingCategory.Pawn
and in others its
target.Thing is Pawn

As far as I can tell both pieces of code check to see if a thing is a pawn, so why have both? What's the difference between the two?
"The power of friendship destroyed the jellyfish."

Supes

The first one is comparing 2 Objects

where as the second one is comparing Class Type

NoImageAvailable

Quote from: Supes on June 12, 2015, 11:59:03 PM
The first one is comparing 2 Objects

where as the second one is comparing Class Type

Yes, but what's the difference in practice? Both take a Thing and return true if it's a pawn or is there ever a case where Thing.def.category == ThingCategory.Pawn is true/false but not target.Thing is Pawn?
"The power of friendship destroyed the jellyfish."

1000101

There is a technical difference but no practical difference.  It would all depend on the circumstances as to which method you should use.  For example, you may need to resolve the object class type before you could access data elements/member functions.  That being said, most of the time you can make certain assumptions of the object class type unless you are writing a sufficiently "low" level function where the object may be ambiguous until resolved.  In the context of RimWorld modding this is usually not the case but there will be times when you will need to verify the object class before access.
(2*b)||!(2*b) - That is the question.
There are 10 kinds of people in this world - those that understand binary and those that don't.

Powered By

Tynan

Really, I should use the category in every case. I shouldn't use "is" because it's possible I or someone else might want to have a Thing that is of category Pawn but is not actually of class Pawn (e.g. a modder with a subclass of Pawn).
Tynan Sylvester - @TynanSylvester - Tynan's Blog

isistoy

Actually, our main class is inherited from it, in K9
<Stay on the scene like a State machine>