据别人说:
Actor
具备挂载组件的能力,因此可以通过Scene组件,拥有Transform,因而具有坐标的概念。
Object
不具备挂载组件的能力,因此不具备坐标概念。
因此UObject
要想获得WorldContext
只能通过中介
,让能够直接获取世界实例的实例把世界信息间接告诉UObject
meta=(WorldContext="WorldContextObject")
通过meta
,自动获取调用它的对象,将其赋值给它指定的参数(meta=(WorldContext="WorldContextObject"))
,这样我们就能通过WorldContextObject
来间接获取当前的世界
UE4中使用meta=(WorldContext="WorldContextObject")
的地方
全局的一些功能库中,例如
1.GetAllActorsWithTag
// .h
UFUNCTION(BlueprintCallable, Category="Utilities", meta=(WorldContext="WorldContextObject"))
static void GetAllActorsWithTag(const UObject* WorldContextObject,
FName Tag, TArray<AActor*>& OutActors);
// .cpp
void UGameplayStatics::GetAllActorsWithTag(const UObject* WorldContextObject, FName Tag, TArray<AActor*>& OutActors)
{
QUICK_SCOPE_CYCLE_COUNTER(UGameplayStatics_GetAllActorsWithTag);
OutActors.Reset();
// We do nothing if no tag is provided, rather than giving ALL actors!
if (!Tag.IsNone())
{
if (UWorld* World = GEngine->GetWorldFromContextObject(WorldContextObject,
EGetWorldErrorMode::LogAndReturnNull))
{
for (FActorIterator It(World); It; ++It)
{
AActor* Actor = *It;
if (Actor->ActorHasTag(Tag))
{
OutActors.Add(Actor);
}
}
}
}
}
以上遍历Actor
的方法使用迭代器FActorIterator
该方法的使用是在When Playing in Editor
/**
* Actor iterator
* Note that when Playing In Editor, this will find actors only in CurrentWorld
*/
class FActorIterator : public TActorIteratorBase<FActorIterator>
{
friend class TActorIteratorBase<FActorIterator>;
typedef TActorIteratorBase<FActorIterator> Super;
}
2.蓝图异步节点
一般要继承自UBlueprintAsyncActionBase
UFUNCTION(BlueprintCallable, meta = (BlueprintInternalUseOnly = "true", WorldContext = "WorldContextObject"))
static UBPAsyncNode_Custom* CustomAsyncBPNode(UObject* WorldContextObject);