Help me understand using file.Write to create a logfile...

I am trying to think of the simplest/best practice way to do this. I have buttons on a derma panel. Each has a DoClick function which various functions. I want it to record the time when that button was pressed and the log file to say when it was pressed and to say **if it wasn’t pressed. **
Right now my thinking is something like this :


Button1 = vgui.Create ("DButton", TAB1) 
Button1.SetSize/Pos/Text stuff

wasButton1PressedVar= 0

Button1.DoClick = function ()

//all my functions 
wasButtonPressedVar = 1 

//not sure what to do after

end


Here my comprehension of lua starts to wane. I’m not sure if Netlibrary is the right solution because I will still need something server side for if the player DOESN’T touch the button. I think I need file.Write to be server side but how best to manage it? I’m considering using a command (netlibrary from this derma panel) that would essentially be “Write log file now”. When that happens, the script would check to see the status of all the button variables pressed, if they contain Timestamps, and if so, write them to file. I’m just a little foggy how how the process would go. Thanks to other users help here, I’ve used Gamemode in scripts to make a change a variable server-side FROM a client-side. I’ve never done it the other way around. I am a little confused about timestamps…see this thread from a way back. The problem is my variables are local because they are inside the net library function but they are getting global nil errors as a result… Still learning the basics I think…

For when the player doesn’t press the button, how are they able to choose not to? Is there a close button, or is there a second button, does the button time out, etc.?

But yes, if you want this to be logged on the server, you will need to make use of the net library, and tell it to send a message to the server.

For the actual log file, have you checked out

file.Write and

file.Append?
I strongly recommend you give these a look and try to learn how they work. If you have any trouble, let me know, and if I have time, I’ll try to help more.

Thanks. I’m taking a look at them but what is holding me back is if they DON’T press the button. That is just as important so I am trying to think of a way to have a default string that will say, “they didn’t press it” and then a net library command that sends and overwrites this. Experimenting now…

Well like I asked earlier, what do you define as not pressing the button? Does the button disappear after a short delay once it appears, is there a close button, is there a second button that does something and then closes the dialogue, so on and so forth…

Sorry I missed that. It is a derma panel with menus and buttons. The button doesn’t dissappear. Some buttons actually change functions after they are pressed the first time. (not going to try to track 2nd times)…

So there are ten buttons for examples. I want my log to look something like this :

“Player did NOT hit button 1”

“Player hit button 2 @ 13 minutes 3 seconds in”

Player did NOT hit button 3.

Player did NOT hit button 4.

And so on… Does that make more sense?

So then you can use a net message from inside the callback of pressing a button.
Example, if second button is pressed, it would simply transmit a net message with a number, like


net.Start("ButtonPressed")
net.WriteInt(2, 16) -- 2 is the number of the button
net.SendToServer()


-- Now on the server's end

net.Receive("ButtonPressed", function(length, client)
    local button = net.ReadInt(16)
    for i = 1, 4 do -- 4 would be whatever amount of buttons you have
        if i == button then
             -- file.Append() -- write the line in the file that they pressed this button.
        else
             -- file.Append() -- write the line that they did NOT press this button
        end
    end
end)

Awesome… Quick question - I have 2 tabs with 10 buttons each. So I treat it as 20 buttons or 10 buttons with a special tab designated somehow?

Why did they label this advice as dumb? Does it not work?

Only 1 person (MeepDarknessM) rated it dumb, and you shouldn’t listen to him. He legit thinks that sending LocalPlayer() through net messages is a good idea.

Instead of making a variable like BUTTON1PRESSED do,
Button1.pressed (using the same variable you use to create the button).
So, when they exit the panel what you can do is loop by all the buttons and send to the server which buttons did he press.
I’ll post some code tomorrow.

Please do. I’m terrible at loops.

I just have to know one thing.
How are you placing buttons? Putting them manually in some pos on the menu or a DPanelList

[editline]28th August 2015[/editline]

Or what

Pos… hope that’s not an issue … when i was learning Derma I just put in a position and copied its POS, modified slightly, rinse and repeat. I didn’t experiment with DPanelList much…

I mean in this panel you’re making

It is a dpanel and inside of it I have buttons that have POS set for them all individually.

Well I’ve gone over what I gave you and I’m not sure what’s ‘bad’ about it…?
If someone disagrees, please tell me why rather than simply rating.

You have to add this to your code

Clientside:



local panels = panel:GetParent() -- Change panel here

if panels then
	for k, v in pairs(panels) do
		if v:GetClassName() == "DButton" then
			v.Pressed = false

			if not v.DoClick then
				v.DoClick = function(self)
					self.Pressed = true
				end
			end
		end
	end
end
-- If the button has a .DoClick function, you should put on it this: button.Pressed = true

-- Now the networking part
-- Add this to the close button
local pressedbuttons = {}

for k, v in pairs(panels) do
	if v:GetClassName() == "DButton" then
		local button = {
			[v:GetText()] = v.Pressed
		}
		table.Add(pressedbuttons, button)
	end
end

net.Start("PanelClosed")
	net.WriteTable(pressedbuttons)
net.SendToServer()


Serverside


util.AddNetworkString("PanelClosed")

net.Receive("PanelClosed", function(len, ply)
	local buttons = net.ReadTable()

	for k, v in pairs(buttons) do
		-- K is the name of the button (the text that you see)
		-- V is if the button is pressed or not
		-- So, log stuff in here
	end
end)

Wow man. This is phenomenal stuff. Quick question, do I use the top most panel for the panel name or the one right below it. I have several tabs with different menus that I use. I’m working on putting the code in now for all this and I really appreciate the time you put in it.

EDIT : Answered my own question. I has to be whatever panel I want to use that I create with DPanel command. So If I want to log everything, without creating somethign more complex, I could just duplicate this process (not a big deal really) and then change the variable names and netlibrary references. ?

[editline]30th August 2015[/editline]


-- Add this to the close button
local pressedbuttons = {}

for k, v in pairs(panels) do
	if v:GetClassName() == "DButton" then
		local button = {
			[v:GetText()] = v.Pressed
		}
		table.Add(pressedbuttons, button)
	end
end

net.Start("PanelClosed")
	net.WriteTable(pressedbuttons)
net.SendToServer()

I don’t a close button. I have a concommand that creates the entire menu. In the game I just bound that command to a key…what should I do from here?

I dont know, try searching on google: “Do function when panel closed”

What function are you referring to in terms of close and open? I’ll just learn those and use them.