Loading...
Logo
Processing Forum
I'm trying to build an application to send and receive OSC messages. I built a class of buttons and the idea is that if a formatted message ("name"+id+string, such as: name 2 Harrold) is received, the class checks the id and names the buttons accordingly. Unfortunately I get error with this code in the class constructor.

import oscP5.*;
import netP5.*;

OscP5 oscP5;
NetAddress myRemoteLocation;

Clip clip1;
Clip clip2;
Clip clip3;
Clip clip4;
Clip clip5;
Clip clip6;
Clip clip7;
Clip clip8;

int val=100;
int isplaying;

void setup() {
  //size(screenWidth, screenHeight, P2D);
  size(500, 500);
  PFont font;
  font = loadFont("LiGothicMed-48.vlw");
  oscP5 = new OscP5(this, 12000);
  myRemoteLocation = new NetAddress("127.0.0.1", 11001);

  clip1=new Clip(50, 50, color(200), 0);
  clip2=new Clip(50, 120, color(255, 232, 160), 1);
  clip3=new Clip(50, 190, color(232, 160, 160), 2);
  clip4=new Clip(50, 260, color(45, 200, 1), 3);
  clip5=new Clip(50, 330, color(145, 200, 221), 4);
  clip6=new Clip(50, 400, color(3, 20, 121), 5);
  clip7=new Clip(50, 470, color(255, 200, 255), 6);
  clip8=new Clip(50, 540, color(200, 160, 145), 7);
}

void draw() {
  clip1.display();
  clip1.change();
  clip2.display();
  clip2.change();
  clip3.display();
  clip3.change();
  clip4.display();
  clip4.change();
  clip5.display();
  clip5.change();
  clip6.display();
  clip6.change();
  clip7.display();
  clip7.change();
  clip8.display();
  clip8.change();
}
 

... and the class:

int b;
color c=255;
int sx=150;
int sy=60;
String clipname="clip";

class Clip {
  int x, y;
  color cc;
  color c;
  int id;

  Clip(int tempX, int tempY, color tempcc, int tempid) {
    x=tempX;
    y=tempY;
    cc=tempcc;
    id=tempid;
  }
  void display() {
    fill(c);
    noStroke();
    rect(x, y, sx, sy);
    //triangle(x+sx, sy, x+sx, y+sy, x+sx+20, y+sy/2);
    fill(cc);
    rect(x+10, y+10, 130, 40);
    fill(0);
    text(clipname, x+60, y+30);
   }
  boolean over() {
    if (mouseX>=x&&mouseX<=(x+sx)&&mouseY<=(y+sy)&&mouseY>=y) {
      return true;
    } 
    else {
      return false;
    }
  }
  void change() {
    if (over()&&mousePressed) {
      c=100;
      OscMessage myMessage = new OscMessage("/on");
      myMessage.add(id);
      oscP5.send(myMessage, myRemoteLocation);
      println(id);
    }  
    else {
      c=255;
    }
  }
  void play() {
    color d=color(5, 252, 66);
    fill(d);
    rect(x, y, 10, 60);
  }
}
void oscEvent(OscMessage theOscMessage) {
  if (theOscMessage.checkAddrPattern("name")==true) {
    int nameid = theOscMessage.get(0).intValue();
    String  named = theOscMessage.get(1).stringValue();
    println("name "+nameid+" "+named);
    if(nameid==id){
      clipname=named;
    }
  }
}




Where the last bit of the class constructor, with the OSC parsing causes the trouble:

if (theOscMessage.checkAddrPattern("name")==true) {
    int nameid = theOscMessage.get(0).intValue();
    String  named = theOscMessage.get(1).stringValue();
    println("name "+nameid+" "+named);
    if(nameid==id){ //   "Cannot find anything named "id"
      clipname=named;
    }


Why can't I use int id? 

Replies(8)

General advice: you should really learn to use arrays...

" Where the last bit of the class constructor"
Wrong. oscEvent() isn't in the class at all. It is just after the class definition, but it is at the top (global) level. So it knows nothing about id which is a class variable (a field).
I suppose you have to iterate on the clips (here, having them in an array would make things easier) to find which one has this id.
Good point, thanks! 
I changed the sketch using arrays for the clip class and OSC plugs the receive and parse incoming OSC messages. 

import oscP5.*;
import netP5.*;

OscP5 oscP5;
NetAddress myRemoteLocation;

Clip[] clip=new Clip[8];

int vertical=50;
int val=100;
int isplaying;

color shade=color(200);

int id[]= {0, 1, 2, 3, 4, 5, 6, 7};

void setup() {
  //size(screenWidth, screenHeight, P2D);
  size(500, 700);
  PFont font;
  font = loadFont("LiGothicMed-48.vlw");
  oscP5 = new OscP5(this, 12000);
  myRemoteLocation = new NetAddress("127.0.0.1", 11001);
  oscP5.plug(this, "name", "/name"); 
  oscP5.plug(this, "colore", "color");

  clip[0]=new Clip(vertical, 50, color(shade), id[0]);
  clip[1]=new Clip(vertical, 120, color(255, 232, 160), id[1]);
  clip[2]=new Clip(vertical, 190, color(232, 160, 160), id[2]);
  clip[3]=new Clip(vertical, 260, color(45, 200, 1), id[3]);
  clip[4]=new Clip(vertical, 330, color(145, 200, 221), id[4]);
  clip[5]=new Clip(vertical, 400, color(3, 20, 121), id[5]);
  clip[6]=new Clip(vertical, 470, color(255, 200, 255), id[6]);
  clip[7]=new Clip(vertical, 540, color(200, 160, 145), id[7]);
}
public void name(int ide, String nombre) { // plug for the clip names sent from Max
  for (int j=0;j<id.length;j++) {
    if (id[j]==ide) {
      println("id "+ide+" name "+nombre);
    }
  }
}
public void colore(int idc, int r, int g, int b) { // clip color
  for (int k=0;k<id.length;k++) {
    if (id[k]==idc) {
      println("id "+idc+" RGB "+r+" "+g+" "+b);
      shade=color(r, g, b);
    }
  }
}
void draw() {
  for (int i=0;i< clip.length;i++) { // buttons
    clip[i].display();
    clip[i].change();
  }
}
 

When I send a message like "color 0 23 76 220" to the sketch, it prints out the id and the RGB values from the message. I want to change the color of the buttons (shade) with the incoming messages but it still doesn't work. Any ideas? 
That's a quite common error you made.
You use shade only in:
clip[0]=new Clip(vertical, 50, color(shade), id[0]);
There is no binding to this variable, only its value at the time of the object creation is used: if the value of shade changes, you have to update this clip. (Side note: shade is already a color, so you don't need to call color(shade).)

So how can I pass on the incoming RGB values to the clip class?
Something like clip[0].cc = color(r, g, b);
Or, more formally, provide a method of this class updating the color, something like:
void setColor(int r, int g, int b)
{
  cc = color(r, g, b);
}
called with:
clip[0].setColor(r, g, b);

Brilliant! Thanks!
Working perfectly, thanks!