When I create a Blueprint Function Library, the functions are only available on blueprints that inherit from Actor. Is there a way to change this, or an equivelent feature that also works on non-actors? I have multiple non-actor classes that I expect to inherit from a lot, and making them all into actors isn’t really an option without bloating the level.
Edit: They also seem to work fine in level blueprints, actor components and some other built-in classes, but not from “Object” directly or custom classes that inherit from it.
I guess you could inherit from Object, and then immediately put your whole library in that BP, and then carry on from there.
This is a long-standing issue, apparently
The following may not be entirely accurate anymore since its been a while I look into this. From what I remember function libraries need a “World Context” object (hidden function input), essentially any UObject
that implements GetWorld
. Actors, ActorComponents and a few other classes implement GetWorld
accordingly so these can use the function library functions. All classes that directly inherit from UObject
have the default implementation of GetWorld
(following the outer-chain). However this default implementation is not considered “valid” because the class does not explicitly override GetWorld
resulting in functions requiring a “World Context” object not being callable.
There are two solutions I’m aware of (unfortunately both involve some native code):
- either create a C++ class derived from UObject where you implement
GetWorld
in a valid way. You can find a couple of examples for this if you look at existingGetWorld
implementations from the engine itself. This will make the function library functions callable again in classes inheriting this new class. For some reason the “World Context” object pin will be visible and must be connected manually though (unlike Actors, Components, etc.). Using aSelf
node is sufficient for that though. - create an empty C++ class derived from UObject and add
meta=(ShowWorldContextPin)
in theUCLASS(...)
macro. The function library functions should be callable again for classes derived from this new class. And as the name of the metadata suggests the “World Context” object pin will also be visible here and must be connected manually. Since the object itself may or may not have a valid way toGetWorld
(depending on the outer) you need to decide whether you can passSelf
or another object as the “World Context”
Thanks everyone who replied!
I went with the GetWorld approach and used a “manager” object for the world context. It worked even better than expected because the “World Context” pin didn’t show up. I don’t know why, maybe it’s the method I used of getting the context.