Standalone Game Crashing on LoadMenu() method Call


Standalone Game Crashing on LoadMenu() method Call

The error message does show the code causing the issue. It’s around line 127 of the GameInstance, could be a line or two either side.


void UPzzzleGameInstance::LoadMenu()
{
	if (this->Menu == nullptr)
	{
		UE_LOG(LogTemp, Error, TEXT("No Widget Class  /Game/UI/MenuSystem/"));
		return;
	};
	
	APlayerController* PlayerController = GetFirstLocalPlayerController();
	if (PlayerController)
		PlayerController->bShowMouseCursor = true;

	if (Widget!=nullptr)
	{
		FInputModeUIOnly Mode;
		/*	Mode.SetWidgetToFocus(this->Menu.Get());*/

		Mode.SetLockMouseToViewportBehavior(EMouseLockMode::DoNotLock);
		PlayerController->SetInputMode(Mode);
		if (Widget!=nullptr)
		{
			Widget->AddToViewport();
			Widget->bIsFocusable = true;
			Widget->SetMenuInterface(this);
		}

	}
	else
	{
		UE_LOG(LogTemp,Error,TEXT("ERROR in LoadMenu() function failed to load Widget"))
	}

};

LoadMenu Function

It often helps to compare your code to the instructor’s version (as linked in the resources). You could even temporarily overwrite your C++ code with the reference version to see if that helps, provided that names in Blueprints are compatible, and work your way back from there in small steps. (Backup first/use git if you value your own version…)

Your method does a few things that Sam moved to the Setup method of the MenuWidget base class at some point.

One significant issue is that you are working with a Widget field (obviously not a variable) that is not assigned here. I suppose you wanted to instantiate it from the Menu class before working with it, as suggested by the method name. Maybe the field has a bad value originating from outside this method.

As a minor thing, you are calling method SetInputMode of PlayerController, for which you consider the possibility that it may be nullptr a few lines above - if that was the case, you get a problem here.

Init()

	Widget = CreateWidget<UMainMenu>(GetWorld(), this->Menu);

header file

private:
	class UMainMenu* Widget;
	TSubclassOf<class UUserWidget>Menu;

I have done Debugging
here you can see widget pointer is not null and playercontroller is also not null When I play in Edittor its working fine but when i do in standalone or packge the build its crashing where i put breakpoint
whats wrong with it??

Have you compared against end of lecture code? Also, just to be sure, delete the binaries and intermediate folders to ensure it is an updated build.

Give that a try. Checking against lecture source will probably show the issue.

yeah I did but i’m still getting this crash `
Unhandled Exception: EXCEPTION_ACCESS_VIOLATION 0x00000000

UE4Editor_Pzzzle!UPzzzleGameInstance::LoadMenu() [V:\UE4 Projects\Pzzzle 4.25\Source\Pzzzle\PzzzleGameInstance.cpp:145]
`

Can you share the whole file. There’s a lot of excessive checks on the Widget variable from what I see in the screenshot. It seems that adding to the viewport is causing the issue. Therefore the issue may actually be when the widget initialises.

// Copyright(c)2022 Vishal Ahirwar. All rights reserved.

#include "PzzzleGameInstance.h"
#include"Engine/Engine.h"
#include"UObject/ConstructorHelpers.h"
#include"Actor/PlatformTrigger.h"																					
#include"Blueprint/UserWidget.h"
#include"Widget/MainMenu.h"
#include"OnlineSubsystem.h"
#include"OnlineSessionSettings.h"
#include"Interfaces/OnlineSessionInterface.h"

//#include"Blueprint/TextBlock.h"

const static FName SESSION_NAME = TEXT("Session Main");

UPzzzleGameInstance::UPzzzleGameInstance(const FObjectInitializer& ObjectInitializer):Super(ObjectInitializer)
{
	ConstructorHelpers::FClassFinder<UUserWidget> MenuWidgetClass(TEXT("'/Game/UI/MenuSystem/WP_menu'"));

	if (MenuWidgetClass.Class == nullptr)return;
	this->Menu = MenuWidgetClass.Class;


	

	//	UE_LOG(LogTemp,Warning,TEXT("Found Class %s"),*Menu->GetName())
};

void  UPzzzleGameInstance::Init()
{
	Super::Init();
	IOnlineSubsystem* System = IOnlineSubsystem::Get();

	UE_LOG(LogTemp,Error,TEXT("Init Called!"))

	Widget = CreateWidget<UMainMenu>(GetWorld(), this->Menu);
	//if (Widget)
	//{
	//	Widget->AddToViewport();
	//	Widget->SetMenuInterface(this);
	//};
	if (!Widget)
	{
		UE_LOG(LogTemp,Warning,TEXT("Widget : MainMenu is not CreatedYey"))
	}
	if (System == nullptr)
	{
		UE_LOG(LogTemp, Error, TEXT("No Subsystem Found!"))

	}
	else
	{
		UE_LOG(LogTemp, Warning, TEXT("[Init] Subsystem  %s"), *System->GetSubsystemName().ToString())
			SessionInterface = System->GetSessionInterface();
		if (SessionInterface)
		{
			UE_LOG(LogTemp, Error, TEXT("Session Interface Found!"))
				SessionInterface->OnCreateSessionCompleteDelegates.AddUObject(this, &UPzzzleGameInstance::OnCreateSessionComplete);
			SessionInterface->OnDestroySessionCompleteDelegates.AddUObject(this, &UPzzzleGameInstance::OnDestroySessionComplete);
			SessionInterface->OnFindSessionsCompleteDelegates.AddUObject(this, &UPzzzleGameInstance::OnSessionSearchComplete);
		
		}
	}
}

