Files
UnrealEngineUWP/Engine/Source/Editor/BlueprintGraph/Private/K2Node_Variable.cpp

458 lines
14 KiB
C++
Raw Normal View History

// Copyright 1998-2014 Epic Games, Inc. All Rights Reserved.
#include "BlueprintGraphPrivatePCH.h"
#include "CompilerResultsLog.h"
#include "ClassIconFinder.h"
#define LOCTEXT_NAMESPACE "K2Node"
UK2Node_Variable::UK2Node_Variable(const class FPostConstructInitializeProperties& PCIP)
: Super(PCIP)
{
}
void UK2Node_Variable::Serialize(FArchive& Ar)
{
Super::Serialize(Ar);
// Fix old content
if(Ar.IsLoading())
{
if(Ar.UE4Ver() < VER_UE4_VARK2NODE_NULL_VARSRCCLASS_ON_SELF)
{
// See if bSelfContext is set but there is still a class assigned
if(bSelfContext_DEPRECATED && VariableSourceClass_DEPRECATED != NULL)
{
VariableSourceClass_DEPRECATED = NULL;
UE_LOG(LogBlueprint, Log, TEXT("VarNode '%s' Variable '%s': Cleared VariableSourceClass."), *GetPathName(), *GetVarNameString());
}
}
if(Ar.UE4Ver() < VER_UE4_VARK2NODE_USE_MEMBERREFSTRUCT)
{
// Copy info into new struct
VariableReference.SetDirect(VariableName_DEPRECATED, FGuid(), VariableSourceClass_DEPRECATED, bSelfContext_DEPRECATED);
}
if(Ar.UE4Ver() < VER_UE4_K2NODE_REFERENCEGUIDS)
{
FGuid VarGuid;
if (UBlueprint::GetGuidFromClassByFieldName<UProperty>(GetBlueprint()->GeneratedClass, VariableReference.GetMemberName(), VarGuid))
{
const bool bSelf = VariableReference.IsSelfContext();
VariableReference.SetDirect(VariableReference.GetMemberName(), VarGuid, (bSelf ? NULL : VariableReference.GetMemberParentClass((UClass*)NULL)), bSelf);
}
}
}
}
void UK2Node_Variable::SetFromProperty(const UProperty* Property, bool bSelfContext)
{
SelfContextInfo = bSelfContext ? ESelfContextInfo::Unspecified : ESelfContextInfo::NotSelfContext;
VariableReference.SetFromField<UProperty>(Property, bSelfContext);
}
bool UK2Node_Variable::CreatePinForVariable(EEdGraphPinDirection Direction)
{
const UEdGraphSchema_K2* K2Schema = GetDefault<UEdGraphSchema_K2>();
UProperty* VariableProperty = GetPropertyForVariable();
if (VariableProperty != NULL)
{
// Create the pin
UEdGraphPin* VariablePin = CreatePin(Direction, TEXT(""), TEXT(""), NULL, false, false, GetVarNameString());
K2Schema->ConvertPropertyToPinType(VariableProperty, /*out*/ VariablePin->PinType);
K2Schema->SetPinDefaultValueBasedOnType(VariablePin);
}
else
{
Message_Warn(*FString::Printf(TEXT("CreatePinForVariable: '%s' variable not found. Base class was probably changed."), *GetVarNameString()));
return false;
}
return true;
}
void UK2Node_Variable::CreatePinForSelf()
{
const UEdGraphSchema_K2* K2Schema = GetDefault<UEdGraphSchema_K2>();
UClass* VariableClass = GetVariableSourceClass();
// Create the self pin
if (!K2Schema->FindSelfPin(*this, EGPD_Input))
{
// Do not create a self pin for locally scoped variables
if( !VariableReference.IsLocalScope() )
{
if (VariableReference.IsSelfContext() && (ESelfContextInfo::NotSelfContext != SelfContextInfo))
{
UEdGraphPin* SelfPin = CreatePin(EGPD_Input, K2Schema->PC_Object, K2Schema->PSC_Self, NULL, false, false, K2Schema->PN_Self);
SelfPin->bHidden = true; // don't show in 'self' context
}
else
{
UClass* MemberParentClass = VariableReference.GetMemberParentClass(this);
UClass* AuthoritativeClass = MemberParentClass ? MemberParentClass->GetAuthoritativeClass() : NULL;
UEdGraphPin* TargetPin = CreatePin(EGPD_Input, K2Schema->PC_Object, TEXT(""), AuthoritativeClass, false, false, K2Schema->PN_Self);
TargetPin->PinFriendlyName = LOCTEXT("Target", "Target");
}
}
}
else
{
//@TODO: Check that the self pin types match!
}
}
bool UK2Node_Variable::RecreatePinForVariable(EEdGraphPinDirection Direction, TArray<UEdGraphPin*>& OldPins)
{
// probably the node was pasted to a blueprint without the variable
// we don't want to beak any connection, so the pin will be recreated from old one, but compiler will throw error
// find old variable pin
const UEdGraphPin* OldVariablePin = NULL;
const FString PinName = GetVarNameString();
for(auto Iter = OldPins.CreateConstIterator(); Iter; ++Iter)
{
if(const UEdGraphPin* Pin = *Iter)
{
if(PinName == Pin->PinName)
{
OldVariablePin = Pin;
break;
}
}
}
if(NULL != OldVariablePin)
{
// create new pin from old one
UEdGraphPin* VariablePin = CreatePin(Direction, TEXT(""), TEXT(""), NULL, false, false, PinName);
VariablePin->PinType = OldVariablePin->PinType;
const UEdGraphSchema_K2* K2Schema = GetDefault<UEdGraphSchema_K2>();
K2Schema->SetPinDefaultValueBasedOnType(VariablePin);
Message_Note(*FString::Printf(TEXT("Pin for variable '%s' recreated, but the variable is missing."), *PinName));
return true;
}
else
{
Message_Warn(*FString::Printf(TEXT("RecreatePinForVariable: '%s' pin not found"), *PinName));
return false;
}
}
FLinearColor UK2Node_Variable::GetNodeTitleColor() const
{
UProperty* VariableProperty = GetPropertyForVariable();
if (VariableProperty)
{
const UEdGraphSchema_K2* K2Schema = GetDefault<UEdGraphSchema_K2>();
FEdGraphPinType VariablePinType;
K2Schema->ConvertPropertyToPinType(VariableProperty, VariablePinType);
return K2Schema->GetPinTypeColor(VariablePinType);
}
return FLinearColor::White;
}
UK2Node::ERedirectType UK2Node_Variable::DoPinsMatchForReconstruction( const UEdGraphPin* NewPin, int32 NewPinIndex, const UEdGraphPin* OldPin, int32 OldPinIndex ) const
{
const UEdGraphSchema_K2* K2Schema = GetDefault<UEdGraphSchema_K2>();
if( OldPin->PinType.PinCategory == K2Schema->PC_Exec )
{
return Super::DoPinsMatchForReconstruction(NewPin, NewPinIndex, OldPin, OldPinIndex);
}
const bool bCanMatchSelfs = ((OldPin->PinName == K2Schema->PN_Self) == (NewPin->PinName == K2Schema->PN_Self));
const bool bTheSameDirection = (NewPin->Direction == OldPin->Direction);
if (bCanMatchSelfs && bTheSameDirection)
{
if (K2Schema->ArePinTypesCompatible(NewPin->PinType, OldPin->PinType))
{
return ERedirectType_Name;
}
else if ((OldPin->PinName == NewPin->PinName) && ((NewPin->PinType.PinCategory == K2Schema->PC_Object) ||
(NewPin->PinType.PinCategory == K2Schema->PC_Interface)) && (NewPin->PinType.PinSubCategoryObject == NULL))
{
// Special Case: If we had a pin match, and the class isn't loaded yet because of a cyclic dependency, temporarily cast away the const, and fix up.
// @TODO: Fix this up to be less hacky
UBlueprintGeneratedClass* TypeClass = Cast<UBlueprintGeneratedClass>(OldPin->PinType.PinSubCategoryObject.Get());
if (TypeClass && TypeClass->ClassGeneratedBy && TypeClass->ClassGeneratedBy->HasAnyFlags(RF_BeingRegenerated))
{
UEdGraphPin* NonConstNewPin = (UEdGraphPin*)NewPin;
NonConstNewPin->PinType.PinSubCategoryObject = OldPin->PinType.PinSubCategoryObject.Get();
return ERedirectType_Name;
}
}
else
{
// Special Case: If we're migrating from old blueprint references to class references, allow pins to be reconnected if coerced
const UClass* PSCOClass = Cast<UClass>(OldPin->PinType.PinSubCategoryObject.Get());
const bool bOldIsBlueprint = PSCOClass && PSCOClass->IsChildOf(UBlueprint::StaticClass());
const bool bNewIsClass = (NewPin->PinType.PinCategory == K2Schema->PC_Class);
if (bNewIsClass && bOldIsBlueprint)
{
UEdGraphPin* OldPinNonConst = (UEdGraphPin*)OldPin;
OldPinNonConst->PinName = NewPin->PinName;
return ERedirectType_Name;
}
}
}
return ERedirectType_None;
}
UClass* UK2Node_Variable::GetVariableSourceClass() const
{
UClass* Result = VariableReference.GetMemberParentClass(this);
return Result;
}
UProperty* UK2Node_Variable::GetPropertyForVariable() const
{
const FName VarName = GetVarName();
UEdGraphPin* VariablePin = FindPin(GetVarNameString());
UProperty* VariableProperty = VariableReference.ResolveMember<UProperty>(this);
// if the variable has been deprecated, don't use it
if(VariableProperty != NULL)
{
if (VariableProperty->HasAllPropertyFlags(CPF_Deprecated))
{
VariableProperty = NULL;
}
// If the variable has been remapped update the pin
else if (VariablePin && VarName != GetVarName())
{
VariablePin->PinName = GetVarNameString();
}
}
return VariableProperty;
}
UEdGraphPin* UK2Node_Variable::GetValuePin() const
{
UEdGraphPin* Pin = FindPin(GetVarNameString());
check(Pin == NULL || Pin->Direction == EGPD_Output);
return Pin;
}
void UK2Node_Variable::ValidateNodeDuringCompilation(class FCompilerResultsLog& MessageLog) const
{
Super::ValidateNodeDuringCompilation(MessageLog);
UProperty* VariableProperty = GetPropertyForVariable();
// Local variables do not exist until much later in the compilation than this function can provide
if (VariableProperty == NULL && !VariableReference.IsLocalScope())
{
MessageLog.Warning(*FString::Printf(*LOCTEXT("VariableNotFound", "Unable to find variable with name '%s' for @@").ToString(), *VariableReference.GetMemberName().ToString()), this);
}
}
FName UK2Node_Variable::GetPaletteIcon(FLinearColor& ColorOut) const
{
FName ReturnIconName;
if(VariableReference.IsLocalScope())
{
ReturnIconName = GetVariableIconAndColor(VariableReference.GetMemberScope(this), GetVarName(), ColorOut);
}
else
{
ReturnIconName = GetVariableIconAndColor(GetVariableSourceClass(), GetVarName(), ColorOut);
}
return ReturnIconName;
}
FName UK2Node_Variable::GetVarIconFromPinType(FEdGraphPinType& InPinType, FLinearColor& IconColorOut)
{
FName IconBrush = TEXT("Kismet.AllClasses.VariableIcon");
const UEdGraphSchema_K2* K2Schema = GetDefault<UEdGraphSchema_K2>();
IconColorOut = K2Schema->GetPinTypeColor(InPinType);
if(InPinType.bIsArray)
{
IconBrush = TEXT("Kismet.AllClasses.ArrayVariableIcon");
}
else if(InPinType.PinSubCategoryObject.IsValid())
{
UClass* VarClass = FindObject<UClass>(ANY_PACKAGE, *InPinType.PinSubCategoryObject->GetName());
if( VarClass )
{
IconBrush = FClassIconFinder::FindIconNameForClass( VarClass );
}
}
return IconBrush;
}
FText UK2Node_Variable::GetToolTipHeading() const
{
FText Heading = Super::GetToolTipHeading();
UProperty const* VariableProperty = VariableReference.ResolveMember<UProperty>(this);
if (VariableProperty && VariableProperty->HasAllPropertyFlags(CPF_Net))
{
FText ReplicatedTag = LOCTEXT("ReplicatedVar", "Replicated");
if (Heading.IsEmpty())
{
Heading = ReplicatedTag;
}
else
{
Heading = FText::Format(FText::FromString("{0}\n{1}"), ReplicatedTag, Heading);
}
}
return Heading;
}
FName UK2Node_Variable::GetVariableIconAndColor(UStruct* VarScope, FName VarName, FLinearColor& IconColorOut)
{
FName IconBrush = TEXT("Kismet.AllClasses.VariableIcon");
if(VarScope != NULL)
{
UProperty* Property = FindField<UProperty>(VarScope, VarName);
if(Property != NULL)
{
const UEdGraphSchema_K2* K2Schema = GetDefault<UEdGraphSchema_K2>();
FEdGraphPinType PinType;
if(K2Schema->ConvertPropertyToPinType(Property, PinType)) // use schema to get the color
{
IconBrush = GetVarIconFromPinType(PinType, IconColorOut);
}
}
}
return IconBrush;
}
void UK2Node_Variable::CheckForErrors(const UEdGraphSchema_K2* Schema, FCompilerResultsLog& MessageLog)
{
if(!VariableReference.IsSelfContext() && VariableReference.GetMemberParentClass(this) != NULL)
{
// Check to see if we're not a self context, if we have a valid context. It may have been purged because of a dead execution chain
UEdGraphPin* ContextPin = Schema->FindSelfPin(*this, EGPD_Input);
if((ContextPin != NULL) && (ContextPin->LinkedTo.Num() == 0) && (ContextPin->DefaultObject == NULL))
{
MessageLog.Error(*LOCTEXT("VarNodeError_InvalidVarTarget", "Variable node @@ uses an invalid target. It may depend on a node that is not connected to the execution chain, and got purged.").ToString(), this);
}
}
}
void UK2Node_Variable::ReconstructNode()
{
// update the variable reference if the property was renamed
UClass* const VarClass = GetVariableSourceClass();
if (VarClass)
{
UClass* SearchClass = VarClass;
while (SearchClass != NULL)
{
const TMap<FName, FName>* const ClassTaggedPropertyRedirects = UStruct::TaggedPropertyRedirects.Find( SearchClass->GetFName() );
if (ClassTaggedPropertyRedirects)
{
const FName* const NewPropertyName = ClassTaggedPropertyRedirects->Find( VariableReference.GetMemberName() );
if (NewPropertyName)
{
if (VariableReference.IsSelfContext())
{
VariableReference.SetSelfMember( *NewPropertyName );
}
else
{
VariableReference.SetExternalMember( *NewPropertyName, VarClass );
}
// found, can break
break;
}
}
SearchClass = SearchClass->GetSuperClass();
}
}
const FGuid VarGuid = VariableReference.GetMemberGuid();
if (VarGuid.IsValid())
{
const FName VarName = UBlueprint::GetFieldNameFromClassByGuid<UProperty>(VarClass, VarGuid);
if (VarName != NAME_None && VarName != VariableReference.GetMemberName())
{
if (VariableReference.IsSelfContext())
{
VariableReference.SetSelfMember( VarName );
}
else
{
VariableReference.SetExternalMember( VarName, VarClass );
}
}
}
Super::ReconstructNode();
}
FName UK2Node_Variable::GetCornerIcon() const
{
const UProperty* VariableProperty = VariableReference.ResolveMember<UProperty>(this);
if (VariableProperty && VariableProperty->HasAllPropertyFlags(CPF_Net))
{
return TEXT("Graph.Replication.Replicated");
}
return Super::GetCornerIcon();
}
bool UK2Node_Variable::HasExternalBlueprintDependencies(TArray<class UStruct*>* OptionalOutput) const
{
UClass* SourceClass = GetVariableSourceClass();
UBlueprint* SourceBlueprint = GetBlueprint();
const bool bResult = (SourceClass && (SourceClass->ClassGeneratedBy != NULL) && (SourceClass->ClassGeneratedBy != SourceBlueprint));
if (bResult && OptionalOutput)
{
OptionalOutput->Add(SourceClass);
}
Merging UE4-Pretest @ 2042161 to UE4 Change 1996384 by Andrew Brown: 322252 - EDITOR: Asset picker displays incorrect text when there are no filter results. Change 1996385 by Andrew Brown: 321858 - CRASH: Assertion failed: (Index >= 0) Function: STransformViewportToolBar::GetLocationGridLabel() STextBlock::CacheDesiredSize() Change 1996977 by Andrew Brown: 309685 - UE4: Adding an event/renaming an event on an event track in Matinee does not update the MatineeActor node in blueprint Change 2034873 by Jaroslaw Palczynski: More robust VS installation detection. Change 2039693 by Jaroslaw Palczynski: 327268 - RocketGDC: POSTLAUNCH: DEV: Make engine more robust against bad Visual Studio environment variables Change 1978978 by Jaroslaw Surowiec: - Removed obsolete AllowEliminatingReferences from the FArchive Change 2020326 by Maciej Mroz: pretest BP K2Node: RemovePinsFromOldPins function moved from K2Node to RemovePinsFromOldPins Change 2017608 by Maciej Mroz: pretest Some changes in SFortMissionEventSelector caused by FPinTypeTreeInfo Change 2017463 by Maciej Mroz: PinTypeSelector can lins unloaded UDStructs Change 2019979 by Maciej Mroz: pretest BP: Crash when performing Diff against Depot with blueprints containing Format Text nodes Change 2024469 by Maciej Mroz: MemberReference variable added to PinType. It's necessary for delegate's signature. Change 2024049 by Maciej Mroz: HasExternalBlueprintDependencies added to UK2Node_DynamicCast Change 2024586 by Maciej Mroz: FillSimpleMemberReference fix Change 2024472 by Maciej Mroz: workaround for delegates signature in pintype removed. Change 2023997 by Maciej Mroz: BP, UDStruc: Class UserDefinedStructEditorData added. It fixes many problems with undo/redo. Change 2021934 by Maciej Mroz: typo in a comment Change 2020355 by Maciej Mroz: Back out changelist 2020342 Change 2022178 by Maciej Mroz: CRASH: PRETEST: EDITOR: UDS: Crash when undo then redo new variable in struct that is used by blueprint Change 2021958 by Maciej Mroz: CRASH: PRETEST: EDITOR: UDS: Crash using variable of a type of copied struct in blueprint Change 1986247 by Maciej Mroz: User Defined Structures: circle dependency fixed. Early version. Change 1985107 by Maciej Mroz: UserDefinedStruct cannot have a field of a non-native type Change 1986278 by Maciej Mroz: pretest ensureMsgf in Struct::link Change 1986250 by Maciej Mroz: User Defined Struct: Non native classes are accepted types od values in structures. Change 1980955 by Maciej Mroz: Using AssetPtr and LazyPtr as UFunction parameter (intput or return) is explicitly disallowed. Change 2041215 by Maciej Mroz: ttp331249 BLOCKER: PRETEST: UI: Survive the Storm is missing the Mission HUD. Change 1984316 by Maciej Mroz: New User Defined Structure. WIP - there are still problems with circular dependencies. Change 2011616 by Maciej Mroz: UserDefinedStructures - various problems fixed. Change 2011609 by Maciej Mroz: more robust HasExternalBlueprintDependencies implementation Change 2016697 by Maciej Mroz: pretest BP: UDStruct - default value propagation in cooked build Change 2016288 by Maciej Mroz: pretest BP: UDStruct: Renaming variables wont break links from make/break nodes Change 1987637 by Maciej Mroz: CustomStruct icons placeholders Change 1987422 by Maciej Mroz: Better tooltips for variables in MyBlueprint Change 1991387 by Maciej Mroz: UDStructures fixes: Change 2029165 by Maciej Mroz: BP: better comment for incomatible pins Change 2030016 by Maciej Mroz: 8PRETEST: EDITOR: UDS: Defaults values aren't updated in struct type variables in blueprints Change 2030017 by Maciej Mroz: Unused UDStructure code removed (PPF_UseDefaultsForUDStructures) Change 2028856 by Maciej Mroz: BP: Pins with PC_Struct type are compatible only with exactly the same structure. (No derived structures are not handled as compatible). Change 2026701 by Maciej Mroz: k2: odd error on an add item node within a function (see attached image in details) Change 2028160 by Maciej Mroz: PRETEST: EDITOR: UDS: When deleting structures just after creating there is always some references in the memory Change 2028165 by Maciej Mroz: BP: BreakHitResult function has proper icon. Change 2033340 by Maciej Mroz: ttp330786 PRETEST: EDITOR: UDS: Changes of default values aren't apllied to breeak nodes for text type of variables Change 2034255 by Maciej Mroz: EDITOR: UDS: Changes of default values aren't apllied to make nodes for text type of variables ttp#330620 Change 2037682 by Maciej Mroz: ttp331309 BLOCKER: PRETEST: CRASH: EDITOR: Crash occurs when performing Diff Against Depot on any Blueprint Change 2033142 by Maciej Mroz: CreateDelegate Node uses internally FMemberReference. Refactor. Change 2032329 by Maciej Mroz: ttp330608 CRASH: PRETEST: EDITOR: UDS: Crash when trying to use struct named 'Color' in blueprint Change 2032420 by Maciej Mroz: ttp330620 PRETEST: EDITOR: UDS: Changes of default values aren't apllied to make nodes for text type of variables Change 2033139 by Maciej Mroz: Functions generated from CustomEvents can be also identified by GUID Change 2026631 by Maciej Mroz: BP. UDStruct: Invalid structs are handled better. Change 2025344 by Maciej Mroz: UDStruct enabled by default Change 2026672 by Maciej Mroz: EDITOR: BP: Can't easily remove 'pass-by-reference' pins on ReturnNodes Change 2026411 by Maciej Mroz: ExposeOnSpawn updated, it supports UDStructs, custom native Structs, and it throws compiler error. Change 2025342 by Maciej Mroz: GenerateBlueprintSkeleton moved from BLueprint::Serialize to RegenerateBlueprintClass, because SkeletonClass compilation requires all external dependencies to be loaded and linked. Change 2025570 by Steve Robb: Moved dependency processing to its own function. Change 2033235 by Steve Robb: String improvements Change 2035830 by Steve Robb: Workaround for FriendsAndChat crash in Fortnite. Change 2035115 by Steve Robb: UBT build time regression fixes. Change 2034162 by Steve Robb: 312775: UObject improvement: Ensure that *.generated.inl is included somewhere Change 2034181 by Steve Robb: Removal of any references to .generated.inl Change 2020165 by Steve Robb: BuildPublicAndPrivateUObjectHeaders factored out into its own function. Change 2020187 by Steve Robb: CreateModuleCompileEnvironment function factored out. Change 2020055 by Steve Robb: Refactoring of Unity.cs to remove complex and duplicate iteration. Change 2020083 by Steve Robb: Another use of dictionary utilities. Change 2031049 by Steve Robb: 312775: UObject improvement: Ensure that *.generated.inl is included somewhere Change 2025728 by Steve Robb: Refactored the application of a shared PCH file to multiple file into a single ApplySharedPCH function. Change 2020068 by Steve Robb: A couple of helpful utility functions for populating dictionaries. Change 2032307 by Steve Robb: 312775: UObject improvement: Ensure that *.generated.inl is included somewhere [CL 2054495 by Robert Manuszewski in Main branch]
2014-04-23 20:18:55 -04:00
return bResult || Super::HasExternalBlueprintDependencies(OptionalOutput);
}
FString UK2Node_Variable::GetDocumentationLink() const
{
if( UProperty* Property = GetPropertyForVariable() )
{
// discover if the variable property is a non blueprint user variable
UClass* SourceClass = Property->GetOwnerClass();
if( SourceClass && SourceClass->ClassGeneratedBy == NULL )
{
UStruct* OwnerStruct = Property->GetOwnerStruct();
if( OwnerStruct )
{
return FString::Printf( TEXT("Shared/Types/%s%s"), OwnerStruct->GetPrefixCPP(), *OwnerStruct->GetName() );
}
}
}
return TEXT( "" );
}
FString UK2Node_Variable::GetDocumentationExcerptName() const
{
return GetVarName().ToString();
}
#undef LOCTEXT_NAMESPACE