Detect game quitting in Blueprints

if the player kills the game then nothing is going to get executed, it is a simple as that

I need to run a piece of code when a player exits the game through means other than the exit button, specifically I need to run a php code that updates the online status of a player which is very important but how exactly do i know when a player exits the game?
For instance when the player kills the game through task manager or simply shuts down his pc or any other way of losing connection really.
I found this post https://answers.unrealengine.com/questions/127897/event-on-close.html but it doesn’t help much with blueprints since there isn’t a game engine class…help?

Then how can I know if a player loses connection? I’ve seen some ping-pong type solutions that go back and forth from client to server every second but I’m not really sure how to create it using blueprints.

https://forums.unrealengine.com/development-discussion/blueprint-visual-scripting/40768-detect-connect-disconnect-attempts-on-network-in-blueprints

maybe this helps

If you run unreal server then oyu can detect disconnection in game mode events. If you try via HTTP the you sadly limited the same as websites, as thats nature of HTTP. \You need to use heart beating (Sending “im here” massages) and turn use offline when it does not responce over specific heartbeat time. You might consider using diffrent protocols too.

There possibiloity to detect closure in C++, but even so what about crashes and power downs? You should not relay on user exit messages, insted on connection status as you figured that yourself

What you could do is use the client side to check the game quit event and see what closed it. If it was the in game menus or a force quit. Then you could send a final package to the server detailing the quit status.

if the player kills the game then nothing is going to get executed, it is a simple as that

True! So true. In real life you usually check from the server if the client is “live” periodically to close the connection of “killed” clients. This should be already implemented in UE4 networking system.

Hey, can you please explain how to do this? It would be much appreciated

There’s a virtual method in the AGameMode class called Logout, which is called every time an AController leaves the game. You can override it in your own GameMode classes, check if the AController is an APlayerController (Logout is also called when AIControllers are destroyed) and then have your UE4 server make the HTTP call to your PHP server.

If you’re using Blueprints, the event you want is called OnLogout and is only called for human players.

1 Like

Hey, thanks for replying. I’ve been messing with this for some time and apparently, it doesn’t work as intended when played in editor since no php requests have been sent post logout. I’ll be trying this once i compile my server and see how it works in that environment and letting everyone here know if it works.

Also while we’re at it, since I’ve been looking at alternatives, do you know if there is a way to use the ping-pong type communication between server and client every so often as a check if the player is online in blueprints? I’ve come across a solution in C++ that uses sockets but haven’t had much luck in blueprints. Do you perhaps know a way to do this?

I would do it using heartbeat messages like suggested. It’s the only foolproof way. You can get a faster response by adding logout event handling to it, but the only way to really detect for example, that the user experienced a sudden power outage or their toddler poured soda on the computer, is to notice that say, 3 heartbeat messages in a row did not come in when expected. once you get another one you flip it back to “online” status.