Pull database item from client-side

I have a simple DLabel item client-side. And I want to set the value of the DLabel to an entry in a person’s row via database.

As far as I know, client-side, I would place the following:

Server-side, I would define with

And then specify what cl_test needs to hold with:

Two questions, 1) am I going in the right direction, and #2 - how would I make it to where it fetches this information when the DERMA panel is opened with the DLabel inside that panel?

I’ve read that data can only be xferred via ConCommand when trying to fetch data this way. And if that is the case, how would I call that concommand right when the Panel is opened?

Thanks

1 ) yes
2 ) Here’s an example:
[lua]local function showMenu()
local main = vgui.Create( “DFrame” )
–some crap
local label = vgui.Create( “DLabel”, main )
–some crap

net.Receive( "mymessage", function()
	label:SetText( net.ReadTable().key )
end )

end[/lua]

Untested tho, should work.

[editline]21st August 2015[/editline]

Note that net.Receive() HAS to be inside the showMenu(), otherwise it’ll pop an error that label is nil.

[editline]21st August 2015[/editline]

The other way would be registering label outside the table and then moving net.Receive() outside too - but I believe it would be messy if you had more labels.

Thanks neth, last question, server-side I have this:

How would I call the concommand though? so it initiates getting this data?

Use PlayerInitialSpawn hook for this.

I thought about using that, and would have been way easier than what I’m trying to do, but what happens if the database information changes somewhere during their session, and they open that panel? I’d assume it would still show the old data?

I actually think this is a quite elegant way of doing it (thanks Neth for the idea)



local function showMenu()
	local main = vgui.Create( "DFrame" )
	--some crap
	local label = vgui.Create( "DLabel", main )
	--some crap

	net.Receive( "mymessage", function()
		label:SetText( net.ReadTable().key )
	end )
	
	net.Start("request_mymessage")
	net.SendToServer()
end


Then you send the data to whoever is requesting it via request_mymessage. That way you only send data when it’s needed instead of adding extra data to player spawn phase which already takes a while.

Edit: you could also use RunConsoleCommand to run your concommand instead of a net message to request for new data.

This is where I’m a bit confused I think.

If you use net.start and SendToServer, you are pushing data to server-side. What I need here, is:

  1. Player types !commands in-chat
  2. Panel opens, and requests information from function server-side (via toggling a concommand I assume as to what I’ve read)
  3. Server-side has a function which is toggled by concommand and queries a database table for ‘x’.
  4. Server-side attaches fetched information via net functions, and sends it back to client-side.
  5. Label outputs the information requested.

For your other question - you can actually write a function itself rather than just function’s name in the argument, ergo:
[lua]concommand.Add( “command”, function()
–code
end )[/lua]

  1. http://wiki.garrysmod.com/page/GM/PlayerSay
  2. No, it’s not toggling a concommand, first net.Receive creates a hook - ‘Receiver’ of some sorts and awaits data after which the code in function gets executed.
    What Wyozi has added sends info to the server, saying ‘hey, I’ve opened the menu, pass that data info to me now’
  3. sql.Query()?
  4. Send it via the same message that I’ve created receiver for
  5. Yeah, the receiver sets the code

Please, form your questions more clearly so we actually know what the F are you asking for.

You have two options:

  1. Player types !commands
  2. You capture this event in PlayerSay hook, and call a function that fetches the information from the database and sends a net message to client with all the data
  3. On client you receive the net message, open the panel from net.Receive and display information immediately

Option two:

  1. Player types !commands
  2. You capture this event in PlayerSay hook, and either use a console command or send a net message to client that opens the panel
  3. Once the panel opens, you send a net message to server ( or again, use a conosle command, but I wouldn’t a concommand here ) from the panel
  4. Server gets the net message, grabs data from DB and sends the net message to client
  5. CLient in the mean time displayss “LOADING” text while it awaits the net message from server
  6. Once the net message is received, grab the panel somehow, check if it still exists, if it does, fill it with info by either calling a function on the panel with data as arugment ( recommended ) or by directly putting data to the panel from the net message ( not recommended )

I’d ensure the request_mymessage receiver is limited in some aspect, otherwise you’re opening up the server up to a potential lag exploit via net message spam.
Although the client may end up just overflowing themselves.

Sorry Neth if I wasn’t clear and I appreciate all of your guys help. Typically I can figure these things out, but Client requesting data I’m still learning.

Everything else is working, my only confusion is, server-side, how I go about setting it up to where it only executes when the panel is opened. If I just drop the net items in, it works fine, but if I enclose it in a function, then obviously the function has to be called via client-side in order to retrieve the information.

Sorry again

[Edit] Two new posts prior to me posting then. I’ll read them and see if it states it more clearly for me. Was late posting this.

[editline]21st August 2015[/editline]

I appreciate the replies. I’m going to go try these explanations now and hopefully come up with a working version. Again, really appreciate the explanations all of you guys gave.

Yeah, don’t learn that.

Try handling as much stuff as you can serverside, the less control you give to the client the less a chance something might go wrong, intentionally or by accident.