Loading new data - preload again?

In order to load data with loadStrings(), I assume I need to use preload() - at least, that's what my first test shows :)

But if I want to be able to load another text file while a sketch is running (to load the next level in a game, for instance) can I call preload again? I assume that could cause trouble since it would "preload" other things that are already loaded.

I haven't used callbacks, but is that an alternative to preloading when using loadStrings()? If so, what would it look like.

Also, since the data (a list of numbers) loads as strings, is there an easy way to convert them to numbers as they load into the array? Here's the loading line of code"

gridSquare = loadStrings('data.txt');

But after much banging of my head wondering why the data wasn't doing what it should, , I realised it does exactly what it says and loads strings. I assume I need split. again, but how?

Answers

  • edited October 2016

    After preload(), all resource loadings need an extra callback function, b/c it takes some time to accomplish the task: http://p5js.org/reference/#/p5/loadStrings

    In order to convert String to Number in JS, it's enough to prefix the object w/ the unary operator +:
    let myNumber = +'34.8'; // string '34.8' becomes number 34.8

  • Thanks Goto. I actually managed it by parsing through int. As always, I found the solution minutes after posting the question (I had seen the Processing version but not P5 and was close but not close enough). But this works when called outside of preload() (I tried wrapping it around the loadStrings call inside preload, but it failed)...

    numArray = (int(stringArray));

    I don't quite get callbacks yet so I'll have to come back to that. I understand the purpose but I have to get my head around what it should call when it's done. Does everything stop while it loads, then continue from the callback? Or do other asynchronous things keep happening?

  • Does everything stop while it loads, then continue from the callback?

    The callback parameter is necessary for the reason loadings don't block in JS!
    The program goes on to next statement and doesn't wait for anything to complete loading!

  • edited October 2016

    So, does that mean everything stops if there's a callback? And if the load function is called from some random point (like when the user presses "Load New Game"), where should the callback point to - the setup function, in order to reset everything from scratch? In something essentially linear like QBasic, I could get this stuff - but in Javascript I quickly lose track of how order is determined once the thing is running.

    I realise it's probably a difficult question to answer without seeing the whole thing :-/

    I imagine it will make more sense when I come to do it, but the flow has always bugged me.

  • edited October 2016

    So, does that mean everything stops if there's a callback?

    No! No block means no stop! L-)
    IMO, you shouldn't be worrying about loading & saving operations before you got your game in some functional state.
    Just load the game data once in preload() and leave the rest for later. :-\"

  • I have the basic functionality. The sprite is animated and moves. It climbs, falls and stops as required and can pick things up. There's more needed but it's mostly variations on what I've got already - just more fumbling around with conditionals and keeping count of a few things as play progresses.

    I'm currently working on the map editor (mainly because it's easier that drawing maps with for-loops :) It's pretty well functional now (apart from that P5 save issue which I have more info on in the other thread).

    Basically I'm thinking ahead here, given that I discovered I need to load the data in preload. When the user gets to Level 2, and new data needs to be loaded, what happens? How do things stop and wait for the new data to "preload"?

  • edited October 2016 Answer ✓
    • Easiest option, if memory allows it, is to load everything in preload().
    • Otherwise, you're gonna need to declare some variable to store the state about the data file.
    • Let's call it dataReady here: let dataReady;.
    • Just before loading the data file, assign false to it: dataReady = false;.
    • Within the passed callback function, assign true to it: dataReady = true;.
    • Now every code block which depends on that data file is gonna need to check dataReady before proceeding: if (dataReady) { ... }.
  • Got it. Thanks. Makes absolute sense.

  • Actually, I don't think this works. Some initialising routines need data from the data file but they only run once (called from setup()) so if the data isn't ready instantly it never gets checked again so some functions never receive valid data to work with.

    What I want to do is to load data from a separate text file for each level. I assumed I would do this by incrementing the level as I need it and calling the appropriate file (data1, data2, data3...) and stuffing that in the main array. But nothing works properly if the data doesn't preload.

    Do I have to put a while loop in some loadData() function that repeats until dataReady is true and then put an if (dataReady) in draw() in order to only run the main loop when it;'s good to go (seems like a lot of unnecessary checking once it's true)? And would I call loadData() from setup() and then call loadData() again at the start of each level?

    I guess this is more of a general program design question than specifically p5.

  • Thanks GoTo. I found a solution late last night by using a 2D array and loading each level into the array on preload (as you suggested earlier :) ). I hadn't come to terms with the 2D-array JS syntax before that. Hopefully I've come to terms with it now - and hopefully this does solve the problem.

Sign In or Register to comment.