Page 1 of 1

[Solved] Save\Load without using libraries

Posted: Tue Jun 04, 2013 2:52 pm
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?

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

Posted: Tue Jun 04, 2013 2:57 pm
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.

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

Posted: Tue Jun 04, 2013 3:10 pm
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.

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

Posted: Tue Jun 04, 2013 3:26 pm
by bartbes
You should actually open the file. In any case, it's because read returns a string.

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

Posted: Tue Jun 04, 2013 3:34 pm
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?

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

Posted: Tue Jun 04, 2013 3:43 pm
by MadByte
yesterday there was a similar thread about that.
Here

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

Posted: Tue Jun 04, 2013 4:57 pm
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.

Re: [Solved] Save\Load without using libraries

Posted: Tue Jun 04, 2013 5:34 pm
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 :>