How to use SaveString

Monkey Forums/Monkey Beginners/How to use SaveString

SirRollon(Posted 2014) [#1]
I'm trying to use SaveString function, but I guess I'm doing it wrong.
Can someone have a look at my very simple code ?
When I run it, I have an "identifier not found" error about the savestring function.

Import mojo
Global sauvegarde:String


Class Save Extends App

	Field adjust:Int

	Method OnCreate()

		sauvegarde = "test"
		SaveString(sauvegarde,"monkey://data/save/save.txt")

	End

	Method OnUpdate()

		Print (sauvegarde)
		
	End


	Method OnRender()
	
		

	End

	

	Method OnLoading()

		

	End

End

Function Main()

	New Save

End



muddy_shoes(Posted 2014) [#2]
The function you're looking for is SaveState, not SaveString.

Edit: Or you need to import the os module to use SaveString.


SirRollon(Posted 2014) [#3]
If I put SaveState instead of SaveString, I get this :
"unable to fin overloadfor SaveState(String, String)"


Gerry Quinn(Posted 2014) [#4]
I think SaveString is available only for some platforms i.e. GLFW and stdcpp.

Web-style platforms such as HTML don't allow you to save files freely - I think on those you are limited to SaveState(), or outputting text to the console.

It's not a Monkey issue, just some of the targets don't let you do everything.


muddy_shoes(Posted 2014) [#5]
Yeah, sorry, I read too quickly and missed your file path. If you want to save to a specific file via SaveString, you need to import the os module. If you just want to save one string for game state then use SaveState from the mojo module.


SirRollon(Posted 2014) [#6]
Ok, I missed that part in the os module : "IMPORTANT: The os module is currently only available for the glfw and stdcpp targets."

So the problem remains with the SaveState that gave me an overload error...


muddy_shoes(Posted 2014) [#7]
SaveState doesn't take a file path. It just takes a string. The string is saved to a location specific to each target.


SirRollon(Posted 2014) [#8]
Ok, that works. Thank you.
However if I close the game, the SaveState data are erased.
How to get a persistant data of SaveState on a Html5 ?
Can I do it with savestate or should I use something else ?


muddy_shoes(Posted 2014) [#9]
The SaveState data on HTML5 is saved by the browser on a per-URL basis. The Monkey HTTP server that is used when you run a HTML5 build locally takes a random port number on start and therefore provides a randomised URL. So, if you are running the game locally and close and restart the Monkey HTTP server the URL will be changed and you will appear to lose the save data. On a normal server with a static URL this doesn't happen.


SirRollon(Posted 2014) [#10]
Last question on this subject.
On a normal server, are the saved data linked with cookies ?


muddy_shoes(Posted 2014) [#11]
No. It uses the javascript localStorage object. Exactly how this is implemented is browser dependent.