Page 1 of 1

love.data.pack() no idea how to work it

Posted: Thu Mar 14, 2024 4:47 pm
by baditchyscrot
Hi,

So I'm trying to get my head around the love.data.pack(), love.filesystem.write(), and love.data.unpack().

I'm trying to get a very simple thing going, so I can expand upon it. Basically I was to save a score. So score = 10. It works fine when I just use love.filesystem.write() and save it to "data.txt". But I'm trying to wrap my head around serialization and everything, and I want to use the pack unpack. But anything I try give me an error.

So could someone please expand upon this.

function love.load()
score = 0
end

function love.update(dt)
score = 10
saveData(score)
end

function saveData(scoreValue)
packedData = love.data.pack("string","integer",scoreValue)
love.filesystem.write("data.txt",packedData)
end

Re: love.data.pack() no idea how to work it

Posted: Thu Mar 14, 2024 8:52 pm
by marclurr
This example stores 2 numbers serialised using pack in a file and reads them back with unpack.

Code: Select all

love.filesystem.setIdentity("pack_test")

local fmt = "II"

love.filesystem.write("highscore.bin", love.data.pack("data", fmt, 123, 456))

local readData = love.filesystem.read("data", "highscore.bin")

local score1, score2 = love.data.unpack(fmt, readData)

print(score1, score2)

Re: love.data.pack() no idea how to work it

Posted: Thu Mar 14, 2024 9:06 pm
by marclurr
Out of curiosity I wanted to try writing a table of numbers using this method. It's a little bit awkward but if you really want to store a variable amount of data in a binary format this might work fine:

Code: Select all

love.filesystem.setIdentity("pack_test")

local things = {10, 9, 8, 7}



love.filesystem.write("highscore.bin", love.data.pack("data", "I", #things))
for i = 1, #things do 
    love.filesystem.append("highscore.bin", love.data.pack("data", "I", things[i]))
end

local readData = love.filesystem.read("data", "highscore.bin")

local count, pos = love.data.unpack("I", readData)

for i = 1, count do
    local v, p = love.data.unpack("I", readData, pos)
    print(v)
    pos = p 
end

You could remove the need for a loop by generating the format string based on the size of the table, you'd still need to store the count along with the elements so you can build the same format string when reading.

Re: love.data.pack() no idea how to work it

Posted: Sun Mar 17, 2024 7:03 am
by zorg
You wrote "integer" for love.data.pack's second parameter, which means you either didn't read, or did not understand what that expects.

The relevant wiki page links to the lua equivalent's list of accepted parameters, where you can see that there are multiple ways to pack numbers;
As was shown above, one solution is to write "I" for packing a single integer (although i would recommend a different one that's not dependent on native sizes, or explicitly give it a size like "I4" for storing a number between about -2 million and 2 million.
Also, before you accuse me here too for being mean, i do have an obligation to have others visiting the forums find an answer to questions asked, which is the only reason i bothered to give an answer in the first place.