You’re not going to believe this… I had ChatGPT fix my code!
I tried using ChatGPT from scratch with just a prompt, but it ended up using entirely different function calls that created nodes… that were just hollow shells.
So, I gave it the code that I had made that I thought was the closest, and with just 2 fixes it works!
UEdGraphNode* UMakeBlueprintNodeBPLibrary::AddNodeToBlueprint3(UBlueprint* Blueprint, FName FunctionName, UClass* Class, FVector Location)
{
if (!Blueprint)
{
// Blueprint is null, handle the error
return nullptr;
}
if (!Class)
{
// The blueprint class is null, handle the error
return nullptr;
}
//FName FunctionName = FName(TEXT("CreateBlueprint"));
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>(EventGraph);
UBlueprintFunctionNodeSpawner* FunctionNodeSpawner = UBlueprintFunctionNodeSpawner::Create(Function);
if (FunctionNodeSpawner)
{
FunctionNodeSpawner->SetFlags(RF_Transactional);
NewNode = FunctionNodeSpawner->Invoke(EventGraph, IBlueprintNodeBinder::FBindingSet(), FVector2D(Location.X, Location.Y));
return NewNode;
}
return nullptr;
}