Question about TabletoKeyValues and doing a timestamp...

Right now i have a log file that is generated (this is a custom scenario; I just want to see what buttons are pressed at a certain time)

I’m using



mytable=   util.TableToJSON(table1, true)

 

file.Write( "Logfile.txt" , mytable)

It generates a log that looks like:

I was reading a little about key values…is there a way I can define them so that the first number in that log output (the 1,2,3) can be changed to a variable. I’m using a variable “elapsed” which records curtime, ideally I could set those first 3 numbers at that variable. (I am not sure how to go about this obviously)

My plan B is to just change all variables from strings to tables containing the elapsed variable and the string I need. This makes my log more cluttered but it does work and contains the information I need.

TableToJSON is not a loger function, it’s hut to change tables to a string and way back.
Use instead


table.concat(table1, "
")

Got this error message…

Where shoudl I check?

My table I’m trying to reference contains a table. Everytime the button is pressed I male a table that contains the Timestamp value (calling elapsed function) and the string that tells info about the button.

I can’t concatenate a table inside of a table right? The only other option for me should be to just make a variable for the time elapsed for each button and then when I create the table I put all the variables in.

There might be a more efficient way than doubling the amount of variables in my table but I can’t think of it.

Whenever you normally log a button press, do this instead:
[lua]table1[CurTime()] = your button text here[/lua]
The thing inside the brackets is the index (which by default is just 1, 2, 3…).

I am starting to grasp the concept but I’m not all the way there yet.

So I understand this correctly, I am telling it to change the index to every instance of CurTime being called but I’m not sure what the “your button text goes here would mean” Can you help me with my understand on this?

[editline]20th September 2015[/editline]

I’ve been trying to change the index however when I do and it seems to work, it will always append to the log file…even though I am only using file.Write…

Currently you are just inserting the values into the table. If Button 1 was pressed, you’re inserting “They pressed Button 1” into the table. Instead, you can do table[CurTime()] = “They pressed Button 1”.

How do I reference that? where is my table name in that syntax…I’m sorry for my ignorance; I am honestly trying to read through the wiki and comprehend this stuff…

I literally have :


Button3 = vgui.Create( "DButton", MED )
				Button3:SetSize(200, 100)
				Button3:SetPos(460,10,3)
				Button3:SetText( "Button 3 test!" )
				Button3:SetFont("Trebuchet20")
			
				Button3.DoClick = function ()
				
				table[CurTime()] = "You have pressed button 3"

end

Basically table.insert just inserts it into a numbered spot in a table. You can manually set any spot (index/key) in a table by referring to it. Table.something is equal to the thing after the dot being a string. You can use to make it a value instead.
[lua]table.var = 1 – Makes the value in the table with the key “var” to 1
table[“var”] = 1 – The same as above
table.entity = 2 – Puts it in the spot called “entity” (a string)
table[entity] = 2 – Requires that entity is defined and will put it in a spot indexed by that entity[/lua]
It is particularly helpful to use with entities inside, as you can refer to the same spot even if you get the entity by another mean:
[lua]function GM:PlayerInitialSpawn(ply)
table[ply] = math.random(1,5)
end

for k,v in pairs(player.GetAll()) do
print(table[v]) – Will print the number that the table got for that player on initial spawn
end[/lua]
Basically you can index your log string by the time by putting CurTime() inside the brackets like I showed earlier. This will call CurTime() and use the value it returned as the key/index. When you print a table it will say key: value, so that’s what you can do to make it show time.

Do I need to index it on each button DoClick function then? then when I am done I have a function I call that basically writes that table to a file?

[editline]20th September 2015[/editline]

Finally…i’m getting some results… {“8861.2314453125”:“Button 1 pressed”,“8860.32421875”:“Button2 Pressed”}

Just need to figure out how to clean it up … thank you so much for your diligence…how do I add you to people to reward when I am in power ?

TabeToJSON works fine for me?

I spoke to soon…the table is cleaned up but it doesn’t record the times when the button is NOT pressed, which is something I need… what to do?

I tried adding above the DoClick (when the button is actually created) to :


table["no time"] = "You did not press the button" 

…this works but they BOTH appear in the log…can’t think of the best way to have one override the other… I thought about using table.remove but I don’t know how to decide what order to use.

What exactly are you trying to achieve?

if you want logs which have keys represented by the date then do something like this ( done on mobile so probably wont work )




Logs = {}
Logs[ os.date("%d/%m/%Y", os.time() ) ] = {}

function AddLog( Key, Text )
	table.insert( Logs[Key], Text )
end



I have a derma panel with a bunch of buttons… I want to record whether or NOT they pressed a button and if they did, , what time did they press it?

Why not record if they pressed it, and if there is no data for them pressing it just assume they have not, there is no point in recording useless data.

Use os.date and os.time,

If you are doing it by steamid you can either store that as they key, or do something like this



Buttonstuff = {}

function AddButtonPress( SteamID )
	table.insert( Buttonstuff, { SteamID = SteamID; Date = os.date()} )
end

for k,v in pairs( Buttonstuff ) do
	print( v.SteamID, v.Date )
end



I gave you the code in the other post, but now I see you’re trying to log it.
As I said you should do table.concat.
First argument, the table to concat, 2nd, the concat value. In that case it was
beacouse that makes a new line, so a table argument per line, in order.

Also, never call a table, “table”, beacouse it replaces the table function library, that’s why it’s giving you that error

This is a very unique tech demo I am building. No true multiplayer, just two local machines. I see what you are saying by assuming if they haven’t or not, there is probably a way to generate something separate if the buttonpress is empty but I am just getting started with tables…

What you could also do, is table which has tables which each table has two values, the time (os.time()) and what he did, so then you loop through the whole table, formatting the time(os.date(format, time)) and putting it into a string thing.
I’ll post the code tomorrow, as usual.

The button being pressed is an action performed by the user. It occurs at a set moment it time. NOT pressing the button is not really an action. It doesn’t have a time, and it wasn’t performed by anyone. It’s just an outcome of the lack of an action.

If you want to have a table value for the button not being pressed, just make a variable for each button and set it to false. In DoPress, set the variable to CurTime(). After the menu is closed or whatever, go through each button’s variable. If it is false, then store to the table something that indicates so. If it isn’t false, store to the table the time.

I personally think a better table structure would be button_name:time. Each button is a key in the table, and the value is either “was not pressed”, or “pressed at <time>”.

If you want some actual code for this or get stuck, feel free to ask.

Thanks for your persistence. Below is basically what I have so you can see what I am doing vs trying to do…


Button11 = vgui.Create( "DButton", MED )
                                Button11:SetSize(200, 100)
                                Button11:SetPos(680,130,3)
                                Button11:SetText( "Button 11" )
                                Button11:SetFont("Trebuchet20")
                                Button11.DoClick = function ()
                                local elapsed = CurTime() - start_time         
                                success[elapsed] = "Var 1 results"                             
 
                               
                                end
 
Button41 = vgui.Create( "DButton", MED )
                                Button41:SetSize(200, 100)
                                Button41:SetPos(1120,610,3)
                                Button41:SetText( "END SCENARIO, WRITE LOG" )
                                Button41:SetTextColor( Color( 255, 255, 255 ) )
                                Button41:SetFont("Trebuchet20")
                                Button41.Paint = function( self, w, h )
                                draw.RoundedBox( 0, 0, 0, w, h, Color( 0, 0, 0, 255 ) ) end-- Draw a black button
                                Button41.DoClick = function ()
                           
                               
                                wedidit = util.TableToJSON(success, true)
                                file.Write ("logfile.txt" , wedidit)
                       
                                                                                                               
                                end

This gives me something like {
“2.9658203125”: “Var1 Results”,
“3.85546875”: “Var 2 Results”,
“3.333984375”: “Var 3 Results”
}

Which is a problem too because it doesn’t record whether the button was NOT pressed…but at first I need to at least get it sorted by time (as you can see, it’s not) Important that I get it sorted by time. I think I need a new table and do an insert for each button but I’m not sure how to go about structuring the syntax and what key, values to use…

… I think what I might do is make a separate table with all the NOTPRESSES vars and then just set their values to nil with the DoClick functions… (would a table return some nil values and some whole ones or should I just set it to zero?)