Alright, so in a file called "models.txt" in the data folder I have:
models/player/combine_super_soldier.mdl
models/player/police.mdl
models/player/police_fem.mdl
models/player/combine_soldier_prisonguard.mdl
models/player/combine_soldier.mdl
I iterated through each line and added it to an array:
local line = 1
local Models = {}
local KeepSearching = true
local FileToRead = file.Open("models.txt", "r", "DATA")
while KeepSearching == true do -- Make sure we have a model array from the file..
line = line + 1
local ReadModel = FileToRead:ReadLine(line) -- Iterate through each line in the file..
if ReadModel != nil then
if util.IsValidModel(ReadModel) then -- Make sure the model is valid before adding it to the array.
table.insert(Models, ReadModel)
end
else
KeepSearching = false
end
end
It only adds the model to an array if the model is valid, but it only seems to add the last model: "models/player/combine_soldier.mdl" to the array even though all the models are valid.
It's probably because you have a newline character ('\n' or '\r') at the end of every line except for the last one. You will need to remove it, most likely with string.Replace (I haven't worked with reading files in GLua so I might be full of shit, but I think this is the problem)
Have you tried printing out (or insert into a table) each individual line, before running any checks obviously. That should hopefully give some info about what’s going on.
Instead of replacing it, how about getting a long string of the contents of a file, and just string exploding the \n? It'll create a table full of models for you and stuff.
Yup, that would be another way of doing it. On the other hand, idk what he is doing, why not just define the table and be done with it instead of reading it from a file (again, depends on what you want to do)
Rough code, but should do what you're looking for -
https://gist.github.com/TeddiO/c005eac2cedc8aa1f0ae585f0ab993e2
Just explain a few things, ideally you always want to load a file into memory where possible and close the handler. There's no context handlers for files in gmod and while garbage collection will eventually try to handle it for you, it's always better to try and close it sooner rather than later.
There's a Trim() thrown in there for good measure as I do expect the whole \r\n craziness to get handled, but it's not perfect (and in review, might actually overly-explode more than it should, but you can always fix that).
Wow, big thanks to you and Gmod4Phun, not only did you give me a much better way of doing these things but you also taught me a few things like:
local data = string.Explode("[\r\n]+", fileToRead:Read(), true)
This is so much better than using a while loop and iterate through each line.
fileToRead:Close() -- Close the file handler, bad to keep it open
I sadly did not know I needed to close the file after opening it, I thought it closed after it was read.
models[#models+1] = model -- Quicker to insert this way than table.insert.
I did not know this was better to use.
But yeah, needless to say it is working now so thanks again!
Sorry, you need to Log In to post a reply to this thread.