Need help reading a .txt file

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
Kumlin
Prole
Posts: 6
Joined: Wed Sep 04, 2013 11:53 am

Need help reading a .txt file

Post 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.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Need help reading a .txt file

Post 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.
Help us help you: attach a .love.
User avatar
Kumlin
Prole
Posts: 6
Joined: Wed Sep 04, 2013 11:53 am

Re: Need help reading a .txt file

Post 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!
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: Need help reading a .txt file

Post 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
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
User avatar
Kumlin
Prole
Posts: 6
Joined: Wed Sep 04, 2013 11:53 am

Re: Need help reading a .txt file

Post 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!
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 2 guests