filesystem.newFile????

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

Re: filesystem.newFile????

Post by Robin »

To save tables, simply use Ser.

Code: Select all

local serialize = require 'ser'

--saving code
love.filesystem.write('nameofsavegame', serialize(sometable))

--loading code
sometable = love.filesystem.load('nameofsavegame')()
Help us help you: attach a .love.
pielago
Party member
Posts: 142
Joined: Fri Jun 14, 2013 10:41 am

Re: filesystem.newFile????

Post by pielago »

ohh so what i did will only work for small stuff? or single parts of a table???
if so..
ill try "ser" and see if i can get it to work and save all my tables and load them...
hope it can save tables inside a table...

from what i see "ser" both will go under love.load()
am i correct or not???
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: filesystem.newFile????

Post by Robin »

I don't think I understand what you're saying, sorry.

You'd require Ser at the top of your main.lua or wherever you'd want to use it. The other two pieces of code you would place in the places you'd load or write a savegame. 'nameofsavegame' and sometable are just examples, you would use something different. (For example, let the user type the name of the game to save or load, or let them select it from a list.)

If the things you want to save are stored in multiple tables, you can do it like this:

Code: Select all

--saving code
love.filesystem.write('nameofsavegame', serialize({player, map, enemies})

--loading code
player, map, enemies = unpack(love.filesystem.load('nameofsavegame')())
Help us help you: attach a .love.
pielago
Party member
Posts: 142
Joined: Fri Jun 14, 2013 10:41 am

Re: filesystem.newFile????

Post by pielago »

well i did as you told me but i dont know why it wont save it ... can you help me??
Attachments
tables.love
(4.92 KiB) Downloaded 91 times
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: filesystem.newFile????

Post by Robin »

Ah, yes. I'm sorry, Ser currently doesn't do metatables, nor does any other serialisation library I know of.

You could do this:

Code: Select all

	--load filesystem
	if love.filesystem.exists("player") then
		p = setmetatable(love.filesystem.load('player')(), getmetatable(p))
	end
This replaces your "p" player object, but keeps the metatable.

Important: don't write out your game every frame. Better save on a keypress, for example:

Code: Select all

function love.keypressed(key)
	if key == 's' then
		--write data
		love.filesystem.write('player', serialize(p))
	end
end
Again, "p" instead of "player" (player == nil anyway).

Lastly, either place the line local serialize = require("ser") outside of love.load, or remove the word local. Otherwise love.keypressed can't use the serialisation function.
Help us help you: attach a .love.
pielago
Party member
Posts: 142
Joined: Fri Jun 14, 2013 10:41 am

Re: filesystem.newFile????

Post by pielago »

it worked thank you so now to add more tables will it be like this????

Code: Select all


    function love.keypressed(key)
       if key == 's' then
          --write data
          love.filesystem.write('player', serialize(p,m,s,t))--many metatables
       end
    end

User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: filesystem.newFile????

Post by Robin »

It would be a good idea for a new feature, somewhere in the future, but currently you'll have to pack them first:

Code: Select all

--writing
love.filesystem.write('player', serialize({p,m,s,t}))--note the curly braces 
The loading code is a bit more tricky, mostly because of metatables, I'm sad to say:

Code: Select all

--loading
p, m, s, t = unpack(love.filesystem.load('player')())
setmetatable(p, {__index = Player})
setmetatable(m, {__index = Map})-- I don't know what these are
setmetatable(s, {__index = Something}) -- it might be good to use more descriptive variable names
setmetatable(t, {__index = Tsomething}) -- so that other people or future you can understand what they're for
Help us help you: attach a .love.
pielago
Party member
Posts: 142
Joined: Fri Jun 14, 2013 10:41 am

Re: filesystem.newFile????

Post by pielago »

i see the write its the same

Code: Select all

love.filesystem.write('player', serialize({p,m,s,t}))--note the curly braces
but for the load I can tell its more to add
I can add my maps well that will do it, thank you so much

as long as I save my players variables will do it (hope)

Code: Select all


    --loading
    p, m, s, t = unpack(love.filesystem.load('player')())
    setmetatable(p, {__index = Player})
    setmetatable(m, {__index = Map})-- I don't know what these are
    setmetatable(s, {__index = Something}) -- it might be good to use more descriptive variable names
    setmetatable(t, {__index = Tsomething}) -- so that other people or future you can understand what they're for


Post Reply

Who is online

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