[lua]local num = #file.Find( "trader/*.txt", "DATA" )
local list = file.Find( "trader/*.txt", "DATA" )
for i = 0, num do
for _, f in pairs(list) do
local test = util.JSONToTable( file.Read("trader/"..f, "DATA") )
print(test["offer"])
print(test["offeramount"])
print(test["want"])
print(test["name"])
end
end[/lua]
I am currently running this serverside, and it's working the way I want it to but I am unsure of how to send the results of the function to a derma panel.
I am trying to create a Dlist with the values from every txt file in the folder, when I tried to run this in the actual Derma script it did not work (assuming because Derma is clientside) so my question is, how would I go about gathering all of the data from every JSON file in the trader/ folder, and organize and display it into a DListView. I am very new to Derma and somewhat new to Lua and I have been trying to do this for quite a while now with no decent results unless I change the way I want my system to work. Disregard any bad coding you may see - I am not here for looks I am here for works =p
You send the data from server to client using net library.
I figured as much, I have a tab with the Net Library Usage wiki open, just a little confused as to the use. If I was going to send a couple variables over I would have no problem, but I am confused as to how send the values for probably 10-20 text files over to a Derma DListView and have it create the fields with said data..not asking for you to do it just asking for any advice that may help, if there is any more advice to be given =p
[editline]27th November 2013[/editline]
I will tamper around with the Wiki and make up something to post here, just want to know if I am doing it right or not.
[editline]27th November 2013[/editline]
Do I place the net.Start and whatnot inside of the function? Also, the function loops multiple times how would I send that many tables through the net library without names overlapping?
Not rly sure what you mean with:"without names overlapping" .. but you can make it check what it should do with the data, using net.WriteString or net.WriteFloat.
[lua]
-- Server
function SendTable(type,table,ply)
net.Start("potato")
net.WriteString(type)
net.WriteTable(table)
net.Send(ply)
end
[/lua]
[lua]
-- client
net.Receive( "potato", function( len )
local type= net.ReadString( )
local table = net.ReadTable()
if type == "ply-data" then
--blablabla with var: table
elseif type == "file-data" then
-- blablabla with var: table
end
end )
[/lua]
(Made this outside notepad++ .. so might be a few type-errors. But you get the idea)
btw: net.WriteTable .. isn't the best function .. but it's the lazy one
[lua]local num = #file.Find( "trader/*.txt", "DATA" )
local list = file.Find( "trader/*.txt", "DATA" )
for i = 0, num do
for _, f in pairs(list) do
local test = util.JSONToTable( file.Read("trader/"..f, "DATA") )
net.Start("potato")
net.WriteTable(test)
end
end[/lua]
Something like that?
I call the function using a concommand when I click a DButton, how would I organize what is sent to be listed in the DListView? It's 5 AM for me and I am a little confused so don't be too critical. :dance:
or would it be
[lua]local num = #file.Find( "trader/*.txt", "DATA" )
local list = file.Find( "trader/*.txt", "DATA" )
for i = 0, num do
for _, f in pairs(list) do
net.Start("potato")
local test = util.JSONToTable( file.Read("trader/"..f, "DATA") )
net.WriteTable(test)
end
end[/lua]
Get some sleep and continue working when you can think.
K got sleep now but still not grasping the entire usage of this.
[lua]function SendTable(type, table, ply)
net.Start("potato")
local num = #file.Find( "trader/*.txt", "DATA" )
local list = file.Find( "trader/*.txt", "DATA" )
for i = 0, num do
for _, f in pairs(list) do
local table = util.JSONToTable( file.Read("trader/"..f, "DATA") )
net.WriteTable(table)
net.Send(ply)
end
end
end[/lua]
I really can't find many things online as to how to properly use this...
I have no idea what you are trying to do with that code, but
[code]
function SendTable(type, table, ply)
local list = file.Find( "trader/*.txt", "DATA" )
for _, f in pairs(list) do
local table = util.JSONToTable( file.Read("trader/"..f, "DATA") )
net.Start("potato")
net.WriteTable(table)
net.Send(ply)
end
end[/code]
Don't forget about util.AddNetworkString serverside and net.Receive clientside
Clientside(Derma Panel)
[lua]function Receive()
net.Receive("potato", function( len )
print("shit actually worked")
local test = net.ReadTable()
DermaListView:AddLine(test["offer"],test["offeramount"],test["want"], test["wantamount"])
end)
end
hook.Add("Think", "Think", Receive)[/lua]
ServerSide(init.lua)
[lua]util.AddNetworkString("potato")
function SendTable()
local list = file.Find( "trader/*.txt", "DATA" )
for _, f in pairs(list) do
local table = util.JSONToTable( file.Read("trader/"..f, "DATA") )
net.Start("potato")
net.WriteTable(table)
net.Send(ply)
net.Broadcast()
end
end
concommand.Add( "table", SendTable )[/lua]
Error that shows only AFTER the first person opens it (assuming this error is due to someone recreating the derma list) How would I have it recreate the derma list for each player instead of for the whole server? The list works, but only the first time it is opened, the second person to open the dlist gets the error..How would I update the table through my net function without it throwing that error...
[code]
[ERROR] gamemodes/gmstranded/gamemode/cl_test.lua:145: attempt to call method 'AddLine' (a nil value)
1. func - gamemodes/gmstranded/gamemode/cl_test.lua:145
2. unknown - lua/includes/modules/net.lua:31
[/code]
You create the whole panel in net recieve, or at least check if it's valid or not
Hmm, didn't really think of that for some reason - ill try it out =p thanks
[editline]28th November 2013[/editline]
Could I remove the think hook if I create the whole panel every time via concommand?
Also, would there be a conflict if multiple people opened it at the same time?
[editline]28th November 2013[/editline]
I am planning on making a dbutton that selects the listing and gives items based on it, if another player recreates the panel while he is looking through them would it cause any problems? (I really don't have many people to help me with testing at this time of night). I know you guys get noob questions like all of mine all the time and they probably irritate you, but when I look something up and can't figure it out through google/wikis, then forums are the places I turn to =p
Omg, I haven't noticed it.
[code]
util.AddNetworkString("potato")
function SendTable(ply)
local list = file.Find( "trader/*.txt", "DATA" )
for _, f in pairs(list) do
local table = util.JSONToTable( file.Read("trader/"..f, "DATA") )
net.Start("potato")
net.WriteTable(table)
net.Send(ply)
end
end
concommand.Add( "table", SendTable )[/code]
[code]
net.Receive("potato", function( len )
print("shit actually worked")
local test = net.ReadTable()
DermaListView:AddLine(test["offer"],test["offeramount"],test["want"], test["wantamount"])
end)
[/code]
[editline]28th November 2013[/editline]
There. That should work.
So the think hook and the broadcast was the problem?
They are not supposed to be there. That's not how it works. The function in net.Receive will be executed each time a net message with that name arrives to client. [b]It is not instant[/b]. But there's no reason to add that listener every frame.
net.Broadcast is used to send the message to [b]all players[/b], net.Send( Player ) sends it to specific player, exactly what we need here.
Ah I see, how long does it take if not instant? Will there be enough time before the dlist gets created and needs the values to populate it?
[editline]28th November 2013[/editline]
It doesn't seem to be adding to the dlist at all now, nor printing my "shit actually worked" =p
[editline]28th November 2013[/editline]
If I remove the broadcast and think hook nothing happens, if i remove just the think hook nothing happens, if i remove the broadcast and keep the think hook nothing happens
TL:DR without both the Broadcast and think hook it seems to not work at all.
[editline]28th November 2013[/editline]
Scratch all that, added in the (ply) into the sendtable and it worked, afterwards noticed that you added it as well -_- sorry sorry
[editline]28th November 2013[/editline]
Without the think hook it shows an empty table though, are there any alternatives? What are the downsides to using that think hook?
Stop worrying about the think hook, you don't use it for such thing.
If the table you receive is empty, it's because the table sent is empty. Which could be because it reads nothing in your files?
Without the think hook it is empty, with the think hook it has data..how does that have nothing to do with the think hook?
[editline]28th November 2013[/editline]
K got it working after messing around with the placement of the net.Receive - without the think hook =p
Sorry, you need to Log In to post a reply to this thread.