Files
UnrealEngineUWP/Engine/Plugins/ScriptPlugin/Source/ScriptEditorPlugin/Private/ScriptBlueprintCompiler.cpp
dave jones2 c8574d3b74 Merging //UE5/Dev-LargeWorldCoordinates [at] 18802167 to //UE5/Release-5.0
Blueprint real number support.

This change deprecates the use the of "float" and "double" types in Blueprints in favor of a new "real". By default, "real" is back by a double precision floating point number. However, it can be single precision if the number is a native float property or function parameter. This distinction won't be visible to the Blueprint user: in both instances, they'll be represented by "real" pin types. During deserialization, we'll automatically convert Blueprint pin types to use real/doubles, unless they're used to represent native code (including delegate signatures).

One consequence of this change is that we need to perform implicit casts between single and double precision real numbers. During Blueprint compilation, the compiler will detect points in the graph for when either a widening or narrowing conversion needs to occur. Subsequently, the script bytecode will contain a new cast instruction that performs the conversion. This also works on container types, but each entry in the container will have to be converted. This can introduce unwanted overhead for large containers that are frequently passed between Blueprint and native code.

The scope of this change affects Blueprints used by Gameplay, Animation, Control Rig, and UMG.

#rb marc.audy (serialization changes)
#jira UE-116484
#preflight 61f8bdd5a2514ba12ff7bdfc

#ROBOMERGE-AUTHOR: dave.jones2
#ROBOMERGE-SOURCE: CL 18809077 in //UE5/Release-5.0/... via CL 18809455 via CL 18822548
#ROBOMERGE-BOT: UE5 (Release-Engine-Test -> Main) (v908-18788545)

[CL 18823569 by dave jones2 in ue5-main branch]
2022-02-02 05:50:50 -05:00

254 lines
9.1 KiB
C++

