72 lines
3.2 KiB
Markdown
72 lines
3.2 KiB
Markdown
|
# MakeStructureDefaultValue
|
|||
|
|
|||
|
- **功能描述:** 存储BP中自定义结构里的属性的默认值。
|
|||
|
- **使用位置:** UPROPERTY
|
|||
|
- **引擎模块:** Struct
|
|||
|
- **元数据类型:** bool
|
|||
|
- **限制类型:** BP里的用户自定义Struct
|
|||
|
- **常用程度:** ★
|
|||
|
|
|||
|
存储BP中自定义结构里的属性的默认值。
|
|||
|
|
|||
|
- 在C++中我们写的USTRUCT的结构里的属性默认值并不需要存储在元数据中,因为在创建该结构实例的时候,就自然会调用该结构的构造函数,从而正确初始化值。
|
|||
|
- 而在蓝图中的用户自定义结构,并没有构造函数之类的机制。因此我们需要一个专门的Tab来填写属性的默认值。这些默认值就会存储在属性的元数据中。
|
|||
|
-
|
|||
|
|
|||
|
## 测试代码:
|
|||
|
|
|||
|
在蓝图中定义一个结构BP_StructDefaultTest,并填上默认值。
|
|||
|
|
|||
|

|
|||
|
|
|||
|
## 测试结果:
|
|||
|
|
|||
|
用测试命令行打印出其相关的信息,可以见到MyInt和MyString的真正属性名以及MakeStructureDefaultValue 的值。
|
|||
|
|
|||
|
```cpp
|
|||
|
[struct BP_StructDefaultTest UserDefinedStruct->ScriptStruct->Struct->Field->Object /Game/Struct/BP_StructDefaultTest.BP_StructDefaultTest]
|
|||
|
(BlueprintType = true, Tooltip = )
|
|||
|
ObjectFlags: RF_Public | RF_Standalone | RF_Transactional | RF_WasLoaded | RF_LoadCompleted
|
|||
|
Outer: Package /Game/Struct/BP_StructDefaultTest
|
|||
|
StructFlags: STRUCT_NoFlags
|
|||
|
Size: 24
|
|||
|
{
|
|||
|
(DisplayName = MyInt, Tooltip = , MakeStructureDefaultValue = 789)
|
|||
|
0-[4] int32 MyInt_3_CC664A574A072369083883B38EA2F129;
|
|||
|
PropertyFlags: CPF_Edit | CPF_BlueprintVisible | CPF_ZeroConstructor | CPF_IsPlainOldData | CPF_NoDestructor | CPF_HasGetValueTypeHash
|
|||
|
ObjectFlags: RF_Public | RF_LoadCompleted
|
|||
|
Outer: UserDefinedStruct /Game/Struct/BP_StructDefaultTest.BP_StructDefaultTest
|
|||
|
Path: IntProperty /Game/Struct/BP_StructDefaultTest.BP_StructDefaultTest:MyInt_3_CC664A574A072369083883B38EA2F129
|
|||
|
(DisplayName = MyString, Tooltip = , MakeStructureDefaultValue = Hello)
|
|||
|
8-[16] FString MyString_6_D8FAF5D6454C781C2D5175ACF266C394;
|
|||
|
PropertyFlags: CPF_Edit | CPF_BlueprintVisible | CPF_ZeroConstructor | CPF_HasGetValueTypeHash
|
|||
|
ObjectFlags: RF_Public | RF_LoadCompleted
|
|||
|
Outer: UserDefinedStruct /Game/Struct/BP_StructDefaultTest.BP_StructDefaultTest
|
|||
|
Path: StrProperty /Game/Struct/BP_StructDefaultTest.BP_StructDefaultTest:MyString_6_D8FAF5D6454C781C2D5175ACF266C394
|
|||
|
};
|
|||
|
```
|
|||
|
|
|||
|
## 原理:
|
|||
|
|
|||
|
在BP中结构创建变量保存的时候,如果发现默认值不为空,则设置到MakeStructureDefaultValue中去。之后再MakeStruct的时候就可以用上了。
|
|||
|
|
|||
|
```cpp
|
|||
|
void UK2Node_MakeStruct::FMakeStructPinManager::CustomizePinData(UEdGraphPin* Pin, FName SourcePropertyName, int32 ArrayIndex, FProperty* Property) const
|
|||
|
{
|
|||
|
const FString& MetadataDefaultValue = Property->GetMetaData(TEXT("MakeStructureDefaultValue"));
|
|||
|
if (!MetadataDefaultValue.IsEmpty())
|
|||
|
{
|
|||
|
Schema->SetPinAutogeneratedDefaultValue(Pin, MetadataDefaultValue);
|
|||
|
return;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
static void FUserDefinedStructureCompilerInner::CreateVariables(UUserDefinedStruct* Struct, const class UEdGraphSchema_K2* Schema, FCompilerResultsLog& MessageLog)
|
|||
|
{
|
|||
|
if (!VarDesc.DefaultValue.IsEmpty())
|
|||
|
{
|
|||
|
VarProperty->SetMetaData(TEXT("MakeStructureDefaultValue"), *VarDesc.DefaultValue);
|
|||
|
}
|
|||
|
}
|
|||
|
```
|