void UPzzzleGameInstance::Host()
{

	if (this->SessionInterface.IsValid())
	{

		auto ExistingSession = SessionInterface->GetNamedSession(SESSION_NAME);
		this->GetFirstLocalPlayerController()->ClientTravel("/Game/Loading", ETravelType::TRAVEL_Relative);
		if (ExistingSession != nullptr)
		{
			SessionInterface->DestroySession(SESSION_NAME);
		}
		else
		{
			this->CreateSession();
		}

	}

};
void UPzzzleGameInstance::RefreshServerList()
{
	this->SessionSearch = MakeShareable(new FOnlineSessionSearch);
	if (SessionSearch.IsValid())
	{
		SessionSearch->bIsLanQuery = true;

		UE_LOG(LogTemp, Error, TEXT("Session Searching Started..."))
			SessionInterface->FindSessions(0, SessionSearch.ToSharedRef());

	}
};

void UPzzzleGameInstance::Join(const FString& Address)
{
	//if (this->Widget != nullptr)
	//{
	//	this->Widget->SetServerList({"Server 1", "Server 2","Server 3","Server 4"
	//});
	//}
	//UE_LOG(LogTemp,Error,TEXT("Joining aka addign new child widget to scroll box"))
	UEngine* Engine = GetEngine();
		if (Engine == nullptr)return;
	APlayerController* PlayerController = GetFirstLocalPlayerController();
	if (PlayerController == nullptr)return;
	FInputModeGameOnly Mode;
	Mode.SetConsumeCaptureMouseDown(true);
	PlayerController->SetInputMode(Mode);
	PlayerController->ClientTravel(Address, ETravelType::TRAVEL_Absolute);
	if (Engine)
		Engine->AddOnScreenDebugMessage(0, 2.5f, FColor::Green, (Address));
};

void UPzzzleGameInstance::LoadMenu()
{
	if (this->Menu == nullptr)
	{
		UE_LOG(LogTemp, Error, TEXT("No Widget Class  /Game/UI/MenuSystem/"));
		return;
	};
	
	APlayerController* PlayerController = GetFirstLocalPlayerController();
	if (PlayerController)
		PlayerController->bShowMouseCursor = true;

	if (Widget!=nullptr)
	{
		FInputModeUIOnly Mode;
		/*	Mode.SetWidgetToFocus(this->Menu.Get());*/

		Mode.SetLockMouseToViewportBehavior(EMouseLockMode::DoNotLock);
		if(PlayerController)
		PlayerController->SetInputMode(Mode);
		if (Widget)
		{
			if(Widget)
			Widget->bIsFocusable = true;
			if(Widget)
			Widget->AddToViewport();
			if(Widget)
			Widget->SetMenuInterface(this);
		}

	}
	else
	{
		UE_LOG(LogTemp,Error,TEXT("ERROR in LoadMenu() function failed to load Widget"))
	}

};

void UPzzzleGameInstance::OnCreateSessionComplete(FName SessionName, bool Success)
{
	if (!Success)
	{
		UE_LOG(LogTemp, Error, TEXT("Failed to Create a new Session!"));
		return;
	};
	UEngine* Engine = GetEngine();
	if (Engine == nullptr)return;
	Engine->AddOnScreenDebugMessage(0, 2.5f, FColor::Black, FString("Hosting Game..."));
	UWorld* World = GetWorld();
	FInputModeGameOnly Mode;
	Mode.SetConsumeCaptureMouseDown(true);
	GetFirstLocalPlayerController()->SetInputMode(Mode);
	World->ServerTravel("/Game/ThirdPersonCPP/Maps/ThirdPersonExampleMap?listen");

};

void UPzzzleGameInstance::CreateSession()
{
	if (this->SessionInterface.IsValid())
	{

		FOnlineSessionSettings SessionSettings;
		SessionSettings.bIsLANMatch = true;
		SessionSettings.NumPublicConnections = 2;
		SessionSettings.bShouldAdvertise = true;
		SessionInterface->CreateSession(0, SESSION_NAME, SessionSettings);
	}
};

void UPzzzleGameInstance::OnDestroySessionComplete(FName SessionName, bool Success)
{
	if (Success)
	{
		this->CreateSession();
	}
	else {
		UE_LOG(LogTemp, Error, TEXT("Failed to Destryo Session "))
	};

};

void UPzzzleGameInstance::OnSessionSearchComplete(bool Success)
{
	if (Success && this->SessionSearch.IsValid()&&this->Widget!=nullptr)
	{
		UE_LOG(LogTemp, Error, TEXT("Session Searching Done!"))
			TArray<FOnlineSessionSearchResult>Results = this->SessionSearch->SearchResults;
		TArray<FString>ServerNames;
		for (const auto& Result : Results)
		{

			UE_LOG(LogTemp, Error, TEXT("Session Found : %s"), *Result.GetSessionIdStr())
				ServerNames.Add(Result.GetSessionIdStr());

		};

		this->Widget->SetServerList(ServerNames);
	}

};


1 Like

Sorry for the delay in replying. I’ve been going through the code according to the lecture that you’re on/posted against and the code is very different. At this point, the load menu code should be refactored and a MenuInterface implemented. Also, the menu should be created in LoadMenu and not in Init. I suggest you check against the course code again, specifically this:

2_Menu_System/PuzzlePlatformsGameInstance.cpp at 37c58fbdf5d4a4e6c0d19c2383fb128b6a502661 · UnrealMultiplayer/2_Menu_System · GitHub

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms