• Reading from a txt file and turning it into a table
    3 replies, posted
I am working on a custom gamemode which currently contains 2 entities: lumrace_lum and lumrace_item. In order for the gamemode to be playable there needs to be spawn locations for these entities. I wrote a system that allows the gamemode to read these spawn locations from a txt file, much like ttt. [CODE]item 432 432 64 item -432 -432 64 item 432 -432 64 item -432 432 64 lum 0 0 32 lum 0 0 256[/CODE] This file is called lr_test_lumrace.txt, and it contains all of the spawn locations for the map lr_test. The first value is the entity, which is either 'item' or 'lum', the other 3 values are the x, y and z coordinates. [CODE]if not file.Exists("maps/"..game:GetMap().."_lumrace.txt","GAME") then error("Current map does not have an entity placement file associated with it.\n'maps/"..game:GetMap()..".txt' does not exist") end local a = file.Read("maps/"..game:GetMap().."_lumrace.txt", "GAME") a = devideString(a,"\n") for i,v in ipairs(a) do local b = devideString(a," ") if b[1] == "item" then table.insert(lumrace_spawn_item,{b[2],b[3],b[4]}) elseif b[1] == "lum" then table.insert(lumrace_spawn_lum,{b[2],b[3],b[4]}) else print("Unknown lumrace entity spawn: '"..b[1].."' ("..game:GetMap().."_lumrace.txt)") end end[/CODE] This piece of code is located in init.lua and takes the info found the txt file and puts it in 2 tables: lumrace_spawn_item and lumrace_spawn_lum. [CODE]function devideString(text,devideChar,skipEnter) local output = {} local bookmark = 1 skipEnter = skipEnter or false output[1] = "" for i=1,string.len(text) do if text:sub(i,i) == devideChar then bookmark = bookmark + 1 output[bookmark] = "" elseif skipEnter and text:sub(i,i+1) == "\n" then else output[bookmark] = output[bookmark]..text:sub(i,i) end end return output end[/CODE] This is the 'devideString' function which is located in shared.lua. I wrote this a while back for general usage in lua, so I'm assuming it should work for gmod as well. But when I try to run this code, I get the following error message: [CODE][ERROR] gamemodes/lumrace/gamemode/shared.lua:21: bad argument #1 to 'len' (string expected, got table) 1. len - [C]:-1 2. devideString - gamemodes/lumrace/gamemode/shared.lua:21 3. unknown - gamemodes/lumrace/gamemode/init.lua:15[/CODE] This is because for some reason the argument 'text' is a table instead of a string, but I can't find out why this happens. If anyone could help me understand why this happens and/or how I can fix it, that would be awesome. Thanks
[url]http://wiki.garrysmod.com/page/string/Explode[/url]
[QUOTE=PvM_marcus;50656404][url]http://wiki.garrysmod.com/page/string/Explode[/url][/QUOTE] or [URL="http://wiki.garrysmod.com/page/table/ToString"]http://wiki.garrysmod.com/page/table/ToString[/URL]
So I found the problem: in this chunk of code: [CODE]for i,v in ipairs(a) do local b = devideString(a," ") if b[1] == "item" then [...][/CODE] I was trying to devide each line individually, but the code was trying to devide the entire table. So to fix this I simply had to change [CODE]devideString(a," ")[/CODE] to [CODE]devideString(a[1]," ")[/CODE] Still, thanks for telling me about string.Explode and table.ToString!
Sorry, you need to Log In to post a reply to this thread.