• Reading rom a flat file
    6 replies, posted
Hey , i am want to make a custom model thing , where if you are not in the flat file , you will not have a custom model. so i want the flat file like this: Steam:ID Model but the only thing is , i dont know how to asociate the steam id by the model that folows. this is the code for finding each steam ID , but i still dont know how to make it work function CustomModel(ply , model) steamids = string.Explode( "\n", file.Read("custommodels/people.txt") ) model = string.Explode( " ", file.Read("custommodels/people.txt") ) for k,v in pairs(steamids) do if self:SteamID() == v then v:SetModel(model) return true end end return false end but that doesnt work.
You could get them into table pairs by doing: [lua] local custommodels = {} local lines = string.Explode('\n', file.Read("custommodels/people.txt")) for k,v in pairs(lines) do local exploded = string.Explode(' ', v) table.insert(custommodels, {exploded[1], exploded[2]}) end [/lua] Now custommodels[1][1] will be a SteamID and custommodels[1][2] will be the model name or whatever. Do PrintTable(custommodels) if you don't understand the structure.
Even though that method would work, I would be more inclined to use glon.
[QUOTE=MakeR;20269692]Even though that method would work, I would be more inclined to use glon.[/QUOTE] Yes, glon would be far better. Research that if you don't want human readable and editable files. Because if you load it that way, you have to write it to a file that way too.
Glon and Explode are both excessive for this. [code] local k, id, mdl, txt CustomModels = { } txt = file.Read( "your/custom/models/file.txt" ) for k in txt:gmatch( "%C+" ) do id, mdl = k:match( "(%C*)|(%C+)" ) if id and mdl then util.PrecacheModel( mdl ) CustomModels[ id ] = mdl end end [/code] Saving is simple as well [code] local res, k, v res = "" for k, v in pairs( CustomModels ) do res = string.format( "%s%s%s|%s", res, res ~= "" and "\n" or "", k, v ) end file.Write( "that/path/from/earlier.txt", res ) [/code] Untested, but that should do the job.
Sucks because it turns all numeric keys and values into strings, and for various other reasons.
Sorry, you need to Log In to post a reply to this thread.