About Monkey 2 › Forums › Monkey 2 Programming Help › Load/save persistent data on mobile targets?
Tagged: Android, filesystem
This topic contains 14 replies, has 7 voices, and was last updated by
abakobo
1 year, 6 months ago.
-
AuthorPosts
-
September 26, 2017 at 1:49 pm #10822
Hi all,
Is there an equivalent to the old LoadState() and SaveState() for mobile targets? These should load and save strings that are persistent between installations of the app, used to e.g. store high scores and preferences between reinstalls. I can’t see anything in the docs but probably missed it…
Thanks!
September 27, 2017 at 3:33 am #10824Try to use that:
Monkey123456789101112Local dir:=InternalDir()+"mydata/"CreateDir( dir )Local file:=dir+"values.txt"SaveString( "hp=100~ndmg=13.5",file )Local s:=LoadString( file )If Not s Alert( "Can't read file! ") ; Return' ~n is \nLocal values:=s.Split( "~n" )For Local v:=Eachin valuesAlert( "pair: "+v )NextSeptember 27, 2017 at 5:26 am #10826where are you getting InternalDir() from?
September 27, 2017 at 5:49 am #10827where are you getting InternalDir() from?
std.filesystem namespace.
Monkey12345678910111213141516171819202122232425262728293031#rem monkeydoc Gets the filesystem directory of the app's internal storage directory.Note that only the mobile targets have an internal directory. Other targets will return an empty string.The internal directory is where your app should create and manage app specific files such as game saves, preferences, items purchased and so on.@return The app's internal directory.#endFunction InternalDir:String()#If __TARGET__="android"Local env:=sdl2.Android_JNI_GetEnv()Local cls:=env.FindClass( "com/monkey2/lib/Monkey2FileSystem" )Local mth:=env.GetStaticMethodID( cls,"getInternalDir","()Ljava/lang/String;" )Local dir:=env.CallStaticStringMethod( cls,mth,Null )Return dir#Elseif __TARGET__="ios"local dir:=getInternalDir()Return dir#EndifReturn ""EndSeptember 27, 2017 at 7:44 am #10832ok. how would this work for desktop?
September 27, 2017 at 8:09 am #10834On windows, you can use GetEnv(“appdata”) to get the aplication data directory, then append “/myprogramname/data/” to it.
Local Dir:=GetEnv(“appdata”)+”/myprogramname/data/”
CreateDir(Dir)
etc…Don’t know if that would work on other platforms. Monkey probably should have a command like AppDataDir() to return the place to store persistent data across all platforms.
September 27, 2017 at 8:15 am #10835ok. how would this work for desktop?
Topic about mobile.
For desktop there are AppDir () AssetsDir () HomeDir ().
September 27, 2017 at 8:21 am #10837Maybe I’m oversimplifying here, but isn’t it enough to create a text file in a folder where the main monkey file is, then just:
Monkey1#Import "myFolder/myFile.txt"From then on, simply use SaveString and LoadString with the file asset, like this:
Monkey1SaveString( myData, "asset::myFile.txt" )myFile.txt is copied to the build folder when you build the app, and “lives” with the app from now on, allowing persistent data to be saved and loaded. The original file is unaltered when the app saves the string, since it now saves over the copy in the build folder instead of the original.
(never tried it on mobile, but should work, shouldn’t it?)
September 27, 2017 at 11:19 am #10840Thanks for all the replies!
According to the Android developer docs the internal storage is lost when the user uninstalls the app. It isn’t clear if it is kept on re-install (e.g. upgrading) the app, but this isn’t quite the persistency that i’m looking for.
For my particular use case, I’m going to be unlocking levels as the user progresses through the game, and i’d like that information to remain if the user uninstalls (how dare they!) and re-installs later, or if there is an upgrade (e.g. bug fixes). I think this should be standard behaviour on mobile platforms to store this kind of data persistently?!
Edit: It looks like the old Monkey uses SharedPreferences on Android and something else that I don’t understand on iOS to achieve this. I don’t suppose anyone has implemented this in the new Monkey?
September 27, 2017 at 11:23 am #10841If you will store data in external storage – user can formatting sd-card and lose your data.
Need to use cloud-based storage like google-drive or play-services.
September 27, 2017 at 11:27 am #10842Hi nerobot,
I just added an edit to my above post on how the old Monkey implements persistency through re-installs. I think if the user completely reinstalls their device then i’m not going to be overly bothered if they lose their game progress (maybe I should be?!) but just for upgrading the app, this data should be stored.
September 27, 2017 at 11:52 am #10843I think when user uninstall app he expects data losing.
AFAIK private shared prefs also will be removed on app uninstall.
September 27, 2017 at 12:16 pm #10844Yeah you’re probably right on shared prefs being removed also. I slightly disagree on the user expecting to lose the data though, but maybe a cloud based method is best for storing this as you suggest.
Thanks for the help!
September 28, 2017 at 7:25 pm #10863IMO, a web/cloud solution is best here, as a user *should* be able to wipe device data the way they can wipe apps if they want, so if you want data to be truly recoverable, it should probably be stored off device, perhaps even on an SD card?
Are there any android/ios APIs for accessing google drive or icloud?
September 28, 2017 at 8:04 pm #10865For games, on andrid there’s “google play games”, on ios it’s on the “game center” I think, but I only have an android for now..
(dunno for files cloud) -
AuthorPosts
You must be logged in to reply to this topic.