Message Queue with oscP5 not treating messages as objects

Hi,

Long story, but I’m trying to implement a queue handler for incoming OSC messages, as I’m getting concurrency issues.

There was a really helpful example from feb 2016 on the archived forum where user GoToLoop provided some really helpful example code that seems to do what I’m after.

BUT when I try to run the code I get an error saying
"cannot convert from element type Object to OscMessage"

I’ve tried a few things, and it looks like the message object getting appended to the ArrayList in the oscEvent function is being treated as an Object, rather than an OscMessage, but I can’t figure out how to fix it.

Can anyone please help explain why the example code isn’t working - has something changed in Processing.org, or in the OscP5 library since this was originally shared?

thanks in anticipation - I really want to find a way to solve the concurrency issue or my project won’t ever work properly.

import oscP5.*;
import netP5.*;
 
OscP5 oscP5;
ArrayList oscMessageQueue = new ArrayList();
 
void setup() {
  oscP5 = new OscP5(this, 12000);
}
 
void draw() {
   
  // take care of the OSC messages once per frame
  dispatchOSCMessages();
   
  // Do the rest of your rendering here, it won't be bothered by OSC messages showing up.
}
 
// Handle all the enqueued OSC messages at once.
void dispatchOSCMessages() {
  synchronized(oscMessageQueue) {
    for(OscMessage message : oscMessageQueue) {
      // Do things here that require modifying lists or other actions
      // which should never happen half-way through the draw function.
    }
     
    // clear the list of messages.
    oscMessageQueue.clear();
  }
}
 
// Add the OSC messages to the queue to be handled later.
void oscEvent(OscMessage message) {
  synchronized(oscMessageQueue) {
    oscMessageQueue.add(message);
  }
}
1 Like

Based on the ArrayList docs, your issue is solved by defining the ArrayList to store OscMessage objects:

ArrayList<OscMessage> oscMessageQueue = new ArrayList<OscMessage>();

Maybe it was overlooked in the example, but this should work.

Kf

1 Like

Fantastic, thanks so much - that works!