Accessing GameInstanceSubsystem from UOjbect-derived blueprint

Hey folks,

I have created a subsystem which I’d like to access in a non-actor blueprint, however I’m getting the classic error:

Pin  Context Object  must have a connection

Now, I can access this subsystem from a GameplayAbility blueprint, and I’ve read about how I need to override GetWorld() in my blueprint class, which I have done as follows:

UWorld* UInteraction::GetWorld() const
{
	return GetOuter() ? GetOuter()->GetWorld() : nullptr;
}

but it doesn’t seem to help. I am not sure what I need to do that UGameplayAbilitiy has done to make this work.

Any ideas?

You need to override getworld in your class based off of uobject


#pragma once

#include "CoreMinimal.h"
#include "UObject/NoExportTypes.h"
#include "MyObject.generated.h"

UCLASS(Blueprintable, BlueprintType)
class YOUR_API UMyObject : public UObject
{
	GENERATED_BODY()
	
    UWorld* GetWorld() const override
    {
        if (IsTemplate() || !GetOuter()) 
        {
            return nullptr;
        }
        return GetOuter()->GetWorld();
    }
};

1 Like

That worked perfectly, thank you! I’m just not sure why…

Is it because when editing a blueprint in the editor there is no world to access, so the IsTemplate() check ensures that nullptr is passed through?

If you look into the source code of the definition of isTemplated

/**

  • Determines whether this object is a template object
  • @return true if this object is a template object (owned by a UClass)
    */

If you compile the uobject containing the subsystem you can see the debug info (extended to see variables)

Shows that is template is true and get outer returns nothing (as you are not in game yet so it’s not really valid yet). So you return nullptr as the world context, so it’s empty, but it IS returned as something so it’s not seen as invalid.

And once you do start your game Begin play will kick in and actually make the world context valid later on :slight_smile:

1 Like

Great, thanks for explaining!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.