0

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?

simplexshotz
  • 139
  • 12

1 Answers1

0

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.

Kevin Workman
  • 41,537
  • 9
  • 68
  • 107
  • Are you sure this works in p5? Or just HTML javaScript? – simplexshotz Sep 19 '17 at 21:43
  • @user8340815 P5.js **is** JavaScript. You can write plain old JavaScript in P5.js. In fact, it would be more accurate to say that you're writing P5.js inside plain old JavaScript, as P5.js is really just a JavaScript library. And yes, I just tested it to confirm that it works in P5.js. – Kevin Workman Sep 19 '17 at 21:44
  • Could you give the full code? I can't seem to get it to work. – simplexshotz Sep 19 '17 at 21:51
  • @user8340815 The code in my post pretty much is the full code. If you can't get it working, please post a [mcve] and explain exactly how you're runing your code. – Kevin Workman Sep 19 '17 at 21:52
  • @user8340815 Also note that this won't work if you're just opening a local `.html` file: https://stackoverflow.com/questions/12992494/how-to-read-write-cookies-for-local-file-html-document – Kevin Workman Sep 19 '17 at 21:53
  • **Also note that this won't work if you're just opening a local `.html` file** You mean when it's run in the browser? – simplexshotz Sep 19 '17 at 22:04
  • @user8340815 No. I mean when the url starts with `file://`. – Kevin Workman Sep 19 '17 at 22:13
  • Oops, I realized the issue. I was accidentally using `size()` instead of `createCanvas()` – simplexshotz Sep 19 '17 at 22:34