I google very much to get the best practice of using custom singleton in UE4.
I find out putting instance in game instance is a good way. However, if one object register as a listener to the singleton instance, there will be some issue.
UCLASS()
class UCFGameInstance : public UGameInstance {
GENERATED_BODY()
.
.
UPROPERTY()
UCFLevelStore* LevelStore;
public:
UCFGameInstance();
UCFLevelStore* GetLevelStore();
}
UCLASS()
class UCFLevelStore: public UObject {
GENERATED_BODY()
protected:
UPROPERTY()
TArray<TScriptInterface<ICFLevelStoreInterface>> LevelStoreListeners;
public:
UCFLevelStore();
void AddListener(ICFLevelStoreInterface* listener);
}
// In my custom Hud class
UCFGameInstance* Instance = ((UCFGameInstance*)UGameplayStatics::GetGameInstance(GetWorld()));
Instance->GetLevelStore()->AddListener(this);
[see the attached image below]