Does LoadState / SaveState work on Android?
Monkey Targets Forums/Android/Does LoadState / SaveState work on Android?
| ||
Hi all, I created a simple game for Android recently (see here: http://home.swiftdsl.com.au/%7Etmalcolm/work_app/) and thought of including a save for the high score. When I include the "LoadState" command in my program in the OnCreate method of my app it simply exits to the 'desktop' with no error, but upon removing the command it works as expected. Are there some commands that are troublesome on Android that are already known problems? I will attempt to reproduce this error with some simpler code when I can. |
| ||
Works fine for me... Are you trying to load a string into an int without casting it? I do the following: Method ReadIntState:Int(index:Int, arr:String[]) If index < arr.Length Return Int(arr[index].Trim()) Else Return 0 End End Method Load:Bool() Local state:String = LoadState() If state Local stateParts:=state.Split("~n") Local no:Int = 0 level = ReadIntState(no, stateParts) |
| ||
Okay thanks, I'll have another look when I get the chance. |
| ||
I've tried it again and it simply force closes each time I use the loadstate command. This is how I used it: highscore:Int highscore = Int(LoadState()) if highscore = 0 then highscore = 10000 ' some default high score endif Is there something wrong with doing this? |
| ||
Due to how Android reads the string you must trim it...highscore:Int local state:String = LoadState() highscore = Int(state.Trim()) if highscore = 0 then highscore = 10000 ' some default high score endif Just another gotcha with Android... |
| ||
Thanks therevills, I will try that again later...how did you work out that you needed to trim the string first? |
| ||
Hi therevills, I tried it again, still not working. I did it like this: highscore:Int highscore = Int(LoadState().Trim()) It didn't seem to like that either... |
| ||
Are you sure that the state exists?Local state:String = LoadState() if state highscore = Int(state.Trim()) end |
| ||
Oh...I never realised you had to check if it was not null..I thought it would just return an empty string if it didn't exist... |
| ||
I've always done it because the example shows to do it:'run this app several times to see application state being updated. Import mojo.app Import mojo.graphics Class MyApp Extends App Field state$ Method OnCreate() 'comment out the following line to reset state state=LoadState() If state Print "state found - updating state!" state=Int( state )+1 Else Print "state not found - creating initial state!" state="1" Endif SaveState state End Method OnRender() Cls DrawText "state="+state,0,0 End End Function Main() New MyApp End |
| ||
Thanks therevills..I tried that simple example from the docs and it works..I should have realised that I had to check if the state was null first.. |