Worked on the code for Format Error Message, better design overall.

This commit is contained in:
2025-03-19 20:47:25 -04:00
parent 27be4ce758
commit 5ef3adc527
4 changed files with 255 additions and 166 deletions

View File

@@ -78,7 +78,6 @@ UK2Node_FormatError::UK2Node_FormatError(const FObjectInitializer& ObjectInitial
"Output an error, warning, or informational message to the log file.\n"
"\n"
" \u2022 Use {ArgName} to denote format arguments, giving each argument a different ArgName.\n"
" \u2022 Arguments may be Byte, Integer, Float, Text, String, Name, Boolean, Object or ETextGender.\n"
"\n"
"It is often desirable to use this in conjunction with a separate utility that\n"
"pauses the execution of the blueprint whenever an error is logged."
@@ -267,22 +266,6 @@ FText UK2Node_FormatError::GetTooltipText() const
return NodeTooltip;
}
static UEdGraphPin* FindOutputStructPinChecked(UEdGraphNode* Node)
{
check(NULL != Node);
UEdGraphPin* OutputPin = NULL;
for (int32 PinIndex = 0; PinIndex < Node->Pins.Num(); ++PinIndex)
{
UEdGraphPin* Pin = Node->Pins[PinIndex];
if (Pin && (EGPD_Output == Pin->Direction))
{
OutputPin = Pin;
break;
}
}
check(NULL != OutputPin);
return OutputPin;
}
void UK2Node_FormatError::PostReconstructNode()
{
@@ -298,6 +281,45 @@ void UK2Node_FormatError::PostReconstructNode()
}
}
// Get a function that can convert the specified type into a FFormatArgumentData.
//
UFunction *ToFormatArgumentData(const UEdGraphSchema_K2 *Schema, const FEdGraphPinType& PinType, bool AllowWild)
{
// Special case. Wildcard Pins are unconnected pins.
//
if (PinType.PinCategory == UEdGraphSchema_K2::PC_Wildcard && AllowWild)
{
return UlxFormatDataLibrary::StaticClass()->FindFunctionByName(GET_MEMBER_NAME_CHECKED(UlxFormatDataLibrary, FormatArgumentDataBlank));
}
// Try to find an exact match in the UlxFormatDataLibrary.
//
for (auto It = TFieldIterator<UFunction>(UlxFormatDataLibrary::StaticClass()); It; ++It)
{
UFunction* Function = *It;
FProperty* ValueProperty = Function->FindPropertyByName(TEXT("Value"));
FEdGraphPinType ValuePinType;
bool Convertible = Schema->ConvertPropertyToPinType(ValueProperty, ValuePinType);
if (!Convertible) continue;
if (!Schema->ArePinTypesEquivalent(PinType, ValuePinType)) continue;
return Function;
}
// A case for subclasses of 'Object' which are not exactly 'Object'
//
if (PinType.PinCategory == UEdGraphSchema_K2::PC_Object)
{
return UlxFormatDataLibrary::StaticClass()->FindFunctionByName(GET_MEMBER_NAME_CHECKED(UlxFormatDataLibrary, FormatArgumentDataObject));
}
// We don't have a match.
//
return nullptr;
};
void UK2Node_FormatError::ExpandNode(class FKismetCompilerContext& CompilerContext, UEdGraph* SourceGraph)
{
Super::ExpandNode(CompilerContext, SourceGraph);
@@ -334,136 +356,30 @@ void UK2Node_FormatError::ExpandNode(class FKismetCompilerContext& CompilerConte
FString OriginalName = PinNames[ArgIdx];
UEdGraphPin* ArgumentPin = FindPin(ArgumentNameAddPrefix(OriginalName), EGPD_Input);
static UScriptStruct* FormatArgumentDataStruct = FindObjectChecked<UScriptStruct>(FindObjectChecked<UPackage>(nullptr, TEXT("/Script/Engine")), TEXT("FormatArgumentData"));
// Spawn a "Make Struct" node to create the struct needed for formatting the text.
UK2Node_MakeStruct* MakeFormatArgumentDataStruct = CompilerContext.SpawnIntermediateNode<UK2Node_MakeStruct>(this, SourceGraph);
MakeFormatArgumentDataStruct->StructType = FormatArgumentDataStruct;
MakeFormatArgumentDataStruct->AllocateDefaultPins();
MakeFormatArgumentDataStruct->bMadeAfterOverridePinRemoval = true;
CompilerContext.MessageLog.NotifyIntermediateObjectCreation(MakeFormatArgumentDataStruct, this);
// Set the struct's "ArgumentName" pin literal to be the argument pin's name.
MakeFormatArgumentDataStruct->GetSchema()->TrySetDefaultValue(*MakeFormatArgumentDataStruct->FindPinChecked(GET_MEMBER_NAME_STRING_CHECKED(FFormatArgumentData, ArgumentName)), OriginalName);
UEdGraphPin* ArgumentTypePin = MakeFormatArgumentDataStruct->FindPinChecked(GET_MEMBER_NAME_STRING_CHECKED(FFormatArgumentData, ArgumentValueType));
// Move the connection of the argument pin to the correct argument value pin, and also set the correct argument type based on the pin that was hooked up.
if (ArgumentPin->LinkedTo.Num() > 0)
// Find a function that can convert the input into an FFormatArgumentData.
UFunction *Converter = ToFormatArgumentData(Schema, ArgumentPin->PinType, true);
if (Converter == nullptr)
{
const FName& ArgumentPinCategory = ArgumentPin->PinType.PinCategory;
// Adds an implicit conversion node to this argument based on its function and pin name
auto AddConversionNode = [&](const FName FuncName, const TCHAR* PinName)
{
// Set the default value if there was something passed in, or default to "Text"
MakeFormatArgumentDataStruct->GetSchema()->TrySetDefaultValue(*ArgumentTypePin, TEXT("Text"));
// Spawn conversion node based on the given function name
UK2Node_CallFunction* ToTextFunction = CompilerContext.SpawnIntermediateNode<UK2Node_CallFunction>(this, SourceGraph);
ToTextFunction->SetFromFunction(UKismetTextLibrary::StaticClass()->FindFunctionByName(FuncName));
ToTextFunction->AllocateDefaultPins();
CompilerContext.MessageLog.NotifyIntermediateObjectCreation(ToTextFunction, this);
CompilerContext.MovePinLinksToIntermediate(*ArgumentPin, *ToTextFunction->FindPinChecked(PinName));
ToTextFunction->FindPinChecked(UEdGraphSchema_K2::PN_ReturnValue)->MakeLinkTo(MakeFormatArgumentDataStruct->FindPinChecked(GET_MEMBER_NAME_STRING_CHECKED(FFormatArgumentData, ArgumentValue)));
};
if (ArgumentPinCategory == UEdGraphSchema_K2::PC_Int)
{
MakeFormatArgumentDataStruct->GetSchema()->TrySetDefaultValue(*ArgumentTypePin, TEXT("Int"));
// Need a manual cast from int -> int64
UK2Node_CallFunction* CallFloatToDoubleFunction = CompilerContext.SpawnIntermediateNode<UK2Node_CallFunction>(this, SourceGraph);
CallFloatToDoubleFunction->SetFromFunction(UKismetMathLibrary::StaticClass()->FindFunctionByName(GET_MEMBER_NAME_CHECKED(UKismetMathLibrary, Conv_IntToInt64)));
CallFloatToDoubleFunction->AllocateDefaultPins();
CompilerContext.MessageLog.NotifyIntermediateObjectCreation(CallFloatToDoubleFunction, this);
// Move the byte output pin to the input pin of the conversion node
CompilerContext.MovePinLinksToIntermediate(*ArgumentPin, *CallFloatToDoubleFunction->FindPinChecked(TEXT("InInt")));
// Connect the int output pin to the argument value
CallFloatToDoubleFunction->FindPinChecked(UEdGraphSchema_K2::PN_ReturnValue)->MakeLinkTo(MakeFormatArgumentDataStruct->FindPinChecked(GET_MEMBER_NAME_STRING_CHECKED(FFormatArgumentData, ArgumentValueInt)));
}
else if (ArgumentPinCategory == UEdGraphSchema_K2::PC_Real)
{
if (ArgumentPin->PinType.PinSubCategory == UEdGraphSchema_K2::PC_Float)
{
MakeFormatArgumentDataStruct->GetSchema()->TrySetDefaultValue(*ArgumentTypePin, TEXT("Float"));
CompilerContext.MovePinLinksToIntermediate(*ArgumentPin, *MakeFormatArgumentDataStruct->FindPinChecked(GET_MEMBER_NAME_STRING_CHECKED(FFormatArgumentData, ArgumentValueFloat)));
}
else if (ArgumentPin->PinType.PinSubCategory == UEdGraphSchema_K2::PC_Double)
{
MakeFormatArgumentDataStruct->GetSchema()->TrySetDefaultValue(*ArgumentTypePin, TEXT("Double"));
CompilerContext.MovePinLinksToIntermediate(*ArgumentPin, *MakeFormatArgumentDataStruct->FindPinChecked(GET_MEMBER_NAME_STRING_CHECKED(FFormatArgumentData, ArgumentValueDouble)));
}
else
{
check(false);
}
}
else if (ArgumentPinCategory == UEdGraphSchema_K2::PC_Int64)
{
MakeFormatArgumentDataStruct->GetSchema()->TrySetDefaultValue(*ArgumentTypePin, TEXT("Int64"));
CompilerContext.MovePinLinksToIntermediate(*ArgumentPin, *MakeFormatArgumentDataStruct->FindPinChecked(GET_MEMBER_NAME_STRING_CHECKED(FFormatArgumentData, ArgumentValueInt)));
}
else if (ArgumentPinCategory == UEdGraphSchema_K2::PC_Text)
{
MakeFormatArgumentDataStruct->GetSchema()->TrySetDefaultValue(*ArgumentTypePin, TEXT("Text"));
CompilerContext.MovePinLinksToIntermediate(*ArgumentPin, *MakeFormatArgumentDataStruct->FindPinChecked(GET_MEMBER_NAME_STRING_CHECKED(FFormatArgumentData, ArgumentValue)));
}
else if (ArgumentPinCategory == UEdGraphSchema_K2::PC_Byte && !ArgumentPin->PinType.PinSubCategoryObject.IsValid())
{
MakeFormatArgumentDataStruct->GetSchema()->TrySetDefaultValue(*ArgumentTypePin, TEXT("Int"));
// Need a manual cast from byte -> int
UK2Node_CallFunction* CallByteToIntFunction = CompilerContext.SpawnIntermediateNode<UK2Node_CallFunction>(this, SourceGraph);
CallByteToIntFunction->SetFromFunction(UKismetMathLibrary::StaticClass()->FindFunctionByName(GET_MEMBER_NAME_CHECKED(UKismetMathLibrary, Conv_ByteToInt64)));
CallByteToIntFunction->AllocateDefaultPins();
CompilerContext.MessageLog.NotifyIntermediateObjectCreation(CallByteToIntFunction, this);
// Move the byte output pin to the input pin of the conversion node
CompilerContext.MovePinLinksToIntermediate(*ArgumentPin, *CallByteToIntFunction->FindPinChecked(TEXT("InByte")));
// Connect the int output pin to the argument value
CallByteToIntFunction->FindPinChecked(UEdGraphSchema_K2::PN_ReturnValue)->MakeLinkTo(MakeFormatArgumentDataStruct->FindPinChecked(GET_MEMBER_NAME_STRING_CHECKED(FFormatArgumentData, ArgumentValueInt)));
}
else if (ArgumentPinCategory == UEdGraphSchema_K2::PC_Byte || ArgumentPinCategory == UEdGraphSchema_K2::PC_Enum)
{
static UEnum* TextGenderEnum = FindObjectChecked<UEnum>(nullptr, TEXT("/Script/Engine.ETextGender"), /*ExactClass*/true);
if (ArgumentPin->PinType.PinSubCategoryObject == TextGenderEnum)
{
MakeFormatArgumentDataStruct->GetSchema()->TrySetDefaultValue(*ArgumentTypePin, TEXT("Gender"));
CompilerContext.MovePinLinksToIntermediate(*ArgumentPin, *MakeFormatArgumentDataStruct->FindPinChecked(GET_MEMBER_NAME_STRING_CHECKED(FFormatArgumentData, ArgumentValueGender)));
}
}
else if (ArgumentPinCategory == UEdGraphSchema_K2::PC_Boolean)
{
AddConversionNode(GET_MEMBER_NAME_CHECKED(UKismetTextLibrary, Conv_BoolToText), TEXT("InBool"));
}
else if (ArgumentPinCategory == UEdGraphSchema_K2::PC_Name)
{
AddConversionNode(GET_MEMBER_NAME_CHECKED(UKismetTextLibrary, Conv_NameToText), TEXT("InName"));
}
else if (ArgumentPinCategory == UEdGraphSchema_K2::PC_String)
{
AddConversionNode(GET_MEMBER_NAME_CHECKED(UKismetTextLibrary, Conv_StringToText), TEXT("InString"));
}
else if (ArgumentPinCategory == UEdGraphSchema_K2::PC_Object)
{
AddConversionNode(GET_MEMBER_NAME_CHECKED(UKismetTextLibrary, Conv_ObjectToText), TEXT("InObj"));
}
else
{
// Unexpected pin type!
CompilerContext.MessageLog.Error(*FText::Format(LOCTEXT("Error_UnexpectedPinType", "Pin '{0}' has an unexpected type: {1}"), FText::FromString(OriginalName), FText::FromName(ArgumentPinCategory)).ToString());
}
CompilerContext.MessageLog.Error(TEXT("Cannot convert Pin to a Format Argument"));
continue;
}
else
// Add a node to call the converter.
UK2Node_CallFunction* ConvertNode = CompilerContext.SpawnIntermediateNode<UK2Node_CallFunction>(this, SourceGraph);
ConvertNode->SetFromFunction(Converter);
ConvertNode->AllocateDefaultPins();
CompilerContext.MessageLog.NotifyIntermediateObjectCreation(ConvertNode, this);
UEdGraphPin *ValuePin = ConvertNode->FindPin(TEXT("Value"));
UEdGraphPin *NamePin = ConvertNode->FindPinChecked(TEXT("Name"));
// Set a value for the 'Name' pin of the converter.
ConvertNode->GetSchema()->TrySetDefaultValue(*NamePin, OriginalName);
// Connect the Value pin of the converter.
if (ValuePin != nullptr)
{
// No connected pin - just default to an empty text
MakeFormatArgumentDataStruct->GetSchema()->TrySetDefaultValue(*ArgumentTypePin, TEXT("Text"));
MakeFormatArgumentDataStruct->GetSchema()->TrySetDefaultText(*MakeFormatArgumentDataStruct->FindPinChecked(GET_MEMBER_NAME_STRING_CHECKED(FFormatArgumentData, ArgumentValue)), FText::GetEmpty());
CompilerContext.MovePinLinksToIntermediate(*ArgumentPin, *ValuePin);
}
// The "Make Array" node already has one pin available, so don't create one for ArgIdx == 0
@@ -477,7 +393,7 @@ void UK2Node_FormatError::ExpandNode(class FKismetCompilerContext& CompilerConte
UEdGraphPin* InputPin = MakeArrayNode->FindPinChecked(PinName);
// Find the output for the pin's "Make Struct" node and link it to the corresponding pin on the "Make Array" node.
FindOutputStructPinChecked(MakeFormatArgumentDataStruct)->MakeLinkTo(InputPin);
ConvertNode->GetReturnValuePin()->MakeLinkTo(InputPin);
}
// Move connection of "Format" pin to the call function's "InPattern" pin
@@ -553,26 +469,10 @@ bool UK2Node_FormatError::IsConnectionDisallowed(const UEdGraphPin* MyPin, const
const UEdGraphSchema_K2* K2Schema = Cast<const UEdGraphSchema_K2>(GetSchema());
const FName& OtherPinCategory = OtherPin->PinType.PinCategory;
bool bIsValidType = false;
if (OtherPinCategory == UEdGraphSchema_K2::PC_Int || OtherPinCategory == UEdGraphSchema_K2::PC_Real || OtherPinCategory == UEdGraphSchema_K2::PC_Text ||
(OtherPinCategory == UEdGraphSchema_K2::PC_Byte && !OtherPin->PinType.PinSubCategoryObject.IsValid()) ||
OtherPinCategory == UEdGraphSchema_K2::PC_Boolean || OtherPinCategory == UEdGraphSchema_K2::PC_String || OtherPinCategory == UEdGraphSchema_K2::PC_Name || OtherPinCategory == UEdGraphSchema_K2::PC_Object ||
OtherPinCategory == UEdGraphSchema_K2::PC_Wildcard || OtherPinCategory == UEdGraphSchema_K2::PC_Int64)
{
bIsValidType = true;
}
else if (OtherPinCategory == UEdGraphSchema_K2::PC_Byte || OtherPinCategory == UEdGraphSchema_K2::PC_Enum)
UFunction *Converter = ToFormatArgumentData(K2Schema, OtherPin->PinType, false);
if (Converter == nullptr)
{
static UEnum* TextGenderEnum = FindObjectChecked<UEnum>(nullptr, TEXT("/Script/Engine.ETextGender"), /*ExactClass*/true);
if (OtherPin->PinType.PinSubCategoryObject == TextGenderEnum)
{
bIsValidType = true;
}
}
if (!bIsValidType)
{
OutReason = LOCTEXT("Error_InvalidArgumentType", "Format arguments may only be Byte, Integer, Int64, Float, Double, Text, String, Name, Boolean, Object, Wildcard or ETextGender.").ToString();
OutReason = LOCTEXT("Error_InvalidArgumentType", "Data cannot be converted to text.").ToString();
return true;
}
}