[STREAMS] WriteInt and Stream.Open –

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.

Viewing 12 posts - 1 through 12 (of 12 total)
  • Author
    Posts
  • #12152

    Amon
    Participant

    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!

    #12154

    Mark Sibly
    Keymaster

    Stream.Open returns a new stream so you need to use:

    sr=Stream.Open( blah.. )

    #12155

    Amon
    Participant

    It now errors on sr.WriteInt with the same error.

    Thanks for the help, also.

    #12156

    Mark Sibly
    Keymaster

    If 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.

    #12160

    nerobot
    Participant

    For simple stuff you can use also SaveString ( “22”,”assets/levels.txt” ), and level=Int (LoadString (blah..)) if there as a simple integer value.

    #12162

    Mark Sibly
    Keymaster

    Again though, note that “assets/” will probably NOT work unless there happens to be an assets dir in the current dir!

    #12169

    Amon
    Participant

    Success!

    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.

    #12172

    nerobot
    Participant

    What about

    #12178

    Amon
    Participant

    That keeps crashing with the same error. When I print the path it points to the compiled .products folder and the directory is never created.

    #12182

    Mark Sibly
    Keymaster

    Ok, 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”

    #12187

    nerobot
    Participant

    but perhaps “rw” should be tweaked so it behaves like “w” if no file exists?

    I agree with such behavior.

    #12199

    Amon
    Participant

    Yep, I agree, and, it all works fine when using “w”. So all is good.

    Thank you.

Viewing 12 posts - 1 through 12 (of 12 total)

You must be logged in to reply to this topic.