小寄巧

  • 模板加友元访问未被UPROPERTY标记的私有成员

    //比如要访问UGameFeaturesSubsystem中的GameFeaturePluginNameToPathMap
    template<auto M>
    struct SneakTunnel;
    
    template<class T, class U, T U::*M>
    struct SneakTunnel<M> 
    {
    	friend T& Sneak_GameFeaturesSubsystem(U& u) 
        {
    		return u.*M;
    	}
    };
    
    template struct SneakTunnel<&UGameFeaturesSubsystem::GameFeaturePluginNameToPathMap>;
    TMap<FString,FString>& Sneak_GameFeaturesSubsystem(UGameFeaturesSubsystem&);
    
    //使用方法
    auto& take_control = Sneak_GameFeaturesSubsystem(*Subsystem);
    take_control.Add(PluginName,PluginURL);
    
  • 2.0版本(一键失业)

    //宏定义
    #define ACCESS_MEMBER(ClassName, Member,MemberType) \
    template<auto M>\
    struct Accessor_##ClassName##Member;\
    \
    template<class T, class U, T U::*M>\
    struct Accessor_##ClassName##Member<M> {\
    friend T& Get##Member##From##ClassName(U& u) {\
    return u.*M;\
    }\
    };\
    \
    template struct Accessor_##ClassName##Member<&ClassName::Member>;\
    MemberType& Get##Member##From##ClassName(ClassName&);\
    
    
    //在项目的某个地方,我们要取出UGameInstance类的SubsystemCollection成员,它是个private 属性,它的类型是FObjectSubsystemCollection<UGameInstanceSubsystem>
    //直接写这么一行
    ACCESS_MEMBER(UGameInstance,SubsystemCollection,FObjectSubsystemCollection<UGameInstanceSubsystem>)
    //调用,这其实是UGameInstance::Init()的内容,但是我想要自定义初始化,只能这样了
    GetSubsystemCollectionFromUGameInstance(*this).Initialize(this);
    
    

接口