Where to save savedgames on Android?

Jimmy

New member
3rd Party Module Dev
Tutorial Author
Joined
Jan 2, 2020
Messages
569
Where are you supposed to save savedgames on Android to make sure it will survive an storeupdate?
 

Phil7

Administrator
CX Code Contributor
3rd Party Tool Dev
Joined
Jun 26, 2017
Messages
535
What do you mean by storeupdate? Did you try just using SaveState()? What are the issues you ran into?
 

Jimmy

New member
3rd Party Module Dev
Tutorial Author
Joined
Jan 2, 2020
Messages
569
I have to admit that the saving-file thing is a total confusion for me right now. I havn't tried SavedState that's a brand new instruction to me. I'm currently saving all files using databuffer into the datafolder. I think. Does it need to be saved outside the datafolder or what are you supposed to do? It's just game data under 10MB so it's not anything massive.
 

Phil7

Administrator
CX Code Contributor
3rd Party Tool Dev
Joined
Jun 26, 2017
Messages
535
Well, IFAIK this is the multiplatform way of CX for all generated data that need to be saved. I used it to track user input and let the game react accordingly. It has been some time, but I think on android these data are kept even if you update your game, given that you don't change the identifying stuff.
On Windows I had some issues with an installer (InnoSetup) to make it writeable and keep the data, if you install an update over it, but it worked fine the last time I tried.

Generally I was using JSON to structure the data and have it readable while decently packed.
 

MikeHart

Administrator
Joined
Jun 19, 2017
Messages
2,812
@jimmyon imho on Android AND ios, you can't write into the data folder. You can only read from there.
 

MikeHart

Administrator
Joined
Jun 19, 2017
Messages
2,812
When i find time, i will outline where android saves the content when you target the internal folder. Have to dig into the sources for this.
Btw, didn't you had this figured out by writing to the SD card?
 

Jimmy

New member
3rd Party Module Dev
Tutorial Author
Joined
Jan 2, 2020
Messages
569
Yes exactly saving to external card is no problem. You just have to manually go to Settings/Apps/CerberusGame and allow fileaccess. I think I can write JAVA runtime asking routine to handle that correctly.

The external I can decide exactly where to save, here I save it in my camera folder and load it back.

Also it works using "internal" but I can't find it right now where that goes right now.


code_language.cerberus:
Strict
Import mojo2
Import brl
Import brl.FileSystem

Function Main:Int()
    New myClass
    Return 0
End

Class myClass Extends App

    Field so:String[], canvas:Canvas, bg:Image
   
    Method OnCreate:Int()
        SetUpdateRate 0
        canvas = New Canvas
       
        ' Write file
        Local s:String
        s = "brian~nJohn~nPeter"
         Local fw:= FileStream.Open("cerberus://internal/test2.txt","w")
        'Local fw:= FileStream.Open("cerberus://external/DCIM/Camera/test4.txt","w")
        If fw <> Null
            Print "File open to write"
            fw.WriteString(s)
            fw.Close()
        Else
            Print "File not open to write"
        Endif
       
        ' Read file
        Local fr := FileStream.Open("cerberus://internal/test2.txt","r")
        'Local fr := FileStream.Open("cerberus://external/DCIM/Camera/test4.txt","r")
        If fr <> Null
            Print "File open to read"
            Local s2:String = fr.ReadString()
            fr.Close()
            Print ("------------------")
            Print ("s2="+s2)
            Print ("------------------")
            so = s2.Split("~n")
            Print ("Count="+so.Length())
        Else
            Print "File not open to read"
        Endif
       
        Return 0
    End

    Method OnUpdate:Int()
        Return 0
    End

    Method OnRender:Int()
        canvas.Clear 0,0,0.5
        canvas.SetColor 0.5,0.5,0.5
        canvas.PushMatrix
        canvas.Translate DeviceWidth()/2,DeviceHeight()/2
        canvas.Scale 2.0,2.0
        canvas.DrawText( String("Cerberus X~nHello World!").Split("~n")),DeviceWidth()/2,DeviceHeight()/2,0.5,0.5
        For Local y:Int = 1 To so.Length()
            canvas.DrawText so[y-1], 0, 0+y*20, .5, .5
        Next
        canvas.PopMatrix
        canvas.Flush
        Return 0
    End
   
