Figured it out. -
Start with an interface:
IDebugHelper.h
#pragma once
struct IDebugHelper
{
public:
virtual void WriteInfo() = 0;
};
DebugHelper.cpp
#include "TEST.h"
#include "IDebugHelper.h"
#include "Runtime/Core/Public/Templates/UnrealTypeTraits.h"
Expose_TNameOf(IDebugHelper)
Now for the implementation:
OnScreenDebugHelper.h
#pragma once
// Project includes
#include "Abstracts/IDebugHelper.h"
class OnScreenDebugHelper : public IDebugHelper
{
public:
void WriteInfo() override;
};
OnScreenDebugHelper.cpp
#include "TEST.h"
#include "OnScreenDebugHelper.h"
void OnScreenDebugHelper::WriteInfo()
{
}
Now to register your interface to your implementation on an ioc container
TESTGameInstanceBase.h
#pragma once
// UE includes
#include "Engine/GameInstance.h"
#include "Runtime/Core/Public/Misc/TypeContainer.h"
#include "TESTGameInstanceBase.generated.h"
class TEST_API UTESTGameInstanceBase : public UGameInstance
{
GENERATED_BODY()
public:
UTESTGameInstanceBase();
TTypeContainer<>& GetIOCContainer() { return this->iOCContainer; }
private:
TTypeContainer<> iOCContainer;
};
TESTGameInstanceBase.cpp
#include "TEST.h"
#include "Models/GameInstances/TESTGameInstanceBase.h"
//
#include "Abstracts/IDebugHelper.h"
#include "Helpers/OnScreenDebugHelper.h"
UTESTGameInstanceBase::UTESTGameInstanceBase() {
this->iOCContainer.RegisterClass<IDebugHelper, UOnScreenDebugHelper>(ETypeContainerScope::Instance);
}
And finally, to use it:
if (GetWorld() != nullptr)
{
if (this->GetGameInstance() != nullptr)
{
TSharedPtr<IDebugHelper> debugHelper = ((UMainGameInstance*)this->GetGameInstance())->GetIOCContainer().GetInstance<IDebugHelper>();
}
}