Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use preload() when writing an external library with async functions #1074

Closed
bmoren opened this issue Nov 4, 2015 · 6 comments
Closed

Comments

@bmoren
Copy link
Contributor

bmoren commented Nov 4, 2015

I'm trying to write a function that can be used in preload() to return data without using a callback, similar to how loadJSON works.

for example:

p5.prototype.getCoolData = function(callback){
  var ret = false;
  getExternalData(function(data){
    ret = data;
    if (typeof callback == 'function'){
      callback(data)
    }
  })
  return ret;
}

p5.prototype.registerPreloadMethod('getCoolData'); 

The above code will return false when used in preload() like this:

var cool_data;
function preload(){
  cool_data = getCoolData();
}

function setup(){
  print(cool_data);
}

I was looking through the source code for p5 and couldn't understand how you are doing this with the load methods, like loadJSON and loadXML. I would like to replicate the style in the load methods of supporting both preload & callback. Is there a simple example of setting up a function with async data to use either a callback or the preload() and a variable.

@lmccart
Copy link
Member

lmccart commented Nov 4, 2015

i believe this has to do with the immutability of boolean vars. all of the load methods in p5 return an object that is later modified, so the pointer to the original object remains intact. ie an empty object or array is created and later populated with properties or values.
seems like the same issue is described here: #815 (comment).

this should be better documented, sorry.

@bmoren
Copy link
Contributor Author

bmoren commented Nov 4, 2015

this worked! Its true, don't rewrite the data, just update it!

@bmoren bmoren closed this as completed Nov 4, 2015
@bmoren
Copy link
Contributor Author

bmoren commented Nov 4, 2015

//example async function for use in preload() or with callback
p5.prototype.getData = function(callback){
  //create an object to hold some data. We will need to update this data below, not overwrite it. It is crucial to the preload() to keep the original pointer.
  var ret = {};

  //some async fucntion you are working with
  loadDataFromSpace(function(data){

    //loop through the properties in data
    for( prop in data){
      //set the ret proporties to be the data properties ( update the empty ret object with the properties from data )
      // you CANNOT overwrite ret with another object, it must be updated with the data.
      ret[prop] = data[prop];
    }
    //check if the callback is a function
    if (typeof callback == 'function'){
      //do the callback
      callback(data)
    }
  })
  //return the object which has been filled with data above
  return ret;
}

@bmoren
Copy link
Contributor Author

bmoren commented Nov 4, 2015

Added example to the Libraries Wiki page

@lmccart
Copy link
Member

lmccart commented Nov 4, 2015

thank you!

@dhowe
Copy link
Contributor

dhowe commented Nov 13, 2015

Again, if we stick with this mechanism for preload we should make it very clear in the docs that none of the primitive types (boolean, string, or number) will work as return values for preload registered functions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants