What exactly is "CastFlags"?

I am just curious about what exactly CastFlags are.
I have a use case where I want to be able to switch on the FFieldClass of a given FProperty without just manually using CastField or comparing to the ::StaticClass(). I noticed that CastField (and I think regular Cast too) makes use of some mysterious field called CastFlags to determine if a given object is a valid static_cast candidate for the desired destination type, and I have a hunch that I may be able to leverage this information to accomplish what I need.

My questions:

  • CastFlags could theoretically just be a unique integer assigned to every class, but giving this field uint64 range for that seems like overkill to me and leads me to believe there is more information in this field than just “what is this class”. What all metadata is compacted into this innocent integer?

  • Given an arbitrary uint64 value, if treated as though it was some class’s CastFlags, are there mechanisms to work backwards and determine what class that value would map to? (I am assuming no two classes would have the same CastFlags) Is it possible to get the associated class for any perceivable CastFlags value?

Hi! It is special meta data, that is added to each UCLASS by code generator. The main purpose for them is to skip cast when you want only to know if that thing is child of another. Also, they can be used to implement some safe implementation of cast - you check flags first and only after that cast type…

Thanks for the overview. Do you know if there is some straightforward way to derive the UCLASS given some arbitrary CastFlags that doesn’t require basically just guessing?

Hi again! The answer to your question is simple. Just use smth like this

  • Prop->GetClass()->ClassCastFlags & CASTCLASS_UDelegateProperty to check flags

  • Also there is something very similar and useful with props - EPropertyFlags. It can help you to check the same for Props child classes

  • One more thing - as I said in prev message, engine Casts are already use that ckecks to be more effective. So, what is the case you are talking? It’s very interesting )