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

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
baditchyscrot
Prole
Posts: 1
Joined: Thu Mar 14, 2024 4:39 pm

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

Post 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
User avatar
marclurr
Party member
Posts: 105
Joined: Fri Apr 22, 2022 9:25 am

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

Post 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)
User avatar
marclurr
Party member
Posts: 105
Joined: Fri Apr 22, 2022 9:25 am

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

Post 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.
User avatar
zorg
Party member
Posts: 3444
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

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

Post 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.
Me and my stuff :3True 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.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot], keharriso and 58 guests