Skip to content

Instantly share code, notes, and snippets.

@JonahGrimm
Created October 22, 2023 03:18
Show Gist options
  • Save JonahGrimm/31c80ccddbda0170727044f3c56a8932 to your computer and use it in GitHub Desktop.
Save JonahGrimm/31c80ccddbda0170727044f3c56a8932 to your computer and use it in GitHub Desktop.
Creates a console command that logs the UnrealEngine net mode for the given game instance
#include "GameInstanceCommands.h"
void UGameInstanceCommands::Initialize(FSubsystemCollectionBase& Collection)
{
Super::Initialize(Collection);
TestCmd = IConsoleManager::Get().RegisterConsoleCommand( TEXT("LogNetMode"),
TEXT("Outputs the net mode to the console."),
FConsoleCommandDelegate::CreateUObject( this, &UGameInstanceCommands::LogNetMode ),
ECVF_Default );
}
void UGameInstanceCommands::Deinitialize()
{
Super::Deinitialize();
IConsoleManager::Get().UnregisterConsoleObject( TestCmd );
}
void UGameInstanceCommands::LogNetMode()
{
#if !UE_BUILD_SHIPPING
UWorld* world = GetGameInstance()->GetWorldContext()->World();
ENetMode mode = world->GetNetMode();
UE_LOG(LogTemp, Warning, TEXT("NetMode %s"), *GetNetName(mode));
#endif
}
FString UGameInstanceCommands::GetNetName(ENetMode Mode)
{
switch (Mode)
{
/** Standalone: a game without networking, with one or more local players. Still considered a server because it has all server functionality. */
case NM_Standalone: return "Standalone";
/** Dedicated server: server with no local players. */
case NM_DedicatedServer: return "DedicatedServer";
/** Listen server: a server that also has a local player who is hosting the game, available to other players on the network. */
case NM_ListenServer: return "ListenServer";
/**
* Network client: client connected to a remote server.
* Note that every mode less than this value is a kind of server, so checking NetMode < NM_Client is always some variety of server.
*/
case NM_Client: return "Client";
case NM_MAX: return "MAX";
}
return "Unknown";
}
#pragma once
#include "CoreMinimal.h"
#include "Engine/GameInstance.h"
#include "GameInstanceCommands.generated.h"
/**
*/
UCLASS()
class LEARNINGUNREAL_API UGameInstanceCommands : public UGameInstanceSubsystem
{
GENERATED_BODY()
public:
virtual void Initialize(FSubsystemCollectionBase& Collection) override;
virtual void Deinitialize() override;
private:
IConsoleCommand* TestCmd;
void LogNetMode();
FString GetNetName(ENetMode Mode);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment