Hello! First time posting here. I just can't seem to figure this out myself. I already checked previous forum topics where people had a similar issue, although they're from 2014 so copying code from those topics didn't work. Looking up the documentation left me even more confused.
All I'm trying to do is save my game, and by extension load my game. I made a table called "data" in which I have all the variables that I want to be saved. My game will be a simple Tamagotchi clone of sorts, and I only have a few numbers and booleans that I want to save so far.
When I test my game, no errors appear, but I have a feeling that I did something wrong since in appData there's no LOVE folder, and one isn't creating itself no matter how much I try. I did use setIdentity but that also seems to do nothing.
Both filesystem.write and setIdentity are in love.load. I doubt it's a bug, then what am I doing wrong?
(English isn't my native language, and I'm a total begginner at programming, excuse my lack of knowledge )
[Solved] Problem with love.filesystem.write and read
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
[Solved] Problem with love.filesystem.write and read
Last edited by Natmaz on Mon Apr 23, 2018 6:02 pm, edited 1 time in total.
-
- Party member
- Posts: 544
- Joined: Wed Oct 05, 2016 11:53 am
Re: Problem with love.filesystem.write and read
Can you show your love.load in question?
Re: Problem with love.filesystem.write and read
I apologize if it's a bit messy and lenghty
Code: Select all
function love.load()
love.filesystem.setIdentity('Plumgotchi')
--[[
function loadGame()
toload = love.filesystem.load("save.txt")
toload()
end
--]]
love.window.setMode(500, 500, {}) -- Window size.
love.window.setTitle('Plumgotchi') -- Window title.
gotchiWindow.gay = love.graphics.newImage('Menus/Screen.png')
data = {
species = 'Common Egg',
stage = 'Egg',
gender = 'Egg',
points = 0,
money = 0,
hunger = 4,
mood = 4,
eggPresent = true
}
function saveGame()
-- local save = data
-- love.filesystem.write("save.txt",save)
success, message = love.filesystem.write(name, data, all)
end
eggTimer = 0
timer = 0
timerS = 0
menu = 1
hungryTimer = 0
happyTimer = 0
debug = true
gotchiVisible = true
statScreen = 1
subMenu = false
RNG = 0
Menu1 = false
Menu2 = false
Menu3 = false
Menu4 = false
Menu5 = false
Menu6 = false
Menu7 = false
Menu8 = false
Menu9 = false
Menu10 = false
outerButtons = {}
egg = {}
child = {}
teenager = {}
adult = {}
circle = {}
heart = {}
star = {}
emptyStats = {}
eggmax = love.filesystem.getDirectoryItems('Egg')
childmax = love.filesystem.getDirectoryItems('Child')
teenmax = love.filesystem.getDirectoryItems('Teenager')
adultmax = love.filesystem.getDirectoryItems('Adult')
-- Loading images in a batch.
-- outerButtons.
for i = 1,10 do
outerButtons[i] = love.graphics.newImage('Menus/outer buttons/'..i..'.png')
end
-- circle
for i = 1,4 do
circle[i] = love.graphics.newImage('Menus/on the screen/Hunger '..i..'.png')
end
-- heart
for i = 1,4 do
heart[i] = love.graphics.newImage('Menus/on the screen/Happy '..i..'.png')
end
-- star
for i = 1,4 do
star[i] = love.graphics.newImage('Menus/on the screen/Health '..i..'.png')
end
-- emptyStats
for i = 1,3 do
emptyStats[i] = love.graphics.newImage('Menus/on the screen/Empty stats '..i..'.png')
end
-- egg
for i = 1,#eggmax do
egg[i] = love.graphics.newImage('Egg/Egg '..i..'.png')
end
-- child
for i = 1,#childmax do
child[i] = love.graphics.newImage('Child/Child '..i..'.png')
end
-- teen
for i = 1,#teenmax do
teenager[i] = love.graphics.newImage('Teenager/Teen '..i..'.png')
end
-- adult
for i = 1,#adultmax do
adult[i] = love.graphics.newImage('Adult/Adult '..i..'.png')
end
-- Loading sounds
confirm = love.audio.newSource("Sfx/Select.wav", "static") -- Static for sfx, stream for music.
moveCursor = love.audio.newSource("Sfx/Move Cursor.wav", "static")
cancel = love.audio.newSource("Sfx/Cancel.wav", "static")
end
- zorg
- Party member
- Posts: 3465
- Joined: Thu Dec 13, 2012 2:55 pm
- Location: Absurdistan, Hungary
- Contact:
Re: Problem with love.filesystem.write and read
Reading the wiki's article about love.filesystem.write tells you this: data can either be a string, or a löve object; you're trying to save a table, which is basically a number.
If you don't want to use any fancy serialization libraries, then you could tiptoe the issue the following way:
This basically allows you to save a table, if it only has strings, numbers, and booleans, as either keys or values, to a file, and then you would use love.filesystem.load to load the data back as lua code, and call the chunk that function returned to get back the original values in a table.
If you don't want to use any fancy serialization libraries, then you could tiptoe the issue the following way:
Code: Select all
function serialize(t)
local s = {"return {"}
for k,v in pairs(t) do
table.insert(s, "[")
if type(k) == "string" then table.insert(s, "'") end
table.insert(s, tostring(k))
if type(k) == "string" then table.insert(s, "'") end
table.insert(s, "]")
table.insert(s, " = ")
if type(v) == "string" then table.insert(s, "'") end
table.insert(s, tostring(v))
if type(v) == "string" then table.insert(s, "'") end
table.insert(s, ", ")
end
table.insert(s, "}")
return table.concat(s)
end
Me and my stuff True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
Re: Problem with love.filesystem.write and read
Hmm, do I have to change anything in the function you provided? Since inserting it doesn't seem to do anything, but maybe I just don't know how to check if it works. I'm new to programming and Lua, so even with your explanation (which I appreciate a whole lot) I don't seem to know what I'm supposed to do with it.
-
- Party member
- Posts: 544
- Joined: Wed Oct 05, 2016 11:53 am
Re: Problem with love.filesystem.write and read
Example usage of zorg's code (just put the serialize function somewhere where it's accessible in scope, like at the top of main.lua):
Do note that zorg's example function is a very basic serializer, so it won't cover all use-cases. To get around that you could, for instance, use bitser. Its usage would be something along the lines of:
But as he said, if you don't need anything fancy with the tables you want to save, bitser isn't really necessary.
Code: Select all
local myTable = {
myValue = 1,
mySecondValue = 3,
myThirdValue = 4
}
-- Serialize the table into a string with the function
local myString = serialize(myTable) -- myString is now "return {['mySecondValue'] = 3, ['myValue'] = 1, ['myThirdValue'] = 4, }"
-- Write myString into a file
love.filesystem.write( "myTestFile.txt", myString )
-- Load the file, because of the way the serialize() function formats the string it is
-- now a function that returns the original table's contents when called.
-- This is why we add the extra parentheses to the end
local loadedValues = love.filesystem.load ( "myTestFile.txt" ) ()
print(loadedValues.myValue) -- prints 1
Code: Select all
-- save
serializedString = bitser.dumps(tableToSave)
love.filesystem.write("myTestFile.txt", serializedString)
-- load
love.filesystem.load ("myTestFile.txt" )
someValue = bitser.loads(serializedString)
Re: Problem with love.filesystem.write and read
Holy heck, bitser is super easy to use and it worked for me perfectly!
Thank you very much!
Thank you very much!
Who is online
Users browsing this forum: Ahrefs [Bot], Bing [Bot], Google [Bot] and 4 guests