Files
.obsidian
.vs
00-MOC
01-Diary
02-Note
03-UnrealEngine
Animation
Editor
Gameplay
AI
Animation
Code
Debug
GAS
Gameplay
Http
Lyra
Mass
Online
Other
PuerTS
UObject
UnrealSpecifiers
Flags
Meta
Actor
AnimationGraph
Asset
Blueprint
AdvancedDisplay
AllowPrivateAccess
AllowPrivateAccess.md
Untitled.png
BlueprintAutocast
BlueprintPrivate
BlueprintProtected
BlueprintThreadSafe
CallableWithoutWorldContext
CommutativeAssociativeBinaryOperator
CompactNodeTitle
DefaultToSelf
DontUseGenericSpawnObject
Exec
ExposeOnSpawn
ExposedAsyncProxy
ForceAsFunction
HasDedicatedAsyncNode
HiddenNode
HideThen
IgnoreTypePromotion
Keywords
KismetHideOverrides
Latent
NativeMakeFunc
NotInputConfigurable
ObjectSetType
Param
ProhibitedInterfaces
RestrictedToClasses
ReturnDisplayName
SetParam
ShowWorldContextPin
UnsafeDuringActorConstruction
WorldContext
BlueprintGetter.md
BlueprintInternalUseOnly.md
BlueprintInternalUseOnlyHierarchical.md
BlueprintSetter.md
BlueprintType.md
CPP_Default_XXX.md
CallInEditor.md
CannotImplementInterfaceInBlueprint.md
DisplayName.md
GetByRef.md
HideFunctions.md
IsBlueprintBase.md
IsConversionRoot.md
NativeBreakFunc.md
NativeConst.md
NotBlueprintThreadSafe.md
SparseClassDataTypes.md
Variadic.md
Component
Config
Container
Debug
DetailsPanel
Development
Enum
GAS
Material
Niagara
Numeric
Object
Path
Pin
RigVM
Scene
Script
Sequencer
Serialization
SparseDataType
String
Struct
TypePicker
UHT
Widget
Meta.md
Specifier
UnrealSpecifiers.md
Ue4 c++ UProperty反射 PostEditChangeProperty.md
Ue4Object生命周期.jpg
大钊提供的一种获取UE Private函数的方法.md
LevelScene
Math
Mobile
Physical
Plugins
Rendering
Sequence
UI
VirtualProduction
VisualEffect
卡通渲染相关资料
性能优化
流程管理与部署
.keep
03-UnrealEngine.md
04-ComputerGraphics
05-SDHGame
06-DCC
07-Other
08-Assets
09-Templates
.gitattributes
.gitignore
.gitmodules
LICENSE

68 lines
3.1 KiB
Markdown
Raw Normal View History

2024-10-12 17:19:46 +08:00
# AllowPrivateAccess
- **功能描述:** 允许一个在C++中private的属性可以在蓝图中访问。
- **使用位置:** UPROPERTY
- **元数据类型:** bool
- **关联项:** [BlueprintProtected](../BlueprintProtected/BlueprintProtected.md)
- **常用程度:** ★★★★★
允许一个在C++中private的属性可以在蓝图中访问。
AllowPrivateAccess的意义是允许这个属性在C++是private的不允许C++子类访问,但又允许其暴露到蓝图中访问。
## 测试代码:
```cpp
public:
//CPF_BlueprintVisible | CPF_ZeroConstructor | CPF_IsPlainOldData | CPF_NoDestructor | CPF_HasGetValueTypeHash | CPF_NativeAccessSpecifierPublic
UPROPERTY(BlueprintReadWrite)
int32 MyNativeInt_NativePublic;
private:
//CPF_ZeroConstructor | CPF_IsPlainOldData | CPF_NoDestructor | CPF_HasGetValueTypeHash | CPF_NativeAccessSpecifierPrivate
//error : BlueprintReadWrite should not be used on private members
UPROPERTY()
int32 MyNativeInt_NativePrivate;
//(AllowPrivateAccess = TRUE, Category = MyFunction_Access, ModuleRelativePath = Function/MyFunction_Access.h)
//CPF_BlueprintVisible | CPF_ZeroConstructor | CPF_IsPlainOldData | CPF_NoDestructor | CPF_HasGetValueTypeHash | CPF_NativeAccessSpecifierPrivate
UPROPERTY(BlueprintReadWrite, meta = (AllowPrivateAccess = true))
int32 MyNativeInt_NativePrivate_AllowPrivateAccess;
```
在MyNativeInt_NativePrivate上如果尝试加上BlueprintReadWrite或BlueprintReadOnly都会触发UHT编译报错。
## 蓝图里的效果:
默认情况下MyNativeInt_NativePrivate_AllowPrivateAccess在蓝图里的访问权限和MyNativeInt_NativePublic一致。
如果读者想要修改改属性在蓝图中的访问权限则可以配合加上BlueprintProtected和BlueprintPrivate。
![Untitled](Untitled.png)
## 原理:
UHT在识别属性的BlueprintReadWrite或BlueprintReadOnly标识符的时候会同时检测是否有AllowPrivateAccess没有的话会触发报错。
因此AllowPrivateAccess的意义其实只是在阻止UHT的报错这层检测报错过了之后属性上的BlueprintReadWrite或BlueprintReadOnly就会被识别并发挥作用从而可以在蓝图中访问。
```cpp
private static void BlueprintReadWriteSpecifier(UhtSpecifierContext specifierContext)
{
bool allowPrivateAccess = context.MetaData.TryGetValue(UhtNames.AllowPrivateAccess, out string? privateAccessMD) && !privateAccessMD.Equals("false", StringComparison.OrdinalIgnoreCase);
if (specifierContext.AccessSpecifier == UhtAccessSpecifier.Private && !allowPrivateAccess)
{
context.MessageSite.LogError("BlueprintReadWrite should not be used on private members");
}
}
private static void BlueprintReadOnlySpecifier(UhtSpecifierContext specifierContext)
{
bool allowPrivateAccess = context.MetaData.TryGetValue(UhtNames.AllowPrivateAccess, out string? privateAccessMD) && !privateAccessMD.Equals("false", StringComparison.OrdinalIgnoreCase);
if (specifierContext.AccessSpecifier == UhtAccessSpecifier.Private && !allowPrivateAccess)
{
context.MessageSite.LogError("BlueprintReadOnly should not be used on private members");
}
}
```