what does 'gmatch' (string expected, got nil) mean?
17 replies, posted
Hello, since the last update, my logging system broke. I hate messing with string's, so I'm asking you what I can do to fix this little line.
[lua]
local logrecs = string.Explode("\n", file.Read("logging/log.txt"))
[/lua]
This is the only code that breaks my logging system.
Exploding by "\n" never worked, use this instead:
[lua]
for logline in string.gmatch(file.Read("logging/log.txt"), "%C+") do
print(logline)
--stuff
end
[/lua]
I'm using it on a DListView, and it worked like some weeks ago.
Have you atleast tried using that one?
[url]http://codepad.org/fGjrEEoA[/url]
I'm going to test it now.
[editline]26th February 2011[/editline]
Yeah, that worked. Thanks.
Remember, that code will fuck up if you tabs and other special charachter in your text file, I think people like raBBish can give you a better solution for breaking on newlines.
[QUOTE=Loures;28298107]Remember, that code will fuck up if you tabs and other special charachter in your text file, I think people like raBBish can give you a better solution for breaking on newlines.[/QUOTE]
Indeed.
[lua]for line in (file.Read("somefile") or ""):gmatch("[^\n\r]+") do
-- stuff
end[/lua]
raBBish, providing better patterns since 2007.
I just assumed that escape charachters didn't work in patterns since they don't work in string.Explode.
Okay, but I can't seem to get the user to read the file trough a usermessage. Maybe it's caused by the file being in addons?
[editline]26th February 2011[/editline]
It does write to my server when someone dies, but doesn't read it and put it on the DListView.
[QUOTE=Loures;28298729]raBBish, providing better patterns since 2007.
I just assumed that escape charachters didn't work in patterns since they don't work in string.Explode.[/QUOTE]
They work fine in string.Explode.
Anyone has any idea what can cause it to not be able to read the file?
[QUOTE=Persious;28299974]Anyone has any idea what can cause it to not be able to read the file?[/QUOTE]
1) A null byte
There is no other reason for being unable to read a text file in GMod.
[editline]26th February 2011[/editline]
Post your code to read the file and the contents of the file.
Yeah, will do.
[lua]
-- sv_logging.lua
function playerDies( victim, inflictor, attacker )
if inflictor:IsPlayer() then
inflictor = inflictor:GetActiveWeapon()
end
if attacker == victim then
filex.Append("logging/log.txt", tostring(os.date("%d/%m/%y")).." - "..tostring(os.date("%H:%M:%S")).." - "..attacker:Name().." suicided!".. "\n")
else
filex.Append("logging/log.txt", tostring(os.date("%d/%m/%y")).." - "..tostring(os.date("%H:%M:%S")).." - "..attacker:Name().." killed "..victim:Name().. " with "..inflictor:GetClass().. "\n")
end
end
hook.Add( "PlayerDeath", "playerDeathTest", playerDies )
[/lua]
[lua]
-- cl_logging.lua
local LoggingListView = vgui.Create("DListView")
LoggingListView:SetParent(LoggingMenu)
LoggingListView:SetPos(3, 50)
LoggingListView:SetSize(454, 375)
LoggingListView:SetMultiSelect(false)
LoggingListView:AddColumn("Entry")
for logrecs in (file.Read("logging/log.txt") or ""):gmatch("[^\n\r]+") do
LoggingListView:AddLine( logrecs ) end
LoggingListView.OnRowSelected = function(self, row)
local confirmation = DermaMenu()
confirmation:AddOption("Copy to Clipboard", function() SetClipboardText(self:GetLine(row):GetValue(1)) end)
confirmation:Open()
end
[/lua]
Contents of logging/log.txt?
[editline].[/editline]
Also, are you networking the contents to the client? At the moment you're reading from a file when I presume is empty, since the file is being read from the client not from the server.
I told him he needed to network it to the clients, you can't read files directly from the server.
You really need to network that to the clients. I'll probably get slated for this, but I'd use the datastream, since splitting a string that size into 252 bytes would be a tedious task.
Then I'll need to learn how to use datastream, but thanks!
Sorry, you need to Log In to post a reply to this thread.