How to create/add a new function from C++?

Hey, I am currently trying to spawn a function graph within my UBlueprint. It works but when I compile that new graph I get this compile error message.


Here is my code:

void UMyBlueprintFunctionLibrary::CreateFunctionGraphs()
{
    TSubclassOf<UEdGraphSchema> MySchema2 = UEdGraphSchema_K2::StaticClass();
    UEdGraph* EventGraph = CastChecked<UEdGraph>(FBlueprintEditorUtils::CreateNewGraph(GEngine, NAME_None, UEdGraph::StaticClass(), UEdGraphSchema_K2::StaticClass()));
    if (EventGraph) {
        EventGraph->Rename(*FString("EdGraph_0"));
        const UEdGraphSchema* Schema = EventGraph->GetSchema();
        Schema->CreateDefaultNodesForGraph(*EventGraph);
        GetBlueprintFromObject(GetSelectedBlueprint())->FunctionGraphs.Add(EventGraph);
        FBlueprintEditorUtils::MarkBlueprintAsStructurallyModified(GetBlueprintFromObject(GetSelectedBlueprint()));
        FKismetEditorUtilities::CompileBlueprint(GetBlueprintFromObject(GetSelectedBlueprint()));
    }
}

And there indeed is no root node like this.
grafik
How do I add that?
Someone told me to use FBlueprintEditorUtils::CreateFunctionGraph but I also had struggles with using that.

Thank you!

I managed to fix this! :+1: