NEED HELP - Can't return an array to Bubble from my Plugin

I’m trying to build a simple plugin to randomize the order of an array of a custom data type.

I’m running a function that shuffles the array randomly and then publishing the results back via instance.publish

On doing this, I get an error:
image

My shuffling function in INITIALIZE:

instance.data.shuffle = function () {
      var a = instance.data.inputList;
      var j, x, i;
      for (i = a.length - 1; i > 0; i--) {
          j = Math.floor(Math.random() * (i + 1));
          x = a[i];
          a[i] = a[j];
          a[j] = x;
      }
      instance.data.outputList = a;
      instance.publishState("randomized_list", instance.data.outputList);
    }

UPDATE:

function(instance, properties, context) {
	instance.data.inputList = properties.list;
    console.log(properties.list, instance.data.inputList);
    instance.data.outputList = [];
    try{
        instance.data.randomizeList();
        instance.publishState("randomized_list", instance.data.outputList);
    }
    catch(err){
        instance.publishState("randomized_list", null);
        console.log("Shuffling error", err);
    }
}

It’s probably something simple - but I think there’s a lot for me to learn from solving this problem. Any help is welcome!

Thanks,
Ranjit

Hey, what is the exact issue you are facing?

Things I saw:

  • You are calling instance.data.randomizeList(); from Update but your function is called instance.data.shuffle
  • It’s cleaner to pass the list as a parameter through to instance.data.shuffle than retrieve it from the instance.data object.
  • If your inputList field is of Bubble ‘list’ type then you need to fetch it using .get() and .length(). Bubble doesn’t give you vanilla arrays directly from Properties can you have to call the .get() method to fetch them.
  • It’s fine to split the work across Initialize and Update functions and I often do this, but it does add complexity for debugging which for small things probably isn’t worth it, so your shuffle function could probably just sit within Update too.

Try the following:

Within INTIALIZE

instance.data.shuffle = function (a) {
    var j, x, i;
    for (i = a.length - 1; i > 0; i--) {
        j = Math.floor(Math.random() * (i + 1));
        x = a[i];
        a[i] = a[j];
        a[j] = x;
    }
    return a;
  }

Within UPDATE

  if (properties.list){
    instance.data.inputList = properties.list.get(0,properties.list.length());
    console.log(properties.list, instance.data.inputList);
    instance.data.outputList = [];
    try{
        instance.data.outputList = instance.data.shuffle(instance.data.inputList);
        instance.publishState("randomized_list", instance.data.outputList);
    }
    catch(err){
        instance.publishState("randomized_list", null);
        console.log("Shuffling error", err);
    }
  }
4 Likes
1 Like

Thanks @exception-rambler
silly me!

I was treating properties.list as an array before doing a get() on it.
Tested it now and it works like a charm.

Cheers

1 Like

Loved your notes @jared.gibb
It’s a great reference for plugin development.

Thanks!

1 Like