# Call Backs in Preload();

**URL:** <https://discourse.processing.org/t/call-backs-in-preload/10711>\
**Category:** Coding Questions\
**Created:** [April 28, 2019, 1:01pm UTC](https://discourse.processing.org/t/call-backs-in-preload/10711 "2019-04-28T13:01:46Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![peachman](https://avatars.discourse-cdn.com/v4/letter/p/278dde/32.png) [@peachman](https://discourse.processing.org/u/peachman)\
**Post date:** [April 28, 2019, 1:01pm UTC](https://discourse.processing.org/t/call-backs-in-preload/10711/1 "2019-04-28T13:01:46Z")

</div>

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!

```auto
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

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [April 28, 2019, 2:13pm UTC](https://discourse.processing.org/t/call-backs-in-preload/10711/2 "2019-04-28T14:13:27Z")

</div>

possibly it is running, but you just overwrite

```auto
itemdata

```

can you make a

```auto
itemdata_shield
itemdata_cape

```

* * *

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

* * *

---

<div class="post-metadata">

**Author:** ![peachman](https://avatars.discourse-cdn.com/v4/letter/p/278dde/32.png) [@peachman](https://discourse.processing.org/u/peachman)\
**Post date:** [April 28, 2019, 2:41pm UTC](https://discourse.processing.org/t/call-backs-in-preload/10711/3 "2019-04-28T14:41:26Z")

</div>

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.

```auto
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:

```auto
[undefined,undefined,undefined,undefined,[Object5], ...]

```

Second File: Run through, itemstore might look like:

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

```

Third File: Run through, itemstore might look like:

```auto
[[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.

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [April 28, 2019, 4:43pm UTC](https://discourse.processing.org/t/call-backs-in-preload/10711/4 "2019-04-28T16:43:56Z")

</div>

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

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

```

-b- outside preload use

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

```

add i remember about a problem where

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

```

was required

* * *

my worry is, that using two times

```auto
itemdata =
itemdata =

```

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

---

<div class="post-metadata">

**Author:** ![peachman](https://avatars.discourse-cdn.com/v4/letter/p/278dde/32.png) [@peachman](https://discourse.processing.org/u/peachman)\
**Post date:** [April 28, 2019, 5:36pm UTC](https://discourse.processing.org/t/call-backs-in-preload/10711/5 "2019-04-28T17:36:18Z")

</div>

Hi,

Thanks again,

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

```auto
  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);

```

```auto
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

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [April 29, 2019, 2:28am UTC](https://discourse.processing.org/t/call-backs-in-preload/10711/6 "2019-04-29T02:28:08Z")

</div>

hm, did you try? ( like in setup )

```auto
  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);

```
