how to write a funtion in MAX

TIENDUNG's icon

hi all,
I am beginer, so I need your help. I want to write a funtion in MAX, like write C (if.. then..) code in MAX to do some thing. How to do it???
thank you so much for your support
thank you

bkshepard's icon

Have you checked the help and reference files for the [if] object? They have all kinds of examples.

Luke Hall's icon

You can use a number of languages inside max if you feel more at home writing lines of code than connecting little boxes together. Java and javascript can be implemented using the [mxj], [js] and [jsui] objects which are included in the standard distribution. You can also use third-party externals to embed csound, chuck, ruby, python, lisp and a few others in the max environment, search http://www.maxobjects.com/ for user created objects.

If you are more at home with C then you should download the max SDK (it is linked on the C74 homepage) and then you can write your own custom objects to include in your patches. If you have questions about C/java/js stuff then post a question in the relevant developer forum and you're more likely to get a helpful response.

lh

Exit Only's icon

Yes, Max is a bit different in that the standard control and iteration functions don't translate exactly. Here are a couple of pointers-

You can encapsulate a set of commands in an patcher or abstraction.

An 'if then' clause can be created using the 'if' object, but once you get more comfortable you might find yourself using the comparator objects (such as >,

To do a operation repeatedly, use uzi. Uzi can output an index value for each bang, so this works kind of like a for loop. It even has a break command to stop iterating.

Read the tutorials and understand the order of operations! Do the max tutorials first, even if they seem boring (i want sound!) they will explain the control structures you will use with MSP.

Tj Shredder's icon

In 90% of all cases where a lines of code programmer is seeking for [if], the standard [split] object does the job better...
In all other cases gates could be used...
(I almost never need [if], but that's a matter of taste...)

Stefan

seejayjames's icon

Or [select] becomes your "switch" statement, allowing multiple if/then tests of what a given number might be, then doing something different for each. [route] goes a step further and can work with indexed lists. I also hardly ever use [if] but occasionally it works great for the task at hand. However, there are usually other ways you can do the same thing.

Sometimes the rightmost outlets of [select] and [route] are more useful than the others... they can allow filtering of a bunch of values easily, then dump out the unknown (but not matched) input value.

Remember that the humble "bang" can do absolutely anything and everything you want it to do, it all depends on what it connects to and in what order things happen. On that note, look into [trigger], one of the most useful and important objects... it not only keeps your logic flow proceeding in a certain order, it forces you to think about how the flow should go in the first place, which helps with your design. So the 2D representation in your patch is executed linearly, it just all happens so fast that it seems to be simultaneous. Keeping these two ideas in mind can help you organize your layout as well as how things interact, and in what order. Having experience in coding can be helpful as well as a hindrance when learning Max, but that's all in how open you are to new ways of programming and how well you understand the connections/dissimilarities between the two.

Peter Castine's icon

TIENDUNG wrote on Mon, 25 May 2009 05:03hi all,
I am beginer, so I need your help. I want to write a funtion in MAX, like write C (if.. then..) code in MAX to do some thing. How to do it???
thank you so much for your support
thank you

Take an afternoon to work through the first dozen or so tutorials. That will take care of this and most of your other questions.

Seriously. If you don't take the time to work through the tutorials, or some of the other documentation, you're going to be left with dozens of questions like this.