Can you reference a table as your content in file.Write ?
16 replies, posted
[CODE]recap = { Var1 ,
Var2
}
file.Write( "Logfile.txt" , recap)
print recap [/CODE]
Var1 & 2 are declared elsewhere but work fine. The Print command does return a value but it is that 0x3ef format. The problem is, when I tried to add multiple variables to my file.Write, it would only do the first one and not the subsequent variables. I thought referencing a table would be the solution but I must be doing something wrong.
You're supposed to use PrintTable(table) instead of just print to return the correct format
Honestly, I'm not entirely sure what you're asking here but util.TableToJSON/util.JSONToTable is good for saving tables in a txt file
[QUOTE=NiandraLades;48696892]You're supposed to use PrintTable(table) instead of just print to return the correct format
Honestly, I'm not entirely sure what you're asking here but util.TableToJSON/util.JSONToTable is good for saving tables in a txt file[/QUOTE]
Is there any other way to save multiple variables to a file? (from a single command)
I'm getting this [CODE]{"1":"VAR1SecondState"}[/CODE] the second variable doesn't even reappear and for some reason the command to reset the variable doesn't reset the table...i'll have to check some things but why doesn't the 2nd variable appear in the table at all?
[URL="http://wiki.garrysmod.com/page/util/TableToKeyValues"]util.TableToKeyValues[/URL] is a thing but the best method would be JSON/a custom serialiser
[url=https://github.com/vercas/vON/blob/master/von.lua]vON[/url] is also a thing.
I would suggest json instead because it supports more data types.
text files are kind of gross, but if you must use [URL="https://github.com/SuperiorServers/plib_v2/blob/master/lua/plib/libraries/pon1.lua"]pon[/URL]
Let me explain what I am trying to do. Think DnD you have a dungeon master.
In this game mode, the DM presses various buttons based on the actions/questions/commands of users he observes.
So I have a panel with like 40 buttons that each change a variable through netserver to VAR = Button pressed...
I'm trying to have a log file that I can access at the end that contains all these button presses...what is the best way to go about it...?
EDIT: I don't understand why PrintTable tablename returns a 1 in it and only one of the variables....where doe sthe 1 come from
[QUOTE=Blakestr;48697545]Let me explain what I am trying to do. Think DnD you have a dungeon master.
In this game mode, the DM presses various buttons based on the actions/questions/commands of users he observes.
So I have a panel with like 40 buttons that each change a variable through netserver to VAR = Button pressed...
I'm trying to have a log file that I can access at the end that contains all these button presses...what is the best way to go about it...?[/QUOTE]
Is this log file for humans or for computers to read?
[QUOTE=!cake;48697567]Is this log file for humans or for computers to read?[/QUOTE]
Humans...me mainly
[editline]16th September 2015[/editline]
[QUOTE=Blakestr;48697894]Humans...me mainly[/QUOTE]
I'm only using the logfile for information, not computation. I literally want to know if a user pressed a button or if not...
You didnt even close the table, and the code in your start doesnt really look clean, also define the values, >-<
[QUOTE=Blakestr;48697545]EDIT: I don't understand why PrintTable tablename returns a 1 in it and only one of the variables....where doe sthe 1 come from[/QUOTE]
If you do not give the value a key (which you didn't), it will be given a sequential number key. In your original post, [1] = Var1 and [2] = Var2
[QUOTE=whitestar;48701363]You didnt even close the table, and the code in your start doesnt really look clean, also define the values, >-<[/QUOTE]
The table is closed. I don't know why it moved the bracket over like that... It's all even in notepad++...
The variables in the table are defined earlier in my script. They are both defined as strings and are changed to a different screen depending on what derma buttons I press.
I still can't figure out why it doesn't display correctly.
NOTE To people who find this in the feature....if you have a problem, READ YOUR CODE. You might of had a single fucking typo that made you think your table was wrong. It worked fine once I found that one bad reference.
I just need to figure out how to parse the line so that it's not all a table on one line...I'll post the solution when I find it.
[QUOTE=Blakestr;48705874]NOTE To people who find this in the feature....if you have a problem, READ YOUR CODE. You might of had a single fucking typo that made you think your table was wrong. It worked fine once I found that one bad reference.
I just need to figure out how to parse the line so that it's not all a table on one line...I'll post the solution when I find it.[/QUOTE]
I don't see why you don't want to use TableToJSON. If you don't want to use that, then you need to write your own parser that goes through each value in the table and writes it to the file.
[QUOTE=roastchicken;48706052]I don't see why you don't want to use TableToJSON. If you don't want to use that, then you need to write your own parser that goes through each value in the table and writes it to the file.[/QUOTE]
I'm using TableToJSON .... it still ends up on one line
If you want it to be formatted and indented correctly, see here:
[IMG]http://i.imgur.com/XmtBQ6v.png[/IMG]
[QUOTE=AK to Spray;48708253]If you want it to be formatted and indented correctly, see here:
[IMG]http://i.imgur.com/XmtBQ6v.png[/IMG][/QUOTE]
Thanks, I got this to work. I had seen this page but couldn't figure out how to get pretty print to work.
For anyone in the future who finds this thread, here is the code that works for me.
[CODE]util.AddNetworkString("writelog")
net.Receive("writelog", function ()
recap = { Var1 , // these are variables that are declared earlier in this document and changed earlier as well.
Var2,
Var3 }
recaptable = util.TableToJSON(recap, true)
file.Write( "Logfile.txt" , recaptable)
// do file write for current time
print ("Log written, see Logfile.txt or check below")
print recaptable
end)
[/CODE]
Sorry, you need to Log In to post a reply to this thread.