Ulysses
General => Developers Corner => Topic started by: Bite That Apple 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)
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
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)
-
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 (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.
-
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?
-
It can go either way.
Client to Server is just another advantage.
-
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:
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?
-
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.
-
Do the pdata functions even work clientside?
-
I haven't tried, but I would assume no.
In this case, it is a shared function, so it would probably work.
-
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.