Call Backs in Preload();

Hi Again,

Something I might be miss understanding here, but here goes.

The code below doesn’t run how my mind thinks it should. The call back function is being called twice (proved by the print statement) however it is only running the callback function on the item-cape json file and not the shield then the cape.

There are 11 of these JSON files that will be coming from API later, hence why i am only trying to process one at a time. any help would be great!

function preload() {
  for (var i = 0; i < 23124; i++) {
    itemStore.push(undefined);
  }  
  itemdata = loadJSON('itemData/items-shield.json', _preloadloadItemData);
  itemdata = loadJSON('itemData/items-cape.json', _preloadloadItemData);
  guiIcons = loadImage('itemData/GuiIcons.png');
}

function _preloadloadItemData() {
  //print(itemdata);
  for (var i = 0; i < 23124; i++) {    
    if(itemdata[i] != undefined){
      var item = new Item(itemdata[i]);
      itemStore.splice(i,1,item);
    }
  }
}

Thanks

Ryan

possibly it is running, but you just overwrite

itemdata

can you make a

itemdata_shield
itemdata_cape

and your looping i not understand,
23124 * 2 ( * 11 )


Hi Kll,

I was going to make seperate varaibles, however this would be a 11x repeated code? hence why i try to overwrite the variable in hope that it would run thought it all again without having differnt var names.

What happens with the loop is that i have a list of IDs upto 23124. This is the max ID on the database.

each Json file contains odd bits of that id List as such. so for each list i loop through all the entries and if that json file doesnt have id 1, it returns undefined. so the below should overwrite the undefined’s in the list with the ID in the JSON if it comes across it.

function _preloadloadItemData() {
  //print(itemdata);
  for (var i = 0; i < 23124; i++) {    
    if(itemdata[i] != undefined){
      var item = new Item(itemdata[i]);
      itemStore.splice(i,1,item);
    }
  }
}

Example of loading the data goes as follow:

First File Run through, itemstore might look like:

[undefined,undefined,undefined,undefined,[Object5], ...]

Second File: Run through, itemstore might look like:

[undefined,[Object2],[Object3],undefined,[Object5], ...]

Third File: Run through, itemstore might look like:

[[Object1],[Object2],[Object3],[Object4],[Object5], ...]

and so on, untill all the data is loaded. this is what the splice(i,1,item) is for to replace undefined with a data object as such.

Hope that gives some clarification.

if you want combine the data from 2 ?array? can do later,
but i understand
https://p5js.org/reference/#/p5/loadJSON
-a- inside preload use

  itemdata_shield = loadJSON('itemData/items-shield.json');
  itemdata_cape  = loadJSON('itemData/items-cape.json');

-b- outside preload use

  loadJSON('itemData/items-shield.json', _preloadloadItemData);
  loadJSON('itemData/items-cape.json', _preloadloadItemData);

add i remember about a problem where

`datatype` String: "json" or "jsonp"

was required


my worry is, that using two times

itemdata =
itemdata =

would reset the first result, and that fits to your error description.

Hi,

Thanks again,

I did find a solution to this, however found some very odd behaviour while trying to.

  itemdata = loadJSON('itemData/items-ring.json', _preloadloadItemData);
  itemdata_1 = loadJSON('itemData/items-ammo.json', _preloadloadItemData);
  itemdata_2 = loadJSON('itemData/items-body.json', _preloadloadItemData);
  itemdata_3 = loadJSON('itemData/items-feet.json', _preloadloadItemData);
  itemdata_4 = loadJSON('itemData/items-cape.json', _preloadloadItemData);
  itemdata_5 = loadJSON('itemData/items-hands.json', _preloadloadItemData);
  itemdata_6 = loadJSON('itemData/items-legs.json', _preloadloadItemData);
  itemdata_7 = loadJSON('itemData/items-head.json', _preloadloadItemData);
  itemdata_8 = loadJSON('itemData/items-neck.json', _preloadloadItemData);
  itemdata_9 = loadJSON('itemData/items-shield.json', _preloadloadItemData);
  itemdata_10 = loadJSON('itemData/items-weapon.json', _preloadloadItemData);
function _preloadloadItemData(itemdata) {
  for (var i = 0; i < 23124; i++) {    
    if(itemdata[i]){
      var item = new Item(itemdata[i]);
      itemStore.splice(i,1,item);
    }
  }
}

I wasn’t passing itemdata as the argument in the preloadItemData function hence was doing something odd.

This is all good, however I don’t know if its worth pointing out that when i loaded the data in setup instead, i checked the itemStore and found that the item was in there, however i was unable to print out the data individually (itemStore[x]) however I could see the entry in that spot when i printed the whole itemStore? I don’t know if its worth checking this out? if not call this case close.

Thanks

Ryan

hm, did you try? ( like in setup )

  loadJSON('itemData/items-ring.json', _preloadloadItemData); 
  loadJSON('itemData/items-ammo.json', _preloadloadItemData);
  loadJSON('itemData/items-body.json', _preloadloadItemData);
  loadJSON('itemData/items-feet.json', _preloadloadItemData);
  loadJSON('itemData/items-cape.json', _preloadloadItemData);
  loadJSON('itemData/items-hands.json', _preloadloadItemData);
  loadJSON('itemData/items-legs.json', _preloadloadItemData);
  loadJSON('itemData/items-head.json', _preloadloadItemData);
  loadJSON('itemData/items-neck.json', _preloadloadItemData);
  loadJSON('itemData/items-shield.json', _preloadloadItemData);
  loadJSON('itemData/items-weapon.json', _preloadloadItemData);