• best way og sending data to hud?
    11 replies, posted
Anyone who know a good way, or the best way to send data from a mysql database to The hud?
net library? Get data from DB on server > send it from server to client using net library > save on client and draw it
[QUOTE=Robotboy655;42858732]net library? Get data from DB on server > send it from server to client using net library > save on client and draw it[/QUOTE] Yea but I dont really understand the net. Can you give an exempel?
[url]http://wiki.garrysmod.com/page/Net_Library_Usage[/url]
[QUOTE=Robotboy655;42859180][url]http://wiki.garrysmod.com/page/Net_Library_Usage[/url][/QUOTE] Allready readed this and its confusing..
Ok so the net library is actually really simple once you understand it. You start by pooling a message serverside, which pretty much tells the server that this will be sent at some point. Optimally, you should do this a few seconds before the message is sent ( so in an init function or something. ) Let's say we want to send a friendly message to the player when they spawn. You start by pooling the message. [LUA]util.AddNetworkString( "FriendlyMessage" )[/LUA] And then you hook the spawning function. [LUA] -- THIS IS SERVERSIDE local function SendFriendlyMessage( ply ) net.Start( "FriendlyMessage" ) -- We start the message. This simply begins the net message, using the name you chose earlier. Ours is "FriendlyMessage" net.WriteString( "hello friend i love you" ) -- Now we write a string. You can write a lot of things into the message, check the wiki. net.WriteString( "message 2" ) -- If you want to write 2 strings at once, simple do net.WriteString twice. net.Send( ply ) -- It's then as simple as sending it to the player. You can also send it to the whole server with net.Broadcast end hook.Add( "PlayerSpawn", "SendFriendlyMessage", SendFriendlyMessage ) [/LUA] Now the server will send a message to a player whenever they spawn. What's left is telling the client to do something when they receive it. We do this with net.Receive( "MessageName" ), like so: [LUA] --THIS IS CLIENTSIDE local function ReceiveFriendlyMessage() local FriendlyMessage = net.ReadString() -- We can now read the message, in this case assign a variable to the first string sent local message2 = net.ReadString() -- this is the second string we sent from the server, just demonstrating that you can write 2 chat.AddText( Color( 251, 54, 123 ), "Your friendly message is: " .. FriendlyMessage .. "!" ) -- You add the message in chat ( in hot pink because hot ) end net.Receive( "FriendlyMessage", ReceiveFriendlyMessage ) -- When we receive the message, execute the function. [/LUA] Now the player will get a colored chat message whenever they spawn. If you have any more questions, just ask ( but refer to the wiki first! )
I can explain net my self for you but i wont give much code since rejax has pretty much typed a wiki for you. You always have to begin net with net.Start("What you want to call this connection") Then you have to write something to the net. net.WriteString("Hi") Writing something to net doesnt have to be a string. it can be ENT it can be many things. It shows all the possible ways to write to net on the wiki. Once you have written to your net you can do net.Broadcast() or net.SendToServer() or whatever you need to send too. Now on the server we have to make the message pooled. all you have to put is this: util.AddNetworkString( "the name of your net.start" ) now we right code under net.recieve! net. Receive("The name you put in your net.start"), function(ply) print("HI I RECEIVED NET") end
Thank you guys! My understanding for this is much better now! :)
Glad to help!
God damin it. Now how am i going to solve this on server side if i can't the the ply information? ERROR [code] [ERROR] gamemodes/darkrp/gamemode/modules/levelsystem/sv_levelsystem.lua:82: attempt to index global 'ply' (a nil value) 1. v - gamemodes/darkrp/gamemode/modules/levelsystem/sv_levelsystem.lua:82 2. unknown - lua/includes/modules/hook.lua:82 [/code] Code [code] util.AddNetworkString("SendDataToHUD") function SendHUDinfoMA(ply, cmd, args) local q = databaseObject:query("SELECT xp FROM players WHERE id=".. ply:UniqueID()) q.onSuccess = function(query, data) net.Start("SendDataToHUD") net.WriteString(data[1].xp) net.Send(ply) end end hook.Add("Think", "SendHUDinfo", SendHUDinfo) [/code]
You should really only query the database once, when the player first joins. Store all players' information as networked variables, so the player can access the variable easily. Sending a query [b]every tick[/b] for [b]each[/b] player is very impractical. You could then update the database every so often, or as the player quits.
Ohh yea :) That will ofc reduse lag aswell.
Sorry, you need to Log In to post a reply to this thread.