• Print

Author Topic: Will player variables stick on map change/server shutdown/restart?  (Read 5532 times)

0 Members and 1 Guest are viewing this topic.

Offline iViscosity

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 803
  • Karma: 58
For example, say I do something like this:


Code: Lua
  1. function Setdata( ply )
  2.     ply.health = ply:Health()
  3. end
  4.  
  5.  
  6. --<player disconnects/reconnects>
  7.  
  8.  
  9. function PlayerInitialSpawn( ply )
  10.     ply:SetHealth( ply.health )
  11. end
  12. hook.Add( "PlayerInitialSpawn", "Set health to last known value", PlayerInitialSpawn )
  13.  


Will that ply.health stay even after they leave?
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: Will player variables stick on map change/server shutdown/restart?
« Reply #1 on: May 01, 2017, 12:18:18 pm »
No. Variables you add to objects (ie the Player object) remain only for the lifetime of that object. When a player disconnects, that object is destroyed, along with all associated fields.

To set persistent data, look at SetPData and GetPData, which store data in the server database. Do note, however, that GetPData returns strings, so you'll want to use tonumber to convert data back to numbers. Also, you should probably set these values on the server so players can't tamper with them (not that you need to worry about that).
« Last Edit: May 01, 2017, 12:20:13 pm by Bytewave »
bw81@ulysses-forums ~ % whoami
Homepage

Offline iViscosity

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 803
  • Karma: 58
Re: Will player variables stick on map change/server shutdown/restart?
« Reply #2 on: May 01, 2017, 12:19:49 pm »
Right, I wanted to see if there was a different way. I guess I can always get that on connect then set a variable to it. I've used PData before, but using it too much can cause perf issues.. Wasn't sure but thanks anyway
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: Will player variables stick on map change/server shutdown/restart?
« Reply #3 on: May 01, 2017, 12:23:22 pm »
PData shouldn't be that non-performant. Yes, it relies on the server's SQLite DB, but I don't see why it would cause performance issues unless you're updating it super often.
bw81@ulysses-forums ~ % whoami
Homepage

Offline iViscosity

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 803
  • Karma: 58
Re: Will player variables stick on map change/server shutdown/restart?
« Reply #4 on: May 01, 2017, 12:35:13 pm »
Right, using it too much. In my case, though, now that I think about it, it will only be used on player connect/disconnect which isn't that often on my server so it should be fine.
I'm iViscosity. I like gaming and programming. Need some help? Shoot me PM.

Offline Megiddo

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 6214
  • Karma: 394
  • Project Lead
Re: Will player variables stick on map change/server shutdown/restart?
« Reply #5 on: May 01, 2017, 01:31:30 pm »
The PData implementation is very light -- https://github.com/garrynewman/garrysmod/blob/master/garrysmod/lua/includes/extensions/util.lua#L333 . You should only have issues if you were calling it multiple times per second.

I do have to say that I've seen odd behavior from player tables in the past, and try to avoid them now. Not sure if it's other addons mucking the tables up or what, but I've seen data randomly disappear from the player table.
Experiencing God's grace one day at a time.

Offline Bytewave

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 718
  • Karma: 116
  • :)
    • My Homepage
Re: Will player variables stick on map change/server shutdown/restart?
« Reply #6 on: May 01, 2017, 01:36:49 pm »
I do have to say that I've seen odd behavior from player tables in the past, and try to avoid them now. Not sure if it's other addons mucking the tables up or what, but I've seen data randomly disappear from the player table.
To add on to the odd behavior, there is one annoying quirk of PData: it doesn't store by SteamID64, it stores by UniqueID, which is just a CRC32 hash of gm_STEAMID_gm. Sure, that works, but there's bound to be collisions in there somewhere.
« Last Edit: May 01, 2017, 01:41:21 pm by Bytewave »
bw81@ulysses-forums ~ % whoami
Homepage

Offline iViscosity

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 803
  • Karma: 58
Re: Will player variables stick on map change/server shutdown/restart?
« Reply #7 on: May 02, 2017, 07:09:11 am »
The addon I'm creating also uses MySQL which I have setup to store SteamID64s, but in-case whoever is using the addon doesn't have a database to store the information, I'm using PData.
I'm iViscosity. I like gaming and programming. Need some help? Shoot me PM.

  • Print