Create UFunction through Code

Does anybody knows how to create UFunction objects and make them show up in a Blueprint’s “Functions” list, using C++ code?!

Note that I don’t mean using UFUNCTION(…) macro in C++, not what I am looking for…
I mean, where are the functions used by the UEditor to add a function to the Blueprint graph when you hit the “+” in Editor, like this:

I want to make an extension capable of adding UFunction to a Blueprint without any C++ parent function, like the Editor does.

Hi. It’s easy if you want to do this in editor plugin. There is a code taken from UEditor which makes a new function here:

			const UEdGraphSchema_K2* K2Schema = GetDefault<UEdGraphSchema_K2>();
			UEdGraph* UCSGraph = FBlueprintEditorUtils::CreateNewGraph(BlueprintObj, K2Schema->FN_UserConstructionScript, UEdGraph::StaticClass(), UEdGraphSchema_K2::StaticClass());
			FBlueprintEditorUtils::AddFunctionGraph(BlueprintObj, UCSGraph, /*bIsUserCreated=*/ false, AActor::StaticClass());
			UCSGraph->bAllowDeletion = false;

BlueprintObj variable declared as UBlueprint*. bAllowDeletion don’t allow user to delete this function.
To view great example of programmatically creation and filling function with nodes you have to find “FBlueprintEditor::CollapseSelectionToFunction” function in source code.

In theory this should work; but I am unable to get the UBlueprint* from the object being edited on the customization panel.

All methods I’ve tried casting to UBlueprint* are just failing without ConstructionHelpers.

Maybe it will be helpful:

UBlueprint* GetBlueprintForObject(UObject* ParentObject)
{
	UBlueprint* ParentBlueprint = NULL;

	if (ParentObject != NULL)
	{
		ParentBlueprint = Cast<UBlueprint>(ParentObject);
		if (ParentBlueprint == NULL)
		{
			ParentBlueprint = Cast<UBlueprint>(ParentObject->GetClass()->ClassGeneratedBy);
		}
	}

	return ParentBlueprint;
}

This is working beautifully!

http://i1234.photobucket.com/albums/ff408/BrUnOXaVIeR/FSMAutoGenerateFunctions_zpsiua76utt.gif

Just one more thing I’m struggling with though…
Would be perfect if I could as well set the Category for the generated blueprint function; I tried UField or UFunction → SetMetadata(), but it didn’t work… Any ideas?

This is working beautifully!

Just one more thing I’m struggling with though…
Would be perfect if I could as well set the Category for the generated blueprint function; I tried UField or UFunction → SetMetadata(), but it didn’t work… Any ideas?

It seems BP compiler takes whole metadata for functions from the entry point nodes. You can try to get entry point node from a function graph and set metadata there:

	TArray<const UK2Node_FunctionEntry*> EntryPoints;
	Graph->GetNodesOfClass(EntryPoints);

There could be a lot of entry points in the graph, but separate function graphs have only one entry normally. FunctionEntry node have its own MetaData entry which is visible in the blueprint editor.

It… just works! :smiley:
Thank you so much, I would upvote a hundred times if I could, thanks again!