Problems with GPGS v3 snapshot saving

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:

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:

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:

> 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)

Maybe my code will be helpful

            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)
1 Like

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