How do I prevent ragequitting?

Im curious how do I make it as difficult as possible to quit a multiplayer game prematurely on a desktop?

Don’t get me wrong I want the players to quit whenever they want, but not without seeing the warning message that they’ll suffer penalties for quitting early.

Is there a way to consume the Alt-F4 keys and have a “Are you sure?” message appear instead?

Although I think there’s a way to handle this already somewhere, I can’t seem to find it right now… but one way would be to intercept the WM_QUIT and WM_CLOSE window messages using this How can i add window message? - #12 by gmpreussner_1

Hmm would be great if there already was a solution for this

This is currently not possible using only Blueprints as far as I know. However you can do this using a small bit of C++ and Blueprints like so:

  1. Create a GameInstance C++ class.
  2. Declare these two functions in your GameInstance class in the .h file :
UCLASS()
class MYGAME_API UMyGameInstance : public UGameInstance
{
	GENERATED_BODY()

public:

	//AltF4 Catch
	UFUNCTION(BlueprintCallable)
	void BindWindowCloseRequestAction();

	UFUNCTION(BlueprintCallable, BlueprintImplementableEvent)
	bool OnWindowCloseRequestAction();
}
  1. Add the following code to the .cpp file :
#include "Kismet/GameplayStatics.h"

void UMyGameInstance::BindWindowCloseRequestAction()
{
	if (UGameViewportClient* ViewportClient = GetGameViewportClient())
    {
		ViewportClient->OnWindowCloseRequested().BindUFunction(this, "OnWindowCloseRequestAction");
    }
}
  1. In Unreal create a new Blueprint class with your GameInstance class as the parent.

  2. using the override button in the functions panel, override the “Init” function and the “OnWindowCloseRequestAction” function.

  3. From the Init event add a small delay like 0.01 and then call the BindWindowCloseRequestAction function.


    The delay is to ensure that the ViewportClient is created and is valid before we call the Bind function.

  4. You can now add any functionality that should happen when the player presses Alt+F4 inside the OnWindowCloseRequestAction function.


    Setting the Return Value to true will close the game, false will not.

  5. And finally set the GameInstance Blueprint you created in the editor as the default “Game Instance Class” in Project Settings → Maps & Modes → Game Instance.

2 Likes

Oh wow excellent! Thanks! However I already have a custom game instance, would it be possible to add this piece of code to it instead of making a completely new one? Sorry, just starting out with c++ :smiley:

Yes, it should be.

ahhh ViewportClient! That’s where I needed to look and couldn’t remember.

I know it’s not what you want, but I think that even if you show a popup to a person which is in “rage” that doesn’t mean that person will read it.

I think the solution to this is simpler than that: show the warning to all players before they are in that state ? at the start of the game maybe?

Cheers