About Monkey 2 › Forums › Monkey 2 Programming Help › [STREAMS] WriteInt and Stream.Open –
This topic contains 11 replies, has 3 voices, and was last updated by
Amon
1 year, 4 months ago.
-
AuthorPosts
-
December 5, 2017 at 10:14 pm #12152Monkey1234567If Keyboard.KeyHit(Key.F1)Local sr:Streamsr.Open("asset::levels.txt", "rw")sr.WriteInt(22)sr.Close()Endif
I know I’m missing a ‘new’ somewhere. I’ve tried but I still get “Attempt to invoke method on null instance” from the above code.
I want to Open or Create a new file called levels.txt and write an int to it for player lives or scores etc.
Can somebody assist with an example?
Ta!
December 5, 2017 at 11:21 pm #12154Stream.Open returns a new stream so you need to use:
sr=Stream.Open( blah.. )
December 6, 2017 at 12:05 am #12155Monkey12345678If Keyboard.KeyHit(Key.F1)Local sr:Streamsr = Stream.Open("asset/levels.txt", "rw")sr.WriteInt(22)sr.Close()EndifIt now errors on sr.WriteInt with the same error.
Thanks for the help, also.
December 6, 2017 at 12:09 am #12156If Stream.Open returns null, then file probably could not be found/created.
Try using “asset::levels.txt” (although you’re not really meant to write to assets), or change to a directory that contains an “assets” subdirectory.
December 6, 2017 at 1:30 am #12160For simple stuff you can use also SaveString ( “22”,”assets/levels.txt” ), and level=Int (LoadString (blah..)) if there as a simple integer value.
December 6, 2017 at 2:03 am #12162Again though, note that “assets/” will probably NOT work unless there happens to be an assets dir in the current dir!
December 6, 2017 at 7:10 am #12169Success!
Only if I put the full path to the file “levels.txt”. i.e. sr.WriteInt(“c:/mydirectory/anotherdirectory/assets/levels/levels.txt”).
Anything other than using the full path to the file spits out the same error.
December 6, 2017 at 10:51 am #12172What about
Monkey12345Local path:=AppDir()+"levels.txt"CreateDir( ExtractDir( path ) )Local sr:=Stream.Open( path,"rw" ).....sr.Close()December 6, 2017 at 1:05 pm #12178That keeps crashing with the same error. When I print the path it points to the compiled .products folder and the directory is never created.
December 6, 2017 at 7:35 pm #12182Ok, the main problem here would appear to be that “rw” does not create a file if none exists. Using “w” instead of “rw” fixes all above path problems for me, ie: I can use “asset::text.txt” etc.
On the other hand, “w” will always create a new file, even if it means deleting an existing file.
This is std C behaviour and all seems sensible to me, but perhaps “rw” should be tweaked so it behaves like “w” if no file exists?
[edit]Updated docs to describe “w” vs “rw”
December 7, 2017 at 4:46 am #12187but perhaps “rw” should be tweaked so it behaves like “w” if no file exists?
I agree with such behavior.
December 8, 2017 at 9:03 am #12199Yep, I agree, and, it all works fine when using “w”. So all is good.
Thank you.
-
AuthorPosts
You must be logged in to reply to this topic.