Singleton causes issues when running game without the editor.

I have Actors with dialogue data assigned to them that I want to display in my UI. All my singelton is doing is saving the Current Actor’s dialogue to a variable, and retrieving it when the UI comes up. It doesn’t cause any issues in the editor or if I package the project, but causes a crash if I launch without the editor. How would I go about solving the undefined symbol?

error:


/opt/unreal-engine/Engine/Binaries/Linux/UE4Editor: symbol lookup error: /home/zerophase/Documents/unrealprojects/Dialogues/Binaries/Linux/libUE4Editor-Dialogues-Linux-DebugGame.so: undefined symbol: _ZN10UMessenger14AssignDialogueEP13UBaseDialogue

Launch command:


/path/to/ue4/UE4Editor "/path/to/project/Dialogues/Dialogues.uproject"  -game -debug -targetplatform=LinuxNoEditor

Messenger header:


#pragma once

#include "BaseDialogue.h"


class DIALOGUES_API UMessenger
{


public:
    static UMessenger *Instance();
    UMessenger(UMessenger const&) = delete;
    UMessenger(UMessenger &&) = delete;
    UMessenger &operator=(UMessenger const &) = delete;
    UMessenger &operator=(UMessenger &&) = delete;
    
    void AssignDialogue(UBaseDialogue *baseDialogue);
    UBaseDialogue *GetDialogue();
    
private:
    UMessenger() {}
    ~UMessenger() {}
    
    static UMessenger *instance;
    
    UBaseDialogue *bd;
};


Messenger cpp:


#include "Dialogues.h"#include "Messenger.h"


UMessenger *UMessenger::instance = nullptr;


void UMessenger::AssignDialogue(UBaseDialogue *baseDialogue)
{
    bd = baseDialogue;
}


UBaseDialogue *UMessenger::GetDialogue()
{
    return bd;
}


UMessenger *UMessenger::Instance()
{
    if(!instance)
        instance = new UMessenger();
    return instance;
}




Don’t use raw singletons, they aren’t tracked by UE4’s GC and are of limited use. Instead subclass UGameInstance, which serves basically the same purpose as a singleton except it’s created for you, managed by GC like all UObjects, and isn’t shared between editor and play-in-editor worlds.

Wouldn’t UGameInstance just limit me to one singleton class for the project?

You could just make structs for each singleton type you want and add them to the game instance as UPROPERTY members.

you can use a new module for example, basically it’s a singleton (not by definition but same approach :)), even with Garbage Collector, put your raw singleton inside the plugin module and add 1 unique reference with SharedPtr<> at the begining of your module when you call StartupModule(), mmm not sure if you need delete it at the end, because anyway unreal’s will shutdown everything, just for well write code, you know!

BTW, for the nature of your project, you can develop your approach for your chat saver in a plugin, in that way will be reusable as well

Cheers

Thanks. Modules sound like just what I’m looking for. I don’t really care about it being an actual singelton. I just need a singleton lifetime, like DI has.

For a plugin would I want to drop it in Under developer?