Set GameMode in Plugin C++

Hey there,

I am currently developing an unreal plugin, which also contains a gamemode / playercontroller class. So I got this button in the editor working which sets up my scene for the plugin to work, the only thing that is missing is the custom gamemode, which then has to be set in the world settings manually.

I just wanted to ask if there is a possibility to set the current level gamemode in the plugins c++ code? As I said, the gamemode comes packed in the plugins content folder, it just has to be assigned…

Thanks in advance!

Dominik

Hey Dominik,

I don’t know if you still need an answer but I think I have one for you because I needed the same thing and I just found out how it works.

My setup is just like yours that I also have a custom button in the editor and as soon as I press it I do the following in the clicked method:

UWorld* const World = GEditor->GetEditorWorldContext().World(); // Get a world reference
AWorldSettings* worldSettings = World->GetWorldSettings();   // Get the world's settings
worldSettings->DefaultGameMode = PluginManager::getInstance()->getGameModeClass(); 

The PluginManager here is a Singleton class that stores all the important information about my plugin that I need all the time - e.g. my gamemodeclass…

The getInstance() looks like this:

static PluginManager* getInstance()
{
	return (!instance) ? instance = new PluginManager() : instance;
}

The getGameModeClass() looks like this:

TSubclassOf<class AMyGameMode> getGameModeClass() const
{
	return this->gameModeClass;
}

The setter looks like this:

void setGameModeClass(TSubclassOf<AMyGameMode> gameModeClass)
{
	this->gameModeClass = gameModeClass;
}

The member itself looks like this:

UPROPERTY(EditDefaultsOnly, BlueprintReadWrite) TSubclassOf<class AMyGameMode> gameModeClass = nullptr;

I did the assignment inside the constructor of my gamemode, because it is executed when starting the editor and I then already have a reference to the class without having to find it with the ClassFinder:
PluginManager::getInstance()->setGameModeClass(this->StaticClass());

This works if your GameMode is a C++ class. I guess if your GameMode is a BluePrint, you would have to get the Class like this:

static ConstructorHelpers::FClassFinder<AGameMode> GameModeBPClassFinder(TEXT("/Plugin/GameModeBP"));
if (GameModeBPClassFinder.Class != nullptr)
{
	PluginManager::getInstance()->setGameModeClass(GameModeBPClassFinder.Class);
}

If you still have questions feel free to ask.

Joey