UObject通常無(wú)法調(diào)用任何需要傳入WorldContext的方法,例如延時(shí)函數(shù)Delay,TimeLine或者某個(gè)SubSystem:
image.png
Delay這種上下文函數(shù)根本沒(méi)辦法創(chuàng)建,而Subsystem則會(huì)報(bào)錯(cuò)
image.png
- 藍(lán)圖宏定義中會(huì)檢查是否有傳入WorldContext或重寫(xiě)了GetWorld()函數(shù)
//藍(lán)圖如果定義了這句話則不能在UObject中調(diào)用
UFUNCTION(BlueprintCallable, meta=(WorldContext="WorldContextObject")
void ChildFunction(UObject* WorldContextObject)
主要原因
- 這里卡主了不讓UObject正常使用上下文函數(shù)
藍(lán)圖檢測(cè).png
解決方法:
新建UObject子類(lèi)并重寫(xiě)下列方法
class YOUR_API UChildObject : public UObject
{
GENERATED_BODY()
public:
//UObject work flow
virtual UWorld* GetWorld() const override;
virtual bool ImplementsGetWorld() const override;
}
.CPP中重寫(xiě)
UWorld* UChildObject ::GetWorld() const
{
// CDO objects do not have a world.
// If the object outer is destroyed or unreachable we are likely shutting down and the world should be nullptr.
if (UObject* Outer = GetOuter())
{
if (!HasAnyFlags(RF_ClassDefaultObject)
&& !Outer->HasAnyFlags(RF_BeginDestroyed)
&& !Outer->IsUnreachable())
{
return Outer->GetWorld();
}
}
UWorld* world = nullptr;//= Super::GetWorld();
if (UObject* Outer = GetOuter())
{
world = Outer->GetWorld();
}
if(world == nullptr)
{
for(auto context : GEngine->GetWorldContexts())
{
if(context.WorldType != EWorldType::Editor || context.WorldType != EWorldType::EditorPreview)
{
world = context.World();
}
}
}
if(world == nullptr)
{
FWorldContext* worldContext = GEngine->GetWorldContextFromGameViewport(GEngine->GameViewport);
if (!worldContext)) world = worldContext->World();
}
return world;
}
bool UChildObject ::ImplementsGetWorld() const
{
return true;
}
在UE編輯器中創(chuàng)建藍(lán)圖BlueprintClass并繼承上述子類(lèi),就會(huì)愉快的發(fā)現(xiàn)可以調(diào)用任何函數(shù)了
UE 5.3環(huán)境