Download

Calling blueprint implemented library functions from C++

hi unreal enthusiasts

Let’s say I have a blueprint function library which has been implemented in blueprint, not in C++. It’s path is “Blueprint’/Game/TMP/MyBlueprintFuncLib.MyBlueprintFuncLib’”, it has a function “MyBlueprintFunc”. I am in a C++ context and I would like to call that function.

What I have been trying so far:



UBlueprint* BlueprintLibPtr = LoadObject<UBlueprint>(NULL, TEXT("/Game/TMP/MyBlueprintFuncLib.MyBlueprintFuncLib"), NULL, 0, NULL);
// success, I get a pointer to the function library!

UFunction* unrealFunction = BlueprintLibPtrDCO->FindFunction(TEXT("MyBlueprintFunc"));
// no success, I can't find the function

FOutputDeviceNull nullDevice;
BlueprintLibPtr->CallFunctionByNameWithArguments(TEXT("MyBlueprintFunc"), nullDevice, NULL, true))
// no success, says it can't find the function, probably same problem as above


Questions:

  • How can I achieve the function call?
  • How do I need to specify the function name for BlueprintLibPtrDCO->FindFunction?
  • Could it be that I am trying to make the call too early? (I am calling the code above from a “IModuleInterface::StartupModule” plugin which has LoadingPhase PostEngineInit)
  • I know about the “Game Singleton Class”, which delivers actually more or less about what I want to achieve using the above code. However there seem to be a problem if I move the loading phase of the module (a bug maybe?), the engine crashes until I move certain modules back to LoadingPhase Default resp. until I remove the Game Single Class.

many thanks for your help!

Christian

I believe you don’t want to call FindFunction on the CDO. Doing so will call FindFunctionByName on the UClass returned by GetClass of the CDO. This class will be UBlueprint::StaticClass() or something along those lines NOT the actual UClass generated for the blueprint. Therefore it will be unable to find the function in that class simply because you don’t have the right class you intended.

Try this instead:


UBlueprint* BlueprintLibPtr = LoadObject<UBlueprint>(NULL, TEXT("/Game/TMP/MyBlueprintFuncLib.MyBlueprintFuncLib"), NULL, 0, NULL);
if (BlueprintLibPtr)
{
	UClass* BlueprintGeneratedClass = BlueprintLibPtr->GetBlueprintClass();
	if (BlueprintGeneratedClass)
	{
		UFunction* BlueprintLibraryFunction = BlueprintGeneratedClass->FindFunctionByName(TEXT("MyBlueprintFunc"));
		if (BlueprintLibraryFunction)
		{
			//do stuff with UFunction
		}
	}
}

You can also log out the name of the class BlueprintLibPtr->GetClass() versus BlueprintLibPtr->GetBlueprintClass() to see whether my explanation is correct or not.

Quickly tested your suggestion, doesn’t seem to work. Since I have solved my bigger problem in a different way I´ll have a look at it later. However:

Casting a blueprint function library to UBlueprint is not a good idea (I know its from my initial code) because a blueprint function library is directly a subclass of UObject and not based on UBlueprint. However it also doesn’t crash probably because the used functions are implemented in a common super class anyway.

The FindFunctionByName does not find the function, its always null. I will have a debug session at some point and try to figure out how to do it.