I have a chunk of code that works flawlessly… Once. If I run it a second time I get “Access violation executing location” error.
This function is called many times, and only errors when the Function is “compare”, on the 2nd execution.
Sometimes the declaration of Function results in “Invalid name length”, sometimes not.
Sometimes the “function” is correct, but the "FunctionNodeSpawner " declaration results in a nullptr.
Some times it results in no pins.
It’s like I’m failing to do some clean-up somewhere… but I thought that was all automatic.
Note: The program creates a file, and I have to delete this file before running it again.
if (ComparePinType != “None”) is only true when Function = “Compare”
UEdGraphNode* UMakeBlueprintNodeBPLibrary::AddFunctionNodeToBlueprint(UEdGraph* Graph, FName FunctionName, UClass* Class, FVector2D Location, FName ComparePinType)
{
if (!Graph)
{
// Blueprint is null, handle the error
return nullptr;
}
if (!Class)
{
// The blueprint class is null, handle the error
return nullptr;
}
UFunction* Function = Class->FindFunctionByName(FunctionName);
if (!Function)
{
// The function does not exist in the blueprint class, handle the error
UE_LOG(LogTemp, Warning, TEXT("Function Not Found"));
return nullptr;
}
// Get the Event Graph of the blueprint
//UEdGraph* EventGraph = Blueprint->UbergraphPages[0];
// Create a new function node in the Event Graph
UEdGraphNode* NewNode = NewObject<UEdGraphNode>(Graph);
UBlueprintFunctionNodeSpawner* FunctionNodeSpawner = UBlueprintFunctionNodeSpawner::Create(Function);
if (FunctionNodeSpawner)
{
FunctionNodeSpawner->SetFlags(RF_Transactional);
NewNode = FunctionNodeSpawner->Invoke(Graph, IBlueprintNodeBinder::FBindingSet(), FVector2D(Location.X, Location.Y));
if (ComparePinType != "None")
{
TArray<UEdGraphPin*> Pins;
Pins = NewNode->GetAllPins();
if (Pins.Num() > 0)
{
for (UEdGraphPin* Pin : Pins)
{
if (Pin->PinName == FName(TEXT("A")) || Pin->PinName == FName(TEXT("B")))
{
if (ComparePinType == FName(TEXT("Vector")) || ComparePinType == FName(TEXT("vector")))
{
Pin->PinType.PinCategory = UEdGraphSchema_K2::PC_Struct;
Pin->PinType.PinSubCategoryObject = TBaseStructure<FVector>::Get();
}
else if (ComparePinType == FName(TEXT("double")))
{
Pin->PinType.PinCategory = TEXT("real");
Pin->PinType.PinSubCategory = ComparePinType;
}
else if (ComparePinType == FName(TEXT("color")))
{
Pin->PinType.PinCategory = UEdGraphSchema_K2::PC_Struct;
Pin->PinType.PinSubCategoryObject = TBaseStructure<FColor>::Get();
}
else
{
Pin->PinType.PinCategory = ComparePinType;
}
}
}
}
}
return NewNode;
}
return nullptr;
}