美文网首页
UObject调用上下文函数(WorldContext,UWor

UObject调用上下文函数(WorldContext,UWor

作者: Moo2077 | 来源:发表于2024-01-18 20:32 被阅读0次

UObject通常无法调用任何需要传入WorldContext的方法,例如延时函数Delay,TimeLine或者某个SubSystem:

image.png

Delay这种上下文函数根本没办法创建,而Subsystem则会报错

image.png
  • 蓝图宏定义中会检查是否有传入WorldContext或重写了GetWorld()函数
//蓝图如果定义了这句话则不能在UObject中调用
UFUNCTION(BlueprintCallable, meta=(WorldContext="WorldContextObject")
void ChildFunction(UObject* WorldContextObject)

主要原因

  • 这里卡主了不让UObject正常使用上下文函数
蓝图检测.png

解决方法:
新建UObject子类并重写下列方法

class YOUR_API UChildObject : public UObject
{
    GENERATED_BODY()
public:
        //UObject work flow
    virtual UWorld* GetWorld() const override;
    virtual bool ImplementsGetWorld() const override;
}

.CPP中重写

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)
    {
        world = GEngine->GetWorldContexts()[0].World();
    }
    return world;
}

bool UChildObject ::ImplementsGetWorld() const
{
    return true;
}

在UE编辑器中创建蓝图BlueprintClass并继承上述子类,就会愉快的发现可以调用任何函数了

UE 5.3环境

相关文章

  • 7月18日 中雨

    1. 方法调用和函数调用最大的区别在于调用上下文,方法调用的上下文通常是实例化的对象,函数调用的上下文通常是win...

  • this的指向问题

    函数的调用姿态 js完整的调用姿态是 上下文.函数(),也就是说函数并不能真正单独调用,他一定是被某个上下文调用...

  • 函数上下文

    函数上下文 一、函数执行时,函数的上下文是window. 二、函数作为对象的方法,对象调用方法时,函数的上下文即为...

  • JS函数调用方式

    1.函数调用 2.方法调用 3.构造函数 4.上下文调用: call, apply, bind

  • js原型和闭包(11)——执行上下文栈

    执行全局代码时,会产生一个执行上下文环境,每次调用函数都又会产生执行上下文环境。当函数调用完成时,这个上下文环境以...

  • JavaScript call() , apply() , bi

    上下文 函数的每次调用都会拥有一个特殊值—本次调用的上下文(context),这就是this关键字的值。 如果函数...

  • javascript之this探究

    this是基于上下文环境来变动的 可以用不同的方法进行函数调用,不用的调用机制决定了函数上下文的不同。调用者 ca...

  • this

    this代表了函数运行时,是函数运行上下文 纯函数调用,此时this指向全局对象global 作为构造函数调用,此...

  • JS面向对象--4函数的上下文之五规律

    函数的上下文就是指this是谁规律1:函数用圆括号直接调用 ,函数的上下文是window对象 函数function...

  • block实质

    block实质是:封装了函数及其执行上下文的对象 block调用的本质是:函数的调用

网友评论

      本文标题:UObject调用上下文函数(WorldContext,UWor

      本文链接:https://www.haomeiwen.com/subject/tgldodtx.html