UE4的Cast方法与类型转化

UE4中,我们经常会用到Cast函数,作用是强制转化一个指针到我们需要的类型,并且如果不能强制转化,就会返回nullptr。通过判断转换之后是不是nullptr,我们也能实现很多有用的功能(比如用于判断子弹击中的到底是什么)
那么Cast到底是如何实现的呢?要看Cast,首先要看到一个类型,叫做UClass,他是实现UE类型识别的重要类型。
我们经常打交道的类型都是继承自UObjectBase的,UObjectBase是UE的一个基础类型,内部有一个UClass类型的字段Class,识别类型的重要方法HasAnyCastFlag就是UClass的方法,我们看看HasAnyCastFlag的具体实现:

FORCEINLINE bool HasAnyCastFlag(EClassCastFlags FlagToCheck) const
    {
        return (ClassCastFlags&FlagToCheck) != 0;
    }

我们可以看到,他的代码很简单,就是对比了参数FlagToCheck和自己一个叫ClassCastFlags的字段,这两个对象的类型是EClassCastFlags,其实这个类型很简单,就是int64。而两个类型到底是不是可以强制转化,完全取决于自己这个int64的flag字段和对方int64 flag字段按位与是否等于0。紧接这这个函数实现,就是UE4内部类型的转换关系:

#define CASTCLASS_None                          DECLARE_UINT64(0x0000000000000000)
#define CASTCLASS_UField                        DECLARE_UINT64(0x0000000000000001)
#define CASTCLASS_UInt8Property                 DECLARE_UINT64(0x0000000000000002)
#define CASTCLASS_UEnum                         DECLARE_UINT64(0x0000000000000004)
#define CASTCLASS_UStruct                       DECLARE_UINT64(0x0000000000000008)
#define CASTCLASS_UScriptStruct                 DECLARE_UINT64(0x0000000000000010)
#define CASTCLASS_UClass                        DECLARE_UINT64(0x0000000000000020)
#define CASTCLASS_UByteProperty                 DECLARE_UINT64(0x0000000000000040)
#define CASTCLASS_UIntProperty                  DECLARE_UINT64(0x0000000000000080)
#define CASTCLASS_UFloatProperty                DECLARE_UINT64(0x0000000000000100)
#define CASTCLASS_UUInt64Property               DECLARE_UINT64(0x0000000000000200)
#define CASTCLASS_UClassProperty                DECLARE_UINT64(0x0000000000000400)
#define CASTCLASS_UUInt32Property               DECLARE_UINT64(0x0000000000000800)
#define CASTCLASS_UInterfaceProperty            DECLARE_UINT64(0x0000000000001000)
#define CASTCLASS_UNameProperty                 DECLARE_UINT64(0x0000000000002000)
#define CASTCLASS_UStrProperty                  DECLARE_UINT64(0x0000000000004000)
#define CASTCLASS_UProperty                     DECLARE_UINT64(0x0000000000008000)
#define CASTCLASS_UObjectProperty               DECLARE_UINT64(0x0000000000010000)
#define CASTCLASS_UBoolProperty                 DECLARE_UINT64(0x0000000000020000)
#define CASTCLASS_UUInt16Property               DECLARE_UINT64(0x0000000000040000)
#define CASTCLASS_UFunction                     DECLARE_UINT64(0x0000000000080000)
#define CASTCLASS_UStructProperty               DECLARE_UINT64(0x0000000000100000)
#define CASTCLASS_UArrayProperty                DECLARE_UINT64(0x0000000000200000)
#define CASTCLASS_UInt64Property                DECLARE_UINT64(0x0000000000400000)
#define CASTCLASS_UDelegateProperty             DECLARE_UINT64(0x0000000000800000)
#define CASTCLASS_UNumericProperty              DECLARE_UINT64(0x0000000001000000)
#define CASTCLASS_UMulticastDelegateProperty    DECLARE_UINT64(0x0000000002000000)
#define CASTCLASS_UObjectPropertyBase           DECLARE_UINT64(0x0000000004000000)
#define CASTCLASS_UWeakObjectProperty           DECLARE_UINT64(0x0000000008000000)
#define CASTCLASS_ULazyObjectProperty           DECLARE_UINT64(0x0000000010000000)
#define CASTCLASS_UAssetObjectProperty          DECLARE_UINT64(0x0000000020000000)
#define CASTCLASS_UTextProperty                 DECLARE_UINT64(0x0000000040000000)
#define CASTCLASS_UInt16Property                DECLARE_UINT64(0x0000000080000000)
#define CASTCLASS_UDoubleProperty               DECLARE_UINT64(0x0000000100000000)
#define CASTCLASS_UAssetClassProperty           DECLARE_UINT64(0x0000000200000000)
#define CASTCLASS_UPackage                      DECLARE_UINT64(0x0000000400000000)
#define CASTCLASS_ULevel                        DECLARE_UINT64(0x0000000800000000)
#define CASTCLASS_AActor                        DECLARE_UINT64(0x0000001000000000)
#define CASTCLASS_APlayerController             DECLARE_UINT64(0x0000002000000000)
#define CASTCLASS_APawn                         DECLARE_UINT64(0x0000004000000000)
#define CASTCLASS_USceneComponent               DECLARE_UINT64(0x0000008000000000)
#define CASTCLASS_UPrimitiveComponent           DECLARE_UINT64(0x0000010000000000)
#define CASTCLASS_USkinnedMeshComponent         DECLARE_UINT64(0x0000020000000000)
#define CASTCLASS_USkeletalMeshComponent        DECLARE_UINT64(0x0000040000000000)
#define CASTCLASS_UBlueprint                    DECLARE_UINT64(0x0000080000000000)
#define CASTCLASS_UDelegateFunction             DECLARE_UINT64(0x0000100000000000)
#define CASTCLASS_UStaticMeshComponent          DECLARE_UINT64(0x0000200000000000)
#define CASTCLASS_UMapProperty                  DECLARE_UINT64(0x0000400000000000)

#define CASTCLASS_AllFlags                      DECLARE_UINT64(0xFFFFFFFFFFFFFFFF)

我们新建的类,继承自这些基类,自然也会获得一个ClassCastFlags,用来控制转化关系。
比如我们声明一个继承自APawn的类,然后再Tick中访问UClass的ClassCastFlags字段,得到的值是343597383680,转化为2进制就是0x0000005000000000,比define的CASTCLASS_APawn刚好大一,相与不为0,可以转化。
到这里就比较清楚了,Cast函数内部调用了一个叫DoCast的函数。而DoCast调用了我们上边看到的HasAnyCastFlag:

FORCEINLINE static To* DoCast( UObject* Src )
    {
        return Src && Src->GetClass()->HasAnyCastFlag(TCastFlags<To>::Value) ? (To*)Src : nullptr;
    }

你可能感兴趣的:(c++,UE4瞎讲)