[Solved] Save\Load without using libraries

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
bramblez
Citizen
Posts: 50
Joined: Sun Jul 29, 2012 1:21 pm
Location: Russia, Moscow

[Solved] Save\Load without using libraries

Post by bramblez »

Hello, dear lovers :>

I've read few posts about load/save data, but I don't want to use any libraries. I have a .txt file with only 1 number and I am using filesystem to get that number for using in a table.

Code: Select all

file= love.filesystem.exists("1.txt")
	if not file == true then
		print("made 1.txt")

		data = love.filesystem.newFile("1.txt")
		data :open('w')
		data :write(1)
		data :close()
	end

	data = love.filesystem.newFile("1.txt")
	number = data :read()
	print(number) -- that works and prints '1' in console
	print(modes[number].width) -- that thing does not work, altho if you replace number with just a number '1' - it does work and prints other values from other table
is there a way to solve this somehow?
Last edited by bramblez on Tue Jun 04, 2013 5:34 pm, edited 1 time in total.
User avatar
Plu
Inner party member
Posts: 722
Joined: Fri Mar 15, 2013 9:36 pm

Re: [Not Solved] Save\Load without using libraries

Post by Plu »

The example in the documentation says this:

Code: Select all

file = love.filesystem.newFile("data.txt")
file:open('r')
data = file:read()
The : open('r') call might be needed?

Although since it prints 1 you'd expect it to work as you posted it. Strange that it does not.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: [Not Solved] Save\Load without using libraries

Post by Robin »

What time is it? Unsolicited code advice time!

Replace if not file == true then by if not file then, because the latter is much clearer.
Help us help you: attach a .love.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: [Not Solved] Save\Load without using libraries

Post by bartbes »

You should actually open the file. In any case, it's because read returns a string.
User avatar
bramblez
Citizen
Posts: 50
Joined: Sun Jul 29, 2012 1:21 pm
Location: Russia, Moscow

Re: [Not Solved] Save\Load without using libraries

Post by bramblez »

Robin wrote:What time is it? Unsolicited code advice time!

Replace if not file == true then by if not file then, because the latter is much clearer.
Well thank you, kindly :>
bartbes wrote:You should actually open the file. In any case, it's because read returns a string.
I've tried

Code: Select all

	   data = love.filesystem.newFile("1.txt")
   number = data :open('r') --also tried open('w')
   print(number) -- that works and prints '1' in console
   print(modes[number].width) -- does not work
but no luck. Is there a way to get a value from this file not as a string?
User avatar
MadByte
Party member
Posts: 533
Joined: Fri May 03, 2013 6:42 pm
Location: Braunschweig, Germany

Re: [Not Solved] Save\Load without using libraries

Post by MadByte »

yesterday there was a similar thread about that.
Here
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: [Not Solved] Save\Load without using libraries

Post by bartbes »

bramblez wrote:but no luck. Is there a way to get a value from this file not as a string?
Well, you changed nothing, try using number = tonumber(file:read()).
And yes, it should be file:read, after file:open.
User avatar
bramblez
Citizen
Posts: 50
Joined: Sun Jul 29, 2012 1:21 pm
Location: Russia, Moscow

Re: [Solved] Save\Load without using libraries

Post by bramblez »

bartbes wrote:
bramblez wrote:but no luck. Is there a way to get a value from this file not as a string?
Well, you changed nothing, try using number = tonumber(file:read()).
And yes, it should be file:read, after file:open.
that's hot.

thank you very much :>
Post Reply

Who is online

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