Page 1 of 1
Need help reading a .txt file
Posted: Thu Sep 12, 2013 2:51 pm
by Kumlin
here is my code
Code: Select all
function love.load()
local file = assert(io.open("obj.txt", "r"))
file:seek("set")
stringTwo = file:read("*line")
end
function love.update()
end
function love.draw()
love.graphics.print(stringTwo, 100, 100)
end
It says the file does not exist even though the file is in the same directory as the main.lua file. I haven't been able to find a straight answer on how to read .txt files in lua which is kinda stupid at this point when what I'm trying to do is really simple.
Re: Need help reading a .txt file
Posted: Thu Sep 12, 2013 3:39 pm
by Robin
Welcome.
I'd suggest using
love.filesystem instead of io. If you simply want to read the whole contents of a file in one go, you can use
love.filesystem.read.
Re: Need help reading a .txt file
Posted: Thu Sep 12, 2013 3:56 pm
by Kumlin
Figured out how to read a file it was really easy once I didn't pack it into the .love file. but I have encountered another problem!
In the code below I wanted to select a random line from my text file to print out but I keep getting the same error.
grapics.lua:1265: bad argument #1 to 'print1' (string excpected, got nil)
Code: Select all
function love.load()
Ilength = 0
local file = assert(io.open("obj.txt", "r"))
items = {}
for line in file:lines() do
Ilength = Ilength + 1
table.insert(items, line);
end
math.randomseed(os.time())
math.random()
end
function love.update()
end
function love.draw()
love.graphics.print(items[selector(1, Ilength)], 100, 100)
end
function selector(range1, range2)
outcome = math.random(range1, range2)
end
Thanks so much for the help!
Re: Need help reading a .txt file
Posted: Thu Sep 12, 2013 4:07 pm
by Nixola
Code: Select all
function selector(range1, range2)
outcome = math.random(range1, range2)
end
In this function you assign a random number to the global variable "outcome" and then it just ends.
First: it's better to use local variables inside functions because of various reasons, among which we can find better speed and less chances to have two equally named variables in two different functions "colliding".
Second: you don't ever return the random number you get, so item[selector(a, b)] is always equal to item[nil]; to solve this you could either return outcome or math.random(range1, range2) directly.
Code: Select all
function selector(range1, range2)
local outcome = math.random(range1, range2)
return outcome
end
--or
function selector(range1, range2)
return math.random(range1, range2)
end
There's one last thing I just noticed: instead of having selector as 'wrapper' for math.random, you could call the latter directly:
Code: Select all
function love.draw()
love.graphics.print(items[math.random(1, Ilength)], 100, 100)
end
Re: Need help reading a .txt file
Posted: Thu Sep 12, 2013 4:16 pm
by Kumlin
Thanks so much that worked! Also I need to keep the selector function because I intend to use that throughout my code and rather than typing math.random every time I just want to use the selector function. Another thing I realized is that I need to get the process that selects the item out of the draw function because it gets called over and over again. Thanks for your help!