![]() |
Tutorial03 | HelloWorld 3 - More about messages |
To further explore the types of messages that a Max object can send and receive we're going to alter our class again and call it HelloWorld3.
import com.cycling74.max.*;
public class HelloWorld3 extends MaxObject {
public void bang() {
post("hello bang!");
outletBang(0);
}
public void loadbang() {
post("welcome to the patch!");
}
public void inlet(int i) {
post("hello integer " + i + "!");
outlet(0, i);
}
public void inlet(float f) {
post("hello float " + f + "!");
outlet(0, f);
}
public void list(Atom[] a) {
post("hello list " + Atom.toOneString(a) + "!");
outlet(0, a);
}
public void anything(String s, Atom[] args) {
post("hello anything " + s + " " + Atom.toOneString(args) + "!");
outlet(0, s, args);
}
}
The first thing to notice is that we no longer have a constructor method. It's not necessary to have a constructor if you are happy with the default one inlet and two outlets, and there's nothing else that needs to happen at the time of object instantiation.
The class above has six methods, each of which responds to a special type of Max message. The bang method will be called when an object receives a bang. The method posts a message to the Max console and then calls a method we didn't see in the previous example, MaxObject's outletBang, to send a bang out the object's first outlet.
The loadbang method will be called when a user opens a patch that contains an object of this class. This method is called after construction time, so it is safe to call the outlet methods from within, but in this case we simply post a pleasant welcome message.
The implementation of next two methods, both named inlet, is another example of overloading. The inlet(int) and inlet(float) methods will respond to single integers and floating-point numbers, respectively. Upon receiving an integer, a message that incorporates the input integer i is posted to the Max console, and then i is sent out the first outlet. A similar sequence of operations is executed upon receipt of a floating-point number.
The next method, list, will be called whenever a list is sent in an object's inlet. The argument a is an array of Atoms, and we've used one of the methods from the Atom class, Atom.toOneString(), to convert the individual Atoms into one long String for output to the Max console.
Finally, the anything method will respond to any message whose first element is a symbol that isn't the name of a public method declared elsewhere in the class - in this case, "bang", "inlet", and "list". The String s is this first symbol, and the args array of Atoms that follows can be of any length.
If a class does not define an inlet(int) but does define an inlet(float), incoming integers will be redirected to the inlet(float). If neither of these methods is defined, mxj will call list(Atom[]), if it exists. If it does not exist mxj will call anything(String, Atom[]). If a list is received but no list method is defined, mxj will call anything(String, Atom[]) with the String set to "list". If the anything method is not defined mxj will take the first element of the list and call either inlet(int) or inlet(float) depending on the type of this first element. If no appropriate method to call exists, mxj will post an error message to the Max console.
Example: |