End
 

Jimmy

New member
3rd Party Module Dev
Tutorial Author
Joined
Jan 2, 2020
Messages
569
Here I successfully could make a DIR to list all internal created files (it seem to repeat though)

code_language.cerberus:
Strict
Import mojo2
Import brl
Import brl.FileSystem

Function Main:Int()
    New myClass
    Return 0
End

Class myClass Extends App

    Field so:String[], canvas:Canvas, bg:Image
    
    Method OnCreate:Int()
        SetUpdateRate 0
        canvas = New Canvas
        
        ' Write file
        Local s:String
        s = "brian~nJohn~nPeter"
        Local fw:= FileStream.Open("cerberus://internal/test2.txt","w") 
        'Local fw:= FileStream.Open("cerberus://external/DCIM/Camera/test4.txt","w")
      
   If fw <> Null
            Print "File open to write"
            fw.WriteString(s)
            fw.Close()
        Else
            Print "File not open to write" 
        Endif
        
        ' Read file
        Local fr := FileStream.Open("cerberus://internal/test2.txt","r")
        'Local fr := FileStream.Open("cerberus://external/DCIM/Camera/test4.txt","r")
      
   If fr <> Null
            Print "File open to read"
            Local s2:String = fr.ReadString()
            fr.Close()
            Print ("------------------")
            Print ("s2="+s2)
            Print ("------------------")
            so = s2.Split("~n")
            Print ("Count="+so.Length())
        Else
            Print "File not open to read" 
        Endif
        
        Return 0
    End

    Method OnUpdate:Int()
    
    
       If MouseDown(0)
        Local y:=0
        For Local f:=Eachin LoadDir( "cerberus://internal/",True )
            Local p:=""+f
            Local nm:=(f+"                    ")[..20]
            Local ty:=""
            If FileType( p )=FILETYPE_FILE
                ty=FileSize( p )
            Else
                ty="(dir)"
            Endif
        Print nm+ty
        Next
        
        Endif
        
        Return 0
    End

    Method OnRender:Int()
        canvas.Clear 0,0,0.5
        canvas.SetColor 0.5,0.5,0.5
        canvas.PushMatrix
        canvas.Translate DeviceWidth()/2,DeviceHeight()/2
        canvas.Scale 2.0,2.0
        canvas.DrawText( String("Cerberus X~nHello World!").Split("~n")),DeviceWidth()/2,DeviceHeight()/2,0.5,0.5
        For Local y:Int = 1 To so.Length()
            canvas.DrawText so[y-1], 0, 0+y*20, .5, .5
        Next
        canvas.PopMatrix
        canvas.Flush
        Return 0
    End
    
End
 

Jimmy

New member
3rd Party Module Dev
Tutorial Author
Joined
Jan 2, 2020
Messages
569
Some progress :

* I got Load/Save-state working and it seems to work on all platforms desktop including android even html. But I'm not sure what I can save and load its more a snapshot of some global state and not files? It seems a bit weird but handy to have.

* Load/Save external (works and I know where it is but I don't think it's good practice to use in such situations).

* Load/Save Internal works equally I can read and write data and all is well, but I havn't found where the actual data hides.
I've tried to find an app folder inside my phones' Android/data folder but it's not there.

About the data folder I still can't find the .data folder on android but on desktop I think I can cd to that dir but I havn't had the time to test if its okay to do it even on desktop. Will try this after Christmas.
 

Jimmy

New member
3rd Party Module Dev
Tutorial Author
Joined
Jan 2, 2020
Messages
569
I can for sure read from the .data folder and write to local and keep reading from local instead IF it exist there (even if i'm a bit bothered about not being able to see the folder) but I'm sure there's a reason for this as I seen al lot of people have the same "problem" with their games developed with Android Studio.
 
Top Bottom