Author Topic: ConVars and Pdata  (Read 2240 times)

0 Members and 1 Guest are viewing this topic.

Offline Bite That Apple

  • Hero Member
  • *****
  • Posts: 858
  • Karma: 416
  • Apple Innovations 2010®
    • Fun 4 Everyone Gaming
ConVars and Pdata
« on: May 13, 2014, 04:06:14 PM »
Alright, so as most of you know, I'm not good with client side 'stuff'. So I'm going to get to the point now.
So. I did thing 'funky', and I'm pretty sure that it's not the proper way to do it at all.

So, basically, on my shared side, I have a timer every second, updating this number, and sending the data through usermessages.
Shared File (Basically all server side)
Code: [Select]
function CheckPlayerPointsXP()
timer.Create( "CheckXPOnPlayers", 1, 0, function()
local GetPlayers = player.GetAll()
for k,v in pairs(GetPlayers) do
if v:IsValid() then
local GetPoints = sql.QueryValue( "SELECT Points FROM TDM_XP_SYSTEM WHERE UniqueID = '"..v:UniqueID().."'" )
umsg.Start("TDMGETPOINTS2")
umsg.String(GetPoints)
umsg.String(k)
umsg.End()
end
end
end)
end

Then on my client side, I have it getting the usermessages, and setting PData by player.id. I just wanted ot know if there was a better way of doing it, so it uses less resources..
Client Side
Code: [Select]
function TDMGETPOINTS2(data)
local PlayerPointsByXP = data:ReadString()
local PlayerPointsByXPID = data:ReadString()
if player.GetByID(PlayerPointsByXPID):IsValid() then
player.GetByID(PlayerPointsByXPID):SetPData("MyXPPoints", PlayerPointsByXP)
end
end
usermessage.Hook("TDMGETPOINTS2", TDMGETPOINTS2)
Quote from: John F. Kennedy 1963
A man may die, nations may rise and fall, but an idea lives on.

Offline MrPresident

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 2728
  • Karma: 430
    • |G4P| Gman4President
Re: ConVars and Pdata
« Reply #1 on: May 13, 2014, 04:14:16 PM »
First thing: Use net messages not user messages. They're very similar and easy to use once you get the hang of them. http://wiki.garrysmod.com/page/Category:net

Instead of doing a timer that just queries and refreshes the data every one second, set it up as a callback.

For example: When a player gains experience, have it send a net message to connected players with that player's new exp value.
This way, the values are always accurate and you're not sending tons of potentially unneeded messages every second.



As for newly connected players, just set up a function to send the exp to that one player when they join. Kind of how you are doing your timer now.



edit: Also, with net messages, you can send entities so you can just just send the player's entity instead of converting it to an id and looking it up on the client side.

Offline Bite That Apple

  • Hero Member
  • *****
  • Posts: 858
  • Karma: 416
  • Apple Innovations 2010®
    • Fun 4 Everyone Gaming
Re: ConVars and Pdata
« Reply #2 on: May 13, 2014, 04:22:04 PM »
So wait, I see that it says, "The major advantages of the net library are the large size limit (64kb/message) and the ability to send data backwards - from the client to the server."

Does this mean that it only goes from client to server, or is that just another advantage?
Quote from: John F. Kennedy 1963
A man may die, nations may rise and fall, but an idea lives on.

Offline MrPresident

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 2728
  • Karma: 430
    • |G4P| Gman4President
Re: ConVars and Pdata
« Reply #3 on: May 13, 2014, 05:15:20 PM »
It can go either way.

Client to Server is just another advantage.

Offline Bite That Apple

  • Hero Member
  • *****
  • Posts: 858
  • Karma: 416
  • Apple Innovations 2010®
    • Fun 4 Everyone Gaming
Re: ConVars and Pdata
« Reply #4 on: May 13, 2014, 05:37:58 PM »
Alright, I got most of it done. Though, I'm pretty sure I should still use Pdata for one thing, because when the menu opens up, there is a DListView, and it lists:

Code: [Select]
for k,v in pairs(player.GetAll()) do
    GetGiveMoneyPlayers:AddLine(v:Nick(),v:GetPData('MyXPPoints'))
end


Should I still use Pdata for this, or is there another way?
Quote from: John F. Kennedy 1963
A man may die, nations may rise and fall, but an idea lives on.

Offline Neku

  • Hero Member
  • *****
  • Posts: 549
  • Karma: 27
Re: ConVars and Pdata
« Reply #5 on: May 13, 2014, 11:46:19 PM »
Yeah, go ahead and store whatever amount of XP they have in PData.
Unless you want to have a separate data file with each individual player's XP, then stick with PData.
Out of the Garry's Mod business.

Offline Cobalt

  • Full Member
  • ***
  • Posts: 216
  • Karma: 44
  • http://steamcommunity.com/id/__yvl/
Re: ConVars and Pdata
« Reply #6 on: May 14, 2014, 08:29:29 AM »
Do the pdata functions even work clientside?

Offline Neku

  • Hero Member
  • *****
  • Posts: 549
  • Karma: 27
Re: ConVars and Pdata
« Reply #7 on: May 14, 2014, 09:15:44 AM »
I haven't tried, but I would assume no.
In this case, it is a shared function, so it would probably work.
« Last Edit: May 14, 2014, 09:17:29 AM by Neku »
Out of the Garry's Mod business.

Offline MrPresident

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 2728
  • Karma: 430
    • |G4P| Gman4President
Re: ConVars and Pdata
« Reply #8 on: May 14, 2014, 09:18:52 AM »
PData works clientside, but it utilizes the client databse.

Anything you do with PData on the clientside is not networked with the server or other clients.