Preferences class

About Monkey 2 Forums Monkey 2 Code Library Preferences class

Tagged: 

This topic contains 11 replies, has 3 voices, and was last updated by  therevills 4 months, 2 weeks ago.

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

    nerobot
    Participant

    This class allow us to store params inside of single class-container with load/save stuff.

    Class is based on Json format. You can extend it by adding new methods like GetFloat() if needed.

    You put any supported values using PutValue and get values using GetXxx methods like GetString, GetColor…

    How do I use it:

    wnd.CloseRequested is my own signal:

    #12030

    nerobot
    Participant

    Also there is Changed( key,value ) signal which helps us to be notified about values changes.

    Note: you should to cast value to concrete type from its Variant type.

    #12031

    nerobot
    Participant

    I forgot one thing.

    There is Color extension class to be able to convert colors from/to Json format:

    #12032

    therevills
    Participant

    Cool!

    I dont know if its in MX2 yet, but for Windows we need a way to save to the “correct” Microsoft approved folder locations.

    #12099

    Diffrenzy
    Keymaster

    Very useful,  thanks!

    I’m thinking a good place to store the file would be in SDL_GetPrefPath() (is in MX2 now)  like in

    A simple Applet framework

    What do you think?

    #12100

    nerobot
    Participant

    I still hope that the best way is to use pure monkey2’s path..

    But I didn’t use mobile targets yet.

    You can write own Function GetPrefsFile:String( file:String,author:String="defValue",name:String="defValue" ) that will use sdl prefs or what you want else, and you can change single place if needed to make changes for a whole project.

    And to link with my code:

    Const AppPrefs:=Preferences.Get( GetPrefsFile( "app.json" ) )

    #12665

    therevills
    Participant

    Note: The code from nerobot, needs the extensions from this post: http://monkeycoder.co.nz/forums/topic/json-extensions/

    I’m also getting run time error: “Attempt to invoke method on null instance” on the “If _prefs.Contains( path ) Return _prefs[path]” line…

    Test code, I had to “hack” some of the features to crappy returns etc:

    #12681

    therevills
    Participant

    Here is a slightly fixed simpler runnable example:

    #12685

    nerobot
    Participant

    Thanks for concatenating all related parts here!

    Note: “New Preferences().Get(…” – you needn’t to use New here because Get is a function not a method and it can be accessed by class name not an instance.

    Syntax in my first post is correct.

    #12686

    therevills
    Participant

    That’s what I thought, but without the new you get “Attempt to invoke method on null instance” error…

    #12687

    nerobot
    Participant

    Did you check line with error by debugger?

    #12688

    therevills
    Participant

    I’m also getting run time error: “Attempt to invoke method on null instance” on the “If _prefs.Contains( path ) Return _prefs[path]” line…

    🙂

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

You must be logged in to reply to this topic.