// Copyright Epic Games, Inc. All Rights Reserved.
#include "ScriptBlueprintCompiler.h"
#include "ScriptBlueprint.h"
#include "ScriptBlueprintGeneratedClass.h"
#include "Kismet2/Kismet2NameValidators.h"
#include "Kismet2/KismetReinstanceUtilities.h"
#include "ScriptContext.h"
#include "ScriptContextComponent.h"
#include "K2Node_FunctionEntry.h"
#include "K2Node_VariableGet.h"
#include "K2Node_CallFunction.h"
#include "GameFramework/Actor.h"
#include "ScriptPluginComponent.h"
///-------------------------------------------------------------
FScriptBlueprintCompiler::FScriptBlueprintCompiler(UScriptBlueprint* SourceSketch, FCompilerResultsLog& InMessageLog, const FKismetCompilerOptions& InCompilerOptions)
: Super(SourceSketch, InMessageLog, InCompilerOptions)
, NewScriptBlueprintClass(NULL)
, ContextProperty(NULL)
{
}
FScriptBlueprintCompiler::~FScriptBlueprintCompiler()
{
}
void FScriptBlueprintCompiler::CleanAndSanitizeClass(UBlueprintGeneratedClass* ClassToClean, UObject*& InOldCDO)
{
Super::CleanAndSanitizeClass(ClassToClean, InOldCDO);
// Make sure our typed pointer is set
check(ClassToClean == NewClass && NewScriptBlueprintClass == NewClass);
ContextProperty = NULL;
}
void FScriptBlueprintCompiler::CreateClassVariablesFromBlueprint()
{
Super::CreateClassVariablesFromBlueprint();
UScriptBlueprint* ScriptBP = ScriptBlueprint();
UScriptBlueprintGeneratedClass* NewScripClass = CastChecked<UScriptBlueprintGeneratedClass>(NewClass);
NewScripClass->ScriptProperties.Empty();
for (FScriptField& Field : ScriptDefinedFields)
{
UClass* InnerType = Field.Class;
if (Field.PropertyClass)
{
FName PinCategory;
FName PinSubCategory = NAME_None;
if (Field.PropertyClass->IsChildOf(FStrProperty::StaticClass()))
{
PinCategory = UEdGraphSchema_K2::PC_String;
}
else if (Field.PropertyClass->IsChildOf(FFloatProperty::StaticClass()))
{
#if ENABLE_BLUEPRINT_REAL_NUMBERS
PinCategory = UEdGraphSchema_K2::PC_Real;
PinSubCategory = UEdGraphSchema_K2::PC_Float;
#else
PinCategory = UEdGraphSchema_K2::PC_Float;
#endif
}
else if (Field.PropertyClass->IsChildOf(FIntProperty::StaticClass()))
{
PinCategory = UEdGraphSchema_K2::PC_Int;
}
else if (Field.PropertyClass->IsChildOf(FInt64Property::StaticClass()))
{
PinCategory = UEdGraphSchema_K2::PC_Int64;
}
else if (Field.PropertyClass->IsChildOf(FBoolProperty::StaticClass()))
{
PinCategory = UEdGraphSchema_K2::PC_Boolean;
}
else if (Field.PropertyClass->IsChildOf(FObjectProperty::StaticClass()))
{
PinCategory = UEdGraphSchema_K2::PC_Object;
// @todo: some scripting extensions (that are strongly typed) can handle this better
InnerType = UObject::StaticClass();
}
if (!PinCategory.IsNone())
{
FEdGraphPinType ScriptPinType(PinCategory, PinSubCategory, InnerType, EPinContainerType::None, false, FEdGraphTerminalType());
FProperty* ScriptProperty = CreateVariable(Field.Name, ScriptPinType);
if (ScriptProperty)
{
ScriptProperty->SetMetaData(TEXT("Category"), *ScriptBP->GetName());
ScriptProperty->SetPropertyFlags(CPF_BlueprintVisible | CPF_Edit);
NewScripClass->ScriptProperties.Add(ScriptProperty);
}
}
}
}
CreateScriptContextProperty();
}
void FScriptBlueprintCompiler::CreateScriptContextProperty()
{
// The only case we don't need a script context is if the script class derives form UScriptPluginComponent
UClass* ContextClass = nullptr;
if (Blueprint->ParentClass->IsChildOf(AActor::StaticClass()))
{
ContextClass = UScriptContextComponent::StaticClass();
}
else if (!Blueprint->ParentClass->IsChildOf(UScriptPluginComponent::StaticClass()))
{
ContextClass = UScriptContext::StaticClass();
}
if (ContextClass)
{
FEdGraphPinType ScriptContextPinType(UEdGraphSchema_K2::PC_Object, NAME_None, ContextClass, EPinContainerType::None, false, FEdGraphTerminalType());
ContextProperty = CastFieldChecked<FObjectProperty>(CreateVariable(TEXT("Generated_ScriptContext"), ScriptContextPinType));
ContextProperty->SetPropertyFlags(CPF_ContainsInstancedReference | CPF_InstancedReference);
}
}
void FScriptBlueprintCompiler::CreateFunctionList()
{
Super::CreateFunctionList();
if (!Blueprint->ParentClass->IsChildOf(UScriptPluginComponent::StaticClass()))
{
for (auto& Field : ScriptDefinedFields)
{
if (Field.Class->IsChildOf(UFunction::StaticClass()))
{
CreateScriptDefinedFunction(Field);
}
}
}
}
void FScriptBlueprintCompiler::CreateScriptDefinedFunction(FScriptField& Field)
{
check(ContextProperty);
UScriptBlueprint* ScriptBP = ScriptBlueprint();
const FString FunctionName = Field.Name.ToString();
// Create Blueprint Graph which consists of 3 nodes: 'Entry', 'Get Script Context' and 'Call Function'
// @todo: once we figure out how to get parameter lists for functions we can add suport for that here
UEdGraph* ScriptFunctionGraph = NewObject<UEdGraph>(ScriptBP, *FString::Printf(TEXT("%s_Graph"), *FunctionName));
ScriptFunctionGraph->Schema = UEdGraphSchema_K2::StaticClass();
ScriptFunctionGraph->SetFlags(RF_Transient);
FKismetFunctionContext* FunctionContext = CreateFunctionContext();
FunctionContext->SourceGraph = ScriptFunctionGraph;
FunctionContext->bCreateDebugData = false;
UK2Node_FunctionEntry* EntryNode = SpawnIntermediateNode<UK2Node_FunctionEntry>(NULL, ScriptFunctionGraph);
EntryNode->CustomGeneratedFunctionName = Field.Name;
EntryNode->AllocateDefaultPins();
UK2Node_VariableGet* GetVariableNode = SpawnIntermediateNode<UK2Node_VariableGet>(NULL, ScriptFunctionGraph);
GetVariableNode->VariableReference.SetSelfMember(ContextProperty->GetFName());
GetVariableNode->AllocateDefaultPins();
UK2Node_CallFunction* CallFunctionNode = SpawnIntermediateNode<UK2Node_CallFunction>(NULL, ScriptFunctionGraph);
CallFunctionNode->FunctionReference.SetExternalMember(TEXT("CallScriptFunction"), ContextProperty->PropertyClass);
CallFunctionNode->AllocateDefaultPins();
UEdGraphPin* FunctionNamePin = CallFunctionNode->FindPinChecked(TEXT("FunctionName"));
FunctionNamePin->DefaultValue = FunctionName;
// Link nodes together
UEdGraphPin* ExecPin = Schema->FindExecutionPin(*EntryNode, EGPD_Output);
UEdGraphPin* GetVariableOutPin = GetVariableNode->FindPinChecked(ContextProperty->GetFName());
UEdGraphPin* CallFunctionPin = Schema->FindExecutionPin(*CallFunctionNode, EGPD_Input);
UEdGraphPin* FunctionTargetPin = CallFunctionNode->FindPinChecked(TEXT("self"));
ExecPin->MakeLinkTo(CallFunctionPin);
GetVariableOutPin->MakeLinkTo(FunctionTargetPin);
}
void FScriptBlueprintCompiler::FinishCompilingClass(UClass* Class)
{
UScriptBlueprint* ScriptBP = ScriptBlueprint();
UScriptBlueprintGeneratedClass* ScriptClass = CastChecked<UScriptBlueprintGeneratedClass>(Class);
ScriptClass->SourceCode = ScriptBP->SourceCode;
ScriptClass->ByteCode = ScriptBP->ByteCode;
// Allow Blueprint Components to be used in Blueprints
if (ScriptBP->ParentClass->IsChildOf(UScriptPluginComponent::StaticClass()) && Class != ScriptBP->SkeletonGeneratedClass)
{
Class->SetMetaData(TEXT("BlueprintSpawnableComponent"), TEXT("true"));
}
Super::FinishCompilingClass(Class);
// Ff context property has been created, create a DSO and set it on the CDO
if (ContextProperty)
{
UObject* CDO = Class->GetDefaultObject();
UObject* ContextDefaultSubobject = NewObject<UObject>(CDO, ContextProperty->PropertyClass, "ScriptContext", RF_DefaultSubObject | RF_Public);
ContextProperty->SetObjectPropertyValue(ContextProperty->ContainerPtrToValuePtr<UObject*>(CDO), ContextDefaultSubobject);
}
}
void FScriptBlueprintCompiler::PreCompile()
{
ScriptBlueprint()->UpdateSourceCodeIfChanged();
ScriptContext.Reset(FScriptContextBase::CreateContext(ScriptBlueprint()->SourceCode, NULL, NULL));
bool Result = true;
if (ScriptContext)
{
ScriptDefinedFields.Empty();
ScriptContext->GetScriptDefinedFields(ScriptDefinedFields);
}
ContextProperty = NULL;
}
void FScriptBlueprintCompiler::EnsureProperGeneratedClass(UClass*& TargetUClass)
{
if ( TargetUClass && !( (UObject*)TargetUClass )->IsA(UScriptBlueprintGeneratedClass::StaticClass()) )
{
FKismetCompilerUtilities::ConsignToOblivion(TargetUClass, Blueprint->bIsRegeneratingOnLoad);
TargetUClass = NULL;
}
}
void FScriptBlueprintCompiler::SpawnNewClass(const FString& NewClassName)
{
NewScriptBlueprintClass = FindObject<UScriptBlueprintGeneratedClass>(Blueprint->GetOutermost(), *NewClassName);
if ( NewScriptBlueprintClass == NULL )
{
NewScriptBlueprintClass = NewObject<UScriptBlueprintGeneratedClass>(Blueprint->GetOutermost(), FName(*NewClassName), RF_Public | RF_Transactional);
}
else
{
// Already existed, but wasn't linked in the Blueprint yet due to load ordering issues
FBlueprintCompileReinstancer::Create(NewScriptBlueprintClass);
}
NewClass = NewScriptBlueprintClass;
}
void FScriptBlueprintCompiler::OnNewClassSet(UBlueprintGeneratedClass* ClassToUse)
{
NewScriptBlueprintClass = CastChecked<UScriptBlueprintGeneratedClass>(ClassToUse);
}
bool FScriptBlueprintCompiler::ValidateGeneratedClass(UBlueprintGeneratedClass* Class)
{
bool SuperResult = Super::ValidateGeneratedClass(Class);
bool Result = UScriptBlueprint::ValidateGeneratedClass(Class);
return SuperResult && Result;
}