How to do a timestamp? (stopwatch, counts UP, not down)

I want to start a stopwatch that counts UP (not down like timer)

Is this possible? Then, once I have it, I want to be able to reference it at anytime. Is there any function that can do this? create.Timer and CurTime don’t work the way I want and I get function: 0x164d6408 when I try to print to console.

There is possibly better ways - but you could create a timer that repeats every 1 second with a variable of your choice, and add 1 every time to that variable, so each time the timer calls it will add 1.

[lua]
local val = 0
timer.Create(“stopwatch”, 1, 0, function() val = val + 1 end)
[/lua]

This would keep adding 1 to “val” each second.

Thanks for the quick reply…how would I accomplish this?


timer.Create ("stopwatch", 1, 0, 1+localvar) 
end


?

I edited my post above with what you want :wink:

Awesome…thank you very much.

What’s the most efficient way to make a timestamp for individual buttons pressed on a derma panel, or will I need a new variable set in a net.receive function for each one?

If you get a function reference when you refer to CurTime, make sure to refer to it as CurTime() instead, so that the result of calling the function is obtained, rather than the address of the CurTime function.

To answer the original question, to count up, you can do this:

[lua]

local start = CurTime()

– … a few seconds later…

function SecondsSinceStart()
return CurTime() - start
end

[/lua]

Every time you call SecondsSinceStart(), it tells you how much time from that initial call to “local start = CurTime()” has passed.

Timer way is stupid. CurTime way should work just fine:



local start_time = CurTime()
-- some code
local elapsed = CurTime() - start_time
print(elapsed)


EDIT: God dammit, ninja’d . Yet again.

Bumping this because I want to know how to take the results of this and use them as a key for a table?

if you create the buttons in a for loop, then use K as an identifier, and use that somewhat as variable.

[lua]YourTableHere[CurTime() - start_time] = “Your string here”[/lua]

I’m dumb for not posting code earlier, granted I was away from home and didn’t have access to my script. (Wanted to see if I could get an answer while I was traveling)


       PanelTable.Button3      = vgui.Create( "DButton", PanelTable.FirePanel )
        PanelTable.Button3:SetSize(200, 100)
        PanelTable.Button3:SetPos(440,10,3)
        PanelTable.Button3:SetText( "Button 3" )
        PanelTable.Button3.DoClick = function (self)
                self.Pressed = true
                self.TimePressed = os.time()
        end
table.insert(PanelTable.tblButtons, PanelTable.Button3)
       

Based on what you are saying, I should change this to something like:


       PanelTable.Button3      = vgui.Create( "DButton", PanelTable.FirePanel )
        PanelTable.Button3:SetSize(200, 100)
        PanelTable.Button3:SetPos(440,10,3)
        PanelTable.Button3:SetText( "Button 3" )
        PanelTable.Button3.DoClick = function (self)
                self.Pressed = true
                self.Elapsed = CurTime() - start_time
        end
table.insert(PanelTable.tblButtons, PanelTable.Button3)
       

Right now the code for when the panel closes (I actually need this to be a button and have everything stay persistent even if the panel opens/closes)


   function PanelTable.base:OnClose()
                local panels = PanelTable.tblButtons
                if panels then
                        local pressedbuttons = {}
                        for k, v in pairs(panels) do
                                local buttons = { v:GetText(), v.Pressed and v.Elapsed }
                                table.insert(pressedbuttons, buttons)
                        end
                        net.Start("SendPressedButtons")
                                net.WriteTable(pressedbuttons)
                        net.SendToServer()
                end
        end

Right now this is sent through a loop serverside :


util.AddNetworkString("SendPressedButtons")
net.Receive("SendPressedButtons", function(len, ply)
        local PanelTable = net.ReadTable()
        if not PanelTable then return end
       
        local TextToWrite = ""
        for k, v in pairs(PanelTable) do
                if v[2] and isnumber(v[2]) then
                        TextToWrite = TextToWrite .. "[" .. os.date("%m/%Y", v[2]) .. "] " .. "The button " .. v[1] .. " was pressed
"
                else
                        TextToWrite = TextToWrite .. "[NONE] The button " .. v[1] .. " was not pressed
"
                end
        end
        ply:ChatPrint("Did it work... " .. TextToWrite)
        file.Write("helpme.txt", TextToWrite)
end)
 



I’m confident my syntax is not correct. Can you help? I tried putting in just v[2] into the TextToWrite and it returns 12/1969…

Here is proper syntax for TextToWrite :


TextToWrite = TextToWrite .. math.Round(v[2],2).. " The button " .. v[1] .. " was pressed
"

It would also help you if you didn’t have the other copy of this code further down when you began migrating your test lua’s to your actual regular lua. Dumbass. (yes I’m talking to myself)