Search code examples
javascriptintegerp5.jssaving-data

Saving an integer locally in p5


Okay, so I've looked everywhere and can't find a usable answer. I'm on an iMac using p5 and I need to be able to save (by pressing a key, for example) and load (at the start of the program) an integer. If no value is set, make it 0. I don't care what format the integer is saved in, whether it be a text file or any other file type. I've tried using save(), but there's no way to load the contents afterward. Any help?


Solution

  • P5.js doesn't have anything specific for this. You'll have to use regular JavaScript.

    You should start a search for "JavaScript save data to file" for a ton of results.

    One simple and commonly used option is to use cookies.

    To write:

    document.cookie = 'value=' + yourValueHere + '; expires=18 Dec 2017 12:00:00 UTC'
    

    Then to read:

    var yourValueHere = document.cookie.substring(6);
    

    That's just a very basic example, but hopefully it gets you on the right path. Good luck.