Forums > Java

Starting out JAVA in MAX – send message directly to object

December 26, 2013 | 11:39 am

I have just installed eclipse and got it running with MAX:) I have used Javascript in the past but need something that performs better so this is why I am starting to use JAVA. I used to access objects directly from javascript using the following syntax:

objline1 = this.patcher.getnamed("sn_line1");
objline1.message(0,10);

Is it possible to send a message directly to an object using JAVA?



Lee
December 26, 2013 | 2:23 pm

Hi. No, this isn’t possible from Java, you need to send out of an outlet and route appropriately…


December 26, 2013 | 2:44 pm

Ok. Thanks for the advise. Do you happen to know of any good tutorials apart from the "Writing Max Externals in Java" pdf?

Also when sending to an outlet can I please ask you to advise how to format the message. I am sending the string below to a play object but it did not understand it.

import com.cycling74.max.MaxObject;

public class PlayFragment extends MaxObject {
public void bang() {
String str ="start 100 2000 1900";
post(str);
outlet(0,str);
}
}


December 27, 2013 | 1:32 am

Yo KMLL,

You need to parse the values you want to output as "Atoms" ( the generic datatype that Max uses ), build an Atom[] array from those values, then output the array.

This is how I go about it:

String someString = "allTheThings";
float someFloat = 0.4f;
Atom[] outputAtom = new Atom[] { Atom.newAtom ( someString ), Atom.newAtom ( someFloat ) };
outlet ( 0, outputAtom );

Chris.



Lee
December 27, 2013 | 1:40 am

yup, that.

As for additional tutorials, don’t know of any of hand, but didn’t look that much. found the pdf to be informative enough. i’m guessing if you hunt around for emamples of other people’s code that may help you.


December 27, 2013 | 2:29 am

Thank you both for the input:)

I studied the API in more detail yesterday and it seems that it is actually possible to send a message direcly to an object. Here is an example:

MaxPatcher p = this.getParentPatcher();
MaxBox mb = p.getNamedBox("sn_play1");//The script name needs to be set in the inspector
Atom[] outputAtom = new Atom[] { Atom.newAtom (200), Atom.newAtom (500),Atom.newAtom (300)};
mb.send("start", outputAtom);



Lee
December 27, 2013 | 3:22 am

well, thanks. i’ve learnt something new today also :)



Lee
December 27, 2013 | 3:40 am

btw, if you look in the java/classes directory under your MAX install, there are a number of java files in there that do basic things


December 27, 2013 | 4:15 am

Thanks for the pointer to the files:)


December 27, 2013 | 5:04 pm

KMLL, you wonderful person, you.

I’ve seen this question a few times and the response was always "it can’t be done". This will save me from having to route all over the place. Well done and many thanks!

Chris.


December 28, 2013 | 8:37 am

Yes, it’s possible to send messages between Java objects without using patch cords.

Doing so will make it well-nigh impossible to debug your patches unless you document the holy shit out of what you’re doing. Which you won’t.

Sending messages through patch cords means you have to draw the patch cords, but then you can see the connections in your patch. With invisible connections: you make one change, nothing works, and you can spend many long and tedious hours trying to figure out what’s gone wrong.

If you want your code to be reusable, if you’re working on a project that’s supposed to last and develop over more than a week, then stay away from this trick. But if your time is worth nothing at all, go ahead and be it on your head.-)


December 30, 2013 | 5:51 am

Thanks for the input on this thread you all. Glad I could help Chris:) Guess Peter has a good point but it is a matter of coding style I guess. Also I have to warn any readers that I found the API to be inadequate when interacting with certain objects e.g the line object but I have created a seperate thread about this issue. – Thanks

FYI

http://cycling74.com/forums/topic/issue-with-sending-message-to-line-object-using-java/


December 31, 2013 | 2:00 am

Also I have to warn any readers that I found the API to be inadequate when interacting with certain objects e.g the line object

there’s nothing wrong with the API in this regard.
you just have to know that a list, e.g. 20 200, is a message starting with the (mostly hidden) message header list. i.e. 20 200 and list 20 200 is basically the same.
so in java you can use the send(java.lang.String message, Atom[] args) method to send a list to a max object, like this:

int args[] = {20, 2000};
MaxPatcher p = this.getParentPatcher();
MaxBox mb = p.getNamedBox("sn_line1");
mb.send("list", Atom.newAtom(args));

hope that clears it up.


January 2, 2014 | 2:46 pm

As Volker said, you can use send(java.lang.String message, Atom[] args). There is also MaxSystem.sendMessageToBoundObject(java.lang.String sym_name, java.lang.String msg, Atom[] args)

As mentioned in your other thread about sending messages to line, it may be easier to use the overloaded methods for java primitive types than using the atom versions–performance is better too.

Yes, it’s possible to send messages between Java objects without using patch cords.

Doing so will make it well-nigh impossible to debug your patches unless you document the holy shit out of what you’re doing. Which you won’t.

I totally agree with Peter, but if you do document everything well, you can do many quite powerful things messaging between Java objects. Using the two methods mentioned above maybe have some performance and threading issues vs some of the methods available for using your MXJ’s outlets. You can even build stuff that doesn’t use the Max API but passes around references to Java objects if you really want to get into crazy town.


Viewing 14 posts - 1 through 14 (of 14 total)