• Print

Author Topic: PlayerDisconnected hook, when is it called?  (Read 8025 times)

0 Members and 1 Guest are viewing this topic.

Offline iViscosity

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 803
  • Karma: 58
PlayerDisconnected hook, when is it called?
« on: May 08, 2017, 06:53:09 am »
So clearly the PlayerDisconnected hook is called when someone disconnects from the server, but I was wondering if it's only called when they manually leave. In the recent release I made it's function relies on the PlayerDisconnected hook, but I don't want it to cause issues when people leave because they were karma kicked or kicked by an admin for some other reason. Is there a way to make it so that it is only caused if someone manually leaves the server and not if they are kicked by either an admin or karma?
I'm iViscosity. I like gaming and programming. Need some help? Shoot me PM.

Offline MrPresident

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 2727
  • Karma: 430
    • |G4P| Gman4President
Re: PlayerDisconnected hook, when is it called?
« Reply #1 on: May 08, 2017, 07:42:20 am »
It gets called when a player disconnects, for any reason.

kicked, banned, timed out, quits of their own accord. Any reason.

Essentially, as their player object is about to be deleted, it's called.

Offline iViscosity

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 803
  • Karma: 58
Re: PlayerDisconnected hook, when is it called?
« Reply #2 on: May 08, 2017, 07:43:24 am »
That is what I thought, thanks. Is there a hook or something that calls only when a player manually disconnects?
I'm iViscosity. I like gaming and programming. Need some help? Shoot me PM.

Offline iViscosity

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 803
  • Karma: 58
Re: PlayerDisconnected hook, when is it called?
« Reply #3 on: May 08, 2017, 09:39:47 am »
I have a related question, is there a way to capture when a person is about to leave, before they actually do? Like when they actually hit disconnect (or disconnect in the console), before they actually leave.
I'm iViscosity. I like gaming and programming. Need some help? Shoot me PM.

Offline MrPresident

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 2727
  • Karma: 430
    • |G4P| Gman4President
Re: PlayerDisconnected hook, when is it called?
« Reply #4 on: May 08, 2017, 12:44:48 pm »
The PlayerDisconnect hook is called right before they actually disconnect I believe. Assuming they are not timing out that is.

It is called while the player object is still valid.

If you hook directly into the game event for disconnect, you might be able to tell the difference in the types of disconnects.

Offline MrPresident

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 2727
  • Karma: 430
    • |G4P| Gman4President

Offline iViscosity

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 803
  • Karma: 58
Re: PlayerDisconnected hook, when is it called?
« Reply #6 on: May 09, 2017, 12:55:33 pm »
Question about that since I've only used this once and had no need to get specifics from it, "string reason - Reason for disconnecting", how is this stored? Can I just access it like
Code: Lua
  1.  
  2. gameevent.Listen( "player_disconnect" )
  3. hook.Add( "player_disconnect", "player_disconnect_example", function( data )
  4.         local reason = data.reason
  5.  
  6.  
  7.         if reason == "Disconnect by user." then
  8.             -- The things I want it to do
  9.         end
  10. end )
or is there some other way to do it, or do you not know?
« Last Edit: May 09, 2017, 12:57:43 pm by iViscosity »
I'm iViscosity. I like gaming and programming. Need some help? Shoot me PM.

Offline Bytewave

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 718
  • Karma: 116
  • :)
    • My Homepage
Re: PlayerDisconnected hook, when is it called?
« Reply #7 on: May 09, 2017, 01:37:06 pm »
Question about that since I've only used this once and had no need to get specifics from it, "string reason - Reason for disconnecting", how is this stored? Can I just access it like
Code: Lua
  1.  
  2. gameevent.Listen( "player_disconnect" )
  3. hook.Add( "player_disconnect", "player_disconnect_example", function( data )
  4.         local reason = data.reason
  5.  
  6.  
  7.         if reason == "Disconnect by user." then
  8.             -- The things I want it to do
  9.         end
  10. end )
or is there some other way to do it, or do you not know?
According to this, the possible values are:
Quote
   "self", "kick", "ban", "cheat", "error"

That should be engine-wide. Don't know how it applies to GMod specifically, but I guess it's a good start.

And, yes, it should be just another key in the data table returned by the engine.
« Last Edit: May 09, 2017, 01:44:33 pm by Bytewave »
bw81@ulysses-forums ~ % whoami
Homepage

Offline MrPresident

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 2727
  • Karma: 430
    • |G4P| Gman4President
Re: PlayerDisconnected hook, when is it called?
« Reply #8 on: May 09, 2017, 02:18:29 pm »
Just do some debug printing.

Print data.reason and have a player leave in the way you're looking for and you'll see exactly what it returns and what you should check for.

Offline iViscosity

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 803
  • Karma: 58
Re: PlayerDisconnected hook, when is it called?
« Reply #9 on: May 09, 2017, 02:25:44 pm »
Just do some debug printing.

Print data.reason and have a player leave in the way you're looking for and you'll see exactly what it returns and what you should check for.

Oh yeah, I forgot you can find things by just testing it yourself.
I'm iViscosity. I like gaming and programming. Need some help? Shoot me PM.

Offline MrPresident

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 2727
  • Karma: 430
    • |G4P| Gman4President
Re: PlayerDisconnected hook, when is it called?
« Reply #10 on: May 09, 2017, 02:34:48 pm »
Don't take this the wrong way.. but learning to properly debug code is one of the most important things you can do as a coder/programmer.

Once you figure out the little tricks of the trade, so to speak, it'll really set you above others.

You can figure out what is going on in code almost without fail all by yourself by just using debug printing break points. You can print various variables along the execution order to find all kinds of things.

Offline iViscosity

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 803
  • Karma: 58
Re: PlayerDisconnected hook, when is it called?
« Reply #11 on: May 09, 2017, 03:23:20 pm »
Oh no I'm not, I just get lazy and forget I can try to find my own solutions :P
I'm iViscosity. I like gaming and programming. Need some help? Shoot me PM.

  • Print