-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Comments
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. this should be better documented, sorry. |
this worked! Its true, don't rewrite the data, just update it! |
//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;
} |
Added example to the Libraries Wiki page |
thank you! |
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. |
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:
The above code will return
false
when used in preload() like this: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.The text was updated successfully, but these errors were encountered: