How could I use a function from different class?

edited January 2018 in Library Questions

Hello, I am quite new to Prcoessing and I am stuck on using functions from a class to another function. I basically want to use 'void update' in 'void oscEvent(OscMessage theOscMessage' to send update function to OSC. Hope my problem makes sense :(

here is the code:

void draw() {
  background(0);
  for (Particle p : particles) {
    p.update();
  }
}

class Particle {

  PVector position, velocity;

  void update() {
    PVector mouse = new PVector(map(mouseX, 0, width, -width / 2, width / 2), map(mouseY, 0, height, -height / 2, height / 2), 0);
    PVector acc = PVector.sub(mouse, position);


    acc.limit(FULL_ACC);
    velocity.add(acc);
    velocity.limit(FULL_VEL);
    position.add(velocity);
  }

  void oscEvent(OscMessage theOscMessage) {
    if (theOscMessage.checkAddrPattern("/output_1")==true) {
      update(); //problem is here, what is the proper way in order for the OSC to pick up this info? 
      println("mouseMoved");
    } else if (theOscMessage.checkAddrPattern("/output_2")==true) {
      update();
      println("mouseMoved");
    } else if (theOscMessage.checkAddrPattern("/output_3") == true) {
      update();
      println("mouseMoved");
    } else {
      println("Unknown OSC message received");
    }
  }
}

Answers

  • edited January 2018

    Use ctrl-t for better indents

    Just say particle.update();

  • hi, thanks for getting back! I tried your method but it gave me an error stating "Cannot make a static reference to the non-static update() "

  • edited January 2018

    particle (with a small letter not a capital) was an example for an object

    You need to put in your object particles[i] when it’s an array etc

  • Use ctrl-t for better indents

    this is key. the code you posted is missing several }s which changes the whole meaning of the code. where does draw() end? is update() part of Particle class?

  • Hi, I apologise for the messy code. I fixed the curly brackets. Also, yes the update() is part of Particle class. Hope it makes more sense to you! Thanks

  • edited January 2018
    • If that's your whole Particle class, right off the bat I can see you don't initialize its 2 fields:
      position & velocity!
    • At least do this: final PVector position = new PVector(), velocity = new PVector();.
    • Another bug is that you expect the oscP5 library's instance to callback Particle::oscEvent() method.
    • However, the oscP5 library expects the oscEvent() method to belong to the PApplet's instance we pass as its argument when we instantiate it via new.
  • edited January 2018

    If you really wanna insist on keeping oscEvent() as a Particle method, a possible workaround is to call each instance of Particle from within PApplet::oscEvent(), passing the received OscMessage object to each 1 of them: :ar!

    void oscEvent(final OscMessage msg) {
      for (final Particle p : particles)  p.oscEvent(msg);
    }
    
  • edited January 2018
    • You can also pre-process the received OscMessage inside PApplet::oscEvent() by calling OscMessage::addrPattern() method:
      http://Sojamo.de/libraries/oscp5/examples/oscP5message/oscP5message.pde
    • Given the possible receivable patterns are: "/output_1", "/output_2" & "/output_3".
    • You can instead just grab the last number character from the acquired String: "1", "2" & "3".
    • And pass that parsed value, rather than the whole OscMessage, to a method from Particle: *-:)

    // Forum.Processing.org/two/discussion/25814/
    // how-could-i-use-a-function-from-different-class#Item_8
    
    // GoToLoop (2018-Jan-04)
    
    import oscP5.*;
    import java.util.List;
    
    final List<Particle> particles = new ArrayList<Particle>();
    
    void oscEvent(final OscMessage msg) {
      final String addr = msg.addrPattern();
      if (addr == null || addr.isEmpty())  return;
    
      final int act = addr.charAt(addr.length() - 1) - '0';
      for (final Particle p : particles)  p.action(act);
    }
    
    class Particle {
      void action(final int action) {
        println("Action:", action);
      }
    }
    
  • edited January 2018

    Hi, @GoToLoop thank you so much for your wonderful ideas. I tried all of them if it could make any sense to me. The project itself is longer than the one I posted above and the OSC message helps to send data to Wekinator (machine learning application). The particles can be controlled through Arduino sensor, which then the gestures are trained at Wekinator. So, I'm thinking Void oscEvent will send Particles through OSC message? Would it possible for you to look at the whole code?

    sorry for being a nuisance, but I really want this to work :( Thanks

  • edited January 2018

    Sorry, I don't have the hardware neither know much about the oscP5 library. :-<
    My answers were simply about what were obvious from what you had posted so far. :-\"

  • edited January 2018

    @hsawa post your entire code please

    What gotoloop is saying is that the function oscEvent won't work inside the class because it works only on the level of the sketch and not in a class.

    (This would also apply to the function mousePressed())

    A workaround would be to have the function oscEvent outside the class and pass its action into the class as he has demonstrated.

  • edited January 2018

    a

  • edited January 2018 Answer ✓

    this won't work:

    Particle.update();
    

    instead use e.g. (see gotoloop's post above)

      final int act = addr.charAt(addr.length() - 1) - '0';
      for (final Particle p : particles)  
            p.action(act);
    

    OR

      void oscEvent(OscMessage theOscMessage) {
        if (theOscMessage.checkAddrPattern("/output_1")==true) {
          for (final Particle p : particles)  
               p.action(1);
          println("mouseMoved");
        } else if (theOscMessage.checkAddrPattern("/output_2")==true) {
          for (final Particle p : particles)  
                        p.action(2);
          println("mouseMoved");
        } else if (theOscMessage.checkAddrPattern("/output_3") == true) {
          for (final Particle p : particles)  
                        p.action(3);
          println("mouseMoved");
        } else {
          println("Unknown OSC message received");
        }
      }
    

    and in the function action in the class you receive the parameter act and can act accordingly :

        if(act==1) {
    
        }
        else if(act==2) {
    
        }
        else if(act==3) {
    
        }
    
  • @Chrisir thank you so much!! It worked perfectly!, I was able to train the gestures as well through machine learning. I can't thank you enough. Thank you everyone :)

Sign In or Register to comment.