saveGlobalData question

Good evening everyone,

I’m facing an issue I couldn’t find anything about on the forum. As usual I’m pretty sure I’m doing something wrong, somewhere.
I need to locally store a huge string with saveGlobalData. The code I wrote seems working fine, but whatever is displayed while it saves, it freezes. Once the storing operation has completed then it turns running fine at 60fps. In this case, I made a simple rotating loader. but it freezes either way

I admit it : it is not a big issue that the screen freezes for a bunch of seconds, but since I’m learning I’m also willing to know if there’s anything wrong I am doing.

I’m posting here the code, even though i think it may be pointless as it does not contains the table self.level1 content that makes it freezing ( as it is filled in at run time while the first level is being played).

However, is there anything you notice that could be wrong in the first place?

Sorry for the long post and thank you for your valuable time.
Regards



function setup()
gt = GhostTracker()
at = Atom(WIDTH/2,HEIGHT/2)
at:Show()
end


function draw()
    background(40, 40, 50)
    at:draw()

end

Atom = class()
local Atom_State = 1
local Intro_Timer = 0
Intro_Status = 0
local Atom_Incremental = 0
local Angle = 0
local Angle_Incremental = 0
local Check_Step = 0
local Intro_Cantouch = 0
function Atom:init(x,y)
    self.x = x
    self.y = y
end

function Atom:draw()
if Atom_State == 0 then
    Atom_Incremental = Atom_Incremental + 1
        
    if Atom_Incremental == 300 then
        self:Show()
        gt:SaveTracker()
        self:Hide()
    end
    
        spritePos = vec2( WIDTH-100, 50 )
        pushMatrix()
        translate(spritePos.x, spritePos.y)
            if Angle_Incremental < 3 then
                Angle_Incremental = Angle_Incremental + 0.01
            end
        Angle = Angle + 1 + Angle_Incremental
        if Angle >= 360 then
            Angle = 0
        end
        rotate(Angle)
        sprite("Planet Cute:Tree Short", 0, 0,60)

        popMatrix()        
        pushStyle()
        fill(255,255,255,255)
        text("Saving level information",spritePos.x,spritePos.y+50)
        popStyle()
end
end

function Atom:Show()
    Atom_State = 0
end
function Atom:Hide()
    Atom_State = 1
end

GhostTracker = class()
local TextPosLoop = 0
local SaveDone = 0
local postcontent = ""
local cnt = 0
function GhostTracker:init()
    self.level1 = {}
    self.level2 = {}
    
end

function GhostTracker:draw(x,y,b,f)
    table.insert(self.level1,GhostProperties(x,y,b,f))
end

function GhostTracker:SaveTracker()

    
    if SaveDone == 0 then
        for i,v in ipairs(self.level1) do
            cnt = cnt + 1
            postcontent = postcontent..v.x..";"..v.y..";"..v.b..";"..v.f.."|" 
        end

        saveGlobalData("lv1",postcontent)
    end
    tint(255,255,255,255)
    text(cnt,WIDTH/2,HEIGHT/2)
    SaveDone = 1
    
end



GhostProperties = class()

function GhostProperties:init(x,y,b,f)
    self.x = x -- posX
    self.y = y -- posY
    self.b = b -- shootbullet?
    self.f = f -- frame
end






it sounds as though the saving operation is taking a while. How big is your string? Does it really need to be so big? Can you split it into several strings and spread the saving over several draw operations?

Hi @Ignatz, thanks for your reply. As I asked time ago here in the forum, I was on the need to record each, and every player movement while playing a certaing level. And the code I wrote seems doing it quite fine fine, exept for the freezing thing whilst saving. Once the table is filled with the string, I use it afterwards to read other player movements to display a ghost player on the screen. So to reply your question, yes the string happens to be quite huge as it keeps players coordinates on the screen in the form of a csv. If I split it I have to put my hands on the read mechanism that currently runs flawlessly :frowning:
But I believe you’re right ( as usual )

The quick solution I thought : saving without any animation running in the background :slight_smile:
thanks
regards

ps. Today I’ll be trying to get a sample of the first level csv to post it here.

@deactive - why don’t you save the string in pieces? For example, you could add to the string normally, and when it reaches a certain size, eg 5,000 characters, you save the first 5,000 characters, and remove them from the string. You could use the same name for all the saved pieces, with a number on the end, eg Save001, Save002, …

This means your string stays small, and saving it is quick. When you need to read the string back into memory, you can read in one piece at a time in sequence, and as you get to the end, you can read the next piece. This should make reading it back quicker as well.

:-?

why didn’t I think of this before? I’m deffo going to give this a try today.
I expect several but shorter freezes while saving.

thanks, @Ignatz

@deactive Have you thought about using a file. You can append each move to the file which wouldn’t take any time at all. No need to keep track of string size or multiple names.