Does LoadState / SaveState work on Android?

Monkey Targets Forums/Android/Does LoadState / SaveState work on Android?

matty(Posted 2011) [#1]
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.


therevills(Posted 2011) [#2]
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)



matty(Posted 2011) [#3]
Okay thanks, I'll have another look when I get the chance.


matty(Posted 2011) [#4]
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?


therevills(Posted 2011) [#5]
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...


matty(Posted 2011) [#6]
Thanks therevills, I will try that again later...how did you work out that you needed to trim the string first?


matty(Posted 2011) [#7]
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...


therevills(Posted 2011) [#8]
Are you sure that the state exists?

Local state:String = LoadState()
if state
    highscore = Int(state.Trim())
end



matty(Posted 2011) [#9]
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...


therevills(Posted 2011) [#10]
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



matty(Posted 2011) [#11]
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..