This is possible. The short (simplified) answer is all you have to do is override GetWorld()
to get the methods to show up. Hooray!
The reason why that wasn’t working for me (and probably a bunch of other people) is, if your implementation of GetWorld() called another objects GetWorld, for example:
// We know this object is always attached to an ActorComponent which does implement GetWorld
return GetOuter()->GetWorld();
This would cause it to fail.
#The problem
Skip to the fix if you are not interested
For the functions to show up in the BP editor, UObject::ImplementsGetWorld()
must return true. This function works by calling GetWorld
and seeing if a variable called bGetWorldOverridden
(which it sets to true) is set back to false by the default implementation of GetWorld
. That’s fine - you’ve overridden GetWorld
so it shouldn’t right?
Unfortunately, the ImplementsGetWorld
is called on the CDO (the class default object), whose outer isn’t something sensible like the ActorComponent you always attach your object. This new outer doesn’t implement GetWorld so bGetWorldOverriden (a global variable) gets set back to false, meaning the method doesn’t show up in the blueprint editor.
#The fix
An incredibly ugly fix you can do to force this to work is as follows:
if (GetOuter()->IsA(UActorComponent::StaticClass()))
{
return GetOuter()->GetWorld();
}
else
{
return nullptr;
}
Here, we check if the outer is something sensible (in my case, I know it is an ActorComponent, so I can check the outer is indeed an ActorComponent. If it is, we are a normal object so we can call my correct GetWorld implementation on the Outer.
If the outer is not an ActorComponent then (I’m assuming) we are the CDO and we are being tested to see if we actually implement GetWorld(). It doesn’t matter than we return null, the key thing is we don’t call UObject::GetWorld so the editor thinks we have implemented GetWorld. It therefore shows the methods.