Ulysses

General => Developers Corner => Topic started by: LuaTenshi on December 25, 2012, 05:04:25 PM

Title: Storing data like score amounts and or money, for each player.
Post by: LuaTenshi on December 25, 2012, 05:04:25 PM
How would I store data on players such as Score amounts (I need to have them store in a database so people don't lose their score when they leave.) and stuff like that?
I was thinking of using Networked-Vars but after Megiddo said they where broken I am not so sure about using them, so any ideas?
Title: Re: Storing data like score amounts and or money, for each player.
Post by: Megiddo on December 25, 2012, 11:07:55 PM
I never said they were broken, I said that they have historically been terrible. As in, slow to update, sometimes not synching properly.
Title: Re: Storing data like score amounts and or money, for each player.
Post by: LuaTenshi on December 26, 2012, 12:38:43 AM
I never said they were broken, I said that they have historically been terrible. As in, slow to update, sometimes not synching properly.

For me that translates into broken. lol.
Title: Re: Storing data like score amounts and or money, for each player.
Post by: RynO-SauruS on December 26, 2012, 01:18:21 AM
You could just make a hook for when they leave (I believe it's best to use "OnEntityRemoved" and check if it's a player), get their score (GetFrags()?) and save it.
Title: Re: Storing data like score amounts and or money, for each player.
Post by: LuaTenshi on December 26, 2012, 10:21:36 AM
You could just make a hook for when they leave (I believe it's best to use "OnEntityRemoved" and check if it's a player), get their score (GetFrags()?) and save it.

Yes but I would like to score to update live so where would I hold it then?
Title: Re: Storing data like score amounts and or money, for each player.
Post by: RynO-SauruS on December 26, 2012, 02:58:25 PM
Yes but I would like to score to update live so where would I hold it then?

I'm confused. If you need to be able to get any player's score (frags/kills) at any time, just use ply:GetFrags().
Title: Re: Storing data like score amounts and or money, for each player.
Post by: JamminR on December 26, 2012, 03:06:13 PM
Ryno (glad to see you back btw), I think he's trying to figure out best way/place to store them non-live..that is, through SQLite, SQL, or flat text table.
Though the GetFrags and other functions could be used live...I think he wants to have the score/frags tables get stored somewhere offline whenever they get updated.
Then when a player leaves/rejoins...stuff is still there and loads into live data.
(I could be wrong)

Title: Re: Storing data like score amounts and or money, for each player.
Post by: emilhem on December 29, 2012, 01:10:49 PM
You could use a hook for "PlayerInitialSpawn" and create a timer for that player that updates every second.

Code: [Select]
function updatePlayer( ply )
// Your code to save the frags/data/etc.
end

function timerStart( ply )
timer.Create( "save_stats_"..tostring(ply:SteamID()), 1, 0, function() if not (ply and IsValid(ply)) then return end updatePlayer( ply ) end ) // Runs every second forever.
end
hook.Add( "PlayerInitialSpawn", "timerstarting", timerStart )

function destroyTimers(ply) // Destroy the timers when they disconnect and save their stats one more time.
if (ply and IsValid(ply)) then updatePlayer( ply ) end
if timer.Exists("save_stats_" ..tostring(ply:SteamID())) then
timer.Stop("save_stats_" ..tostring(ply:SteamID()))
timer.Destroy("save_stats_" ..tostring(ply:SteamID()))
end
end
hook.Add( "PlayerDisconnected", "PromotionCleanUP", destroyTimers )