# Problems with GPGS v3 snapshot saving

**URL:** <https://forums.solar2d.com/t/problems-with-gpgs-v3-snapshot-saving/357296>\
**Category:** Games\
**Created:** [April 9, 2025, 11:38pm UTC](https://forums.solar2d.com/t/problems-with-gpgs-v3-snapshot-saving/357296 "2025-04-09T23:38:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![cmaiia](https://forums.solar2d.com/user_avatar/forums.solar2d.com/cmaiia/32/4314_2.png) [@cmaiia](https://forums.solar2d.com/u/cmaiia)\
**Post date:** [April 9, 2025, 11:38pm UTC](https://forums.solar2d.com/t/problems-with-gpgs-v3-snapshot-saving/357296/1 "2025-04-09T23:38:13Z")

</div>

Hello everyone, I’m trying to use the gpgs plugin.

I started using version v2 of it, but it didn’t work and the build gave me an error. So I switched to version 3.

The code below is what I’m using. It works to some extent. I can see if someone is already logged in and log in with the gpgs service. But I can’t save snapshots or load them.

Please help me if you know how to fix this.

my build.settings configuration:  
on the android part:

```auto
usesPermissions =
 {
 "com.google.android.gms.permission.AD_ID",
 "android.permission.GET_ACCOUNTS",
 "com.android.vending.BILLING",
 "android.permission.INTERNET",
 "android.permission.ACCESS_NETWORK_STATE",
 "android.permission.WRITE_EXTERNAL_STORAGE",
 "android.permission.READ_EXTERNAL_STORAGE"
 },

```

in the plugins part:

```auto
plugins =
 {
 ["plugin.admob"] = {publisherId = "com.coronalabs" },
 ["plugin.google.iap.billing.v2"]={ publisherId="com.solar2d"},
["CoronaProvider.native.popup.social"]={publisherId="com.coronalabs" },
["plugin.gpgs.v3"] = {publisherId = "com.solar2d"}

},

```

code configuration:

```auto
> local function carregarJogo()
> -- Listener para o carregamento
> local function loadListener(event)
> if event.isError then
> consoleLog.text = "Erro ao carregar: " .. event.errorMessage
> elseif not event.snapshot then
> consoleLog.text = "Load: Nenhum Snapshot encontrado."
> else
> local snapshot = event.snapshot
> local dadosString = snapshot.contents:read()
> if dadosString then
> local dadosJogo = json.decode(dadosString)
> consoleLog.text = "Nível " .. dadosJogo.nivel .. ", Pontuação " .. dadosJogo.pontuacao
> return dadosJogo
> else
> consoleLog.text = "Load: Snapshot vazio."
> end
> end
> end
>         
> consoleLog.text = "Carregando Snapshot..." -- Depuração
> gpgs.snapshots.open({
> filename = "saveGame",
> create = false,
> listener = loadListener
> })
> end
> 
> local function salvarJogo(dadosJogo)
>         
> local dadosString = json.encode(dadosJogo)
> if type(dadosString) ~= "string" then
> consoleLog.text = "Erro: dadosString não é uma string válida!"
> return
> end
> 
> local function saveListener(event)
> if event.isError then
> consoleLog.text = "Erro ao salvar: " .. event.errorMessage
> else
> consoleLog.text = "Jogo salvo com sucesso!"
> timer.performWithDelay(3000, function()
> carregarJogo()
> end)
> end
> end
>         
> consoleLog.text = "Abrindo Snapshot para salvar..."
> gpgs.snapshots.open({
> filename = "saveGame",
> create = true,
> listener = function(event)
> if event.isError then
> consoleLog.text = "Erro ao abrir Snapshot: " .. event.errorMessage
> elseif not event.snapshot then
> consoleLog.text = "Erro: Snapshot não retornado!"
> else
> local snapshot = event.snapshot
> consoleLog.text = "Snapshot aberto. Escrevendo dados..."
> local success = event.snapshot.contents:write('{"nivel": 1, "pontuacao": 100}')
> if success then
> consoleLog.text = "Dados escritos. Salvando Snapshot..."
> gpgs.snapshots.save({
> snapshot = snapshot,
> description = "Progresso do jogador",
> listener = saveListener
> })
> else
> consoleLog.text = "Falha ao escrever dados no Snapshot!"
> end
> end
> end
> })
> end
> -- Função para verificar progresso salvo
> local function verificarProgressoSalvo()
> gpgs.snapshots.open({
> filename = "saveGame",
> create = false,
> listener = function(event)
> if event.isError then
> consoleLog.text = "Erro ao verificar Snapshot:\n " .. event.errorMessage
> timer.performWithDelay(3000, function()
> salvarJogo(playerDados)
> end)
>                     
> else
> consoleLog.text = "Progresso salvo encontrado!"
> end
> end
> })
> end
> 
> -- Função para tentar login com retry
> local function tentarLogin()
> -- Verifica se já está autenticado
> if gpgs.isAuthenticated() then
> consoleLog.text = "Jogador já autenticado!"
> verificarProgressoSalvo()
> return
> end
> 
> -- Verifica conexão com listener (explorando a funcionalidade)
> if gpgs.isConnected() then
> consoleLog.text = "Já conectado ao GPGS!"
> verificarProgressoSalvo()
> return
> end
> 
> -- Se não estiver conectado, tenta login
> tentativas = tentativas + 1
> if tentativas <= maxTentativas then
> consoleLog.text = "Tentativa " .. tentativas .. " de " .. maxTentativas .. ": Conectando ao GPGS..."
> gpgs.login({
> userInitiated = true,
> useDrive = true,
> listener = function(event)
> if event.isError then
> consoleLog.text = "Falha na tentativa " .. tentativas .. ": " .. event.errorMessage
> print("Erro detalhado: " .. event.errorMessage)
> if tentativas < maxTentativas then
> timer.performWithDelay(intervalo, tentarLogin)
> else
> consoleLog.text = "Falha após " .. maxTentativas .. " tentativas."
> end
> else
> consoleLog.text = "Logado com sucesso na tentativa " .. tentativas .. "!"
> verificarProgressoSalvo()
> end
> end
> })
> end
> end
> 
> -- Inicia o processo de login
> timer.performWithDelay(5000, tentarLogin)

```

---

<div class="post-metadata">

**Author:** ![vlad.yasni](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/v/34f0e0/32.png) [@vlad.yasni](https://forums.solar2d.com/u/vlad.yasni)\
**Post date:** [April 10, 2025, 6:36am UTC](https://forums.solar2d.com/t/problems-with-gpgs-v3-snapshot-saving/357296/2 "2025-04-10T06:36:11Z")

</div>

Maybe my code will be helpful

```auto
            server.readFile = function(p)
               local fileName = p.fileName
               local onComplete = p.onComplete or function() end

               local function docListener( event )
                   
                   processEventTable(event, "Server gpgs readFile")
                   
                    if not event.isError and event.snapshotId then
                        local snapshot = server._gpgs.snapshots.getSnapshot(event.snapshotId)
                        
                        if snapshot then
                            local contents = snapshot.contents.read()

                            onComplete({fileName = fileName, contents = contents})
                        else
                            onComplete({isError = true})
                        end
                    else

                        onComplete({isError = true})
                    end
                end 

                server._gpgs.snapshots.open({
                        filename = fileName,
                        create = false,
                        listener = docListener
                    })

            end

            server.saveFile = function(p)
                local fileName = p.fileName
                local contents = p.contents
                local onComplete = p.onComplete

                local function docListener( event )
                    
                    if not event.isError and event.snapshotId then
                        
                        local snapshot = server._gpgs.snapshots.getSnapshot(event.snapshotId)
                        
                        if snapshot.contents.write(contents) then
                            server._gpgs.snapshots.save({
                                snapshotId = event.snapshotId,
                                listener = function(event) 
                                    
                                    processEventTable(event, "Server gpgs writeFile")
                                    
                                    onComplete({isError = event.isError})
                                end
                            })
                        else
                            onComplete({isError = true})
                        end
                    else
                        onComplete({isError = true})
                    end
                end 

                if fileName and contents and server._gpgs.isLoggedIn == true then

                    server._gpgs.snapshots.open({
                        filename = server._defaultSavedFileName,
                        create = true,
                        listener = docListener
                    })
                else
                    onComplete({isError = true})
                end

            end

            server._gpgs = require( "plugin.gpgs.v3" )
            server._gpgs.enableDebug(DebugService)
            
            server._gpgs.init()
            
            server._gpgs.isConnected(function(event) 
                
                if event.isConnected then

                    server._gpgs.login({
                        useDrive = true,
                        userInitiated = not event.isConnected,
                        listener = function(event)

                            processEventTable(event, "Server gpgs login")

                            if event.phase == "logged in" then
                                
                                server._gpgs.isLoggedIn = true
                                
                                if serverSaved == 0 then
                                    initComplete({isError = false})
                                else
                                    server.readFile({
                                        fileName = server._defaultSavedFileName, 
                                        onComplete = function(p) 

                                            initComplete({fileName = p.fileName, contents = p.contents})         

                                    end})
                                end
                            else --event.isError then  
                                initComplete({isError = true})   
                            end

                        end
                    })
                   
                end
            end)

```

---

<div class="post-metadata">

**Author:** ![cmaiia](https://forums.solar2d.com/user_avatar/forums.solar2d.com/cmaiia/32/4314_2.png) [@cmaiia](https://forums.solar2d.com/u/cmaiia)\
**Post date:** [April 16, 2025, 10:21am UTC](https://forums.solar2d.com/t/problems-with-gpgs-v3-snapshot-saving/357296/3 "2025-04-16T10:21:48Z")

</div>

> [@vlad.yasni](#):
>
> `initComplete`

Sorry for the delay in responding!

Thanks for the code, but I tried to use it and it’s missing information, such as “initComplete”?? I know it’s a function but I don’t know what it’s all about.

Can you make it available so I can test it? Please

---

<div class="post-metadata">

**Author:** ![system](https://forums.solar2d.com/uploads/default/original/1X/1bf6a947166df24e92aaa3358558f4b82d7f3d5a.png) [@system](https://forums.solar2d.com/u/system)\
**Post date:** [October 13, 2025, 10:21am UTC](https://forums.solar2d.com/t/problems-with-gpgs-v3-snapshot-saving/357296/4 "2025-10-13T10:21:51Z")

</div>

Closed on October 13, 2025, 10:21am UTC

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
