Reading and writing

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.
Delibrete
Prole
Posts: 37
Joined: Mon Dec 05, 2011 11:51 am

Reading and writing

Post by Delibrete »

How would one go about reading and writing to text files with love?
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: Reading and writing

Post by Roland_Yonaba »

In pure Lua, you should have IO library... Working with löve, ou should take a look at love.filesystem. All functions you do need to perform what you want are packed in that table.
Delibrete
Prole
Posts: 37
Joined: Mon Dec 05, 2011 11:51 am

Re: Reading and writing

Post by Delibrete »

Roland_Yonaba wrote:In pure Lua, you should have IO library... Working with löve, ou should take a look at love.filesystem. All functions you do need to perform what you want are packed in that table.
I see the functions are there, but I don't know how to use them. How would I use those functions?
User avatar
trubblegum
Party member
Posts: 192
Joined: Wed Feb 22, 2012 10:40 pm

Re: Reading and writing

Post by trubblegum »

Delibrete wrote:
Roland_Yonaba wrote:In pure Lua, you should have IO library... Working with löve, ou should take a look at love.filesystem. All functions you do need to perform what you want are packed in that table.
I see the functions are there, but I don't know how to use them. How would I use those functions?
Is it really more work to click a link than it is to write a forum post asking someone else to do it for you?
If your attention span is so short that you didn't get as far as the example code at the top of the page in the docs, then programming is not for you.
Delibrete
Prole
Posts: 37
Joined: Mon Dec 05, 2011 11:51 am

Re: Reading and writing

Post by Delibrete »

trubblegum wrote:
Delibrete wrote:
Roland_Yonaba wrote:In pure Lua, you should have IO library... Working with löve, ou should take a look at love.filesystem. All functions you do need to perform what you want are packed in that table.
I see the functions are there, but I don't know how to use them. How would I use those functions?
Is it really more work to click a link than it is to write a forum post asking someone else to do it for you?
If your attention span is so short that you didn't get as far as the example code at the top of the page in the docs, then programming is not for you.
Well I've been trying for a few days now to read and write to text files, but it's ok now I've figured it out. But you're right, programming is probably not for me.
User avatar
trubblegum
Party member
Posts: 192
Joined: Wed Feb 22, 2012 10:40 pm

Re: Reading and writing

Post by trubblegum »

No worries .. some people are programmers, others are designers, and yet others are taxidermists.
But Rome wasn't built in a day, so don't give up just yet :)
Delibrete
Prole
Posts: 37
Joined: Mon Dec 05, 2011 11:51 am

Re: Reading and writing

Post by Delibrete »

trubblegum wrote:No worries .. some people are programmers, others are designers, and yet others are taxidermists.
But Rome wasn't built in a day, so don't give up just yet :)
I've been trying for years to become a programmer and a designer, hopefully I'll get there eventually. Thanks man :)
User avatar
sanjiv
Citizen
Posts: 88
Joined: Mon Feb 27, 2012 5:11 am

Re: Reading and writing

Post by sanjiv »

Help. In the following code, I'm trying to write 'hello text file!' to text.txt at the root of my game folder (same level as main.lua). Basically, I want to run the LOVE game, press a key, and have something written to a file.

Code: Select all


function love.load()

   text= love.filesystem.newFile( 'text.txt' )

end

function love.keypressed(key,unicode)

   if key == "space" then
	txt:open('w')
	txt:write("hello text file!")
   end

end

1) How do I get this to work?
2) What if I want to write to text.lua instead of text.txt?
3) What about navigating into folders within the game folder?
Zeliarden
Party member
Posts: 139
Joined: Tue Feb 28, 2012 4:40 pm

Re: Reading and writing

Post by Zeliarden »

Hi!

1.

Code: Select all

   if key == "space" then
should be

Code: Select all

   if key == " " then
and

Code: Select all

   txt:open('w')
   txt:write("hello text file!") 

Code: Select all

   text:open('w')
   text:write("hello text file!")
as you used text (not txt) as variable name at love.load()

2.
change 'text.txt' to 'text.lua'

3.
https://love2d.org/wiki/love.filesystem
love.filesystem can only read/write to that special directory. If you want to save at an other dir you have to use
http://www.lua.org/manual/5.1/manual.html#pdf-io.close
but I would not recommend it
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: Reading and writing

Post by Nixola »

Why should text.txt become text.lua?
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 2 guests