Trouble 'while' coding

bennyfromtheblock's icon

Hi,

I've recently started developing my own externals but have stumbled across a problem that I find very strange. If I use a 'while' statement within any function within an external, it either causes Max to crash or causes nothing to happen when an inlet associated with that function receives a message. If anyone knows anything about this please let me know.

Also is it just me or is some of the code in the MaxAPI reference incorrect/ inconsistent? For example in the 'Scheduler' chapter under heading 2 of 'Creating and using Clocks' it has a line of code that reads:

sched_getftime(&time);

In the list of functions for use with clocks here is no such function.
There is however the function 'scheduler_gettime' or 'clock_getftime'. When I use either of these my code gives me no errors. It took me over a week to realise this because xcode was giving me an error saying that I was trying to link this function from a .o file of the same name as my project so I never thought to check. Maybe I'm missing something?

I am quite new to coding in general so please go easy on me.

Benny

cap10subtext's icon

While just doesn't work in a max object, because you are breaking away from the scheduler and you will cause max to hang. My (albeit beginner) understanding is that while tells the program "don't do anything but this loop until something X happens". You usually want max to do something only when it receives a bang or message.

Other than that I'm not sure about your other problem.

bennyfromtheblock's icon

Ah I see, I was trying to use it to constantly compare two inlets. I'll have to find another way I guess. I'm an uber noob when it comes to coding so I'm sure there is some normal way of doing this.

Thanks for the advice though mate, you've probably saved me a few more hours of rearranging lines of code to no avail.

cap10subtext's icon

Oh, yeah, check out he API 5.1.7 section 4.1 Creating inlets. Remember the Max Convention. Set a value with the right cold inlet, compare with the left hot and output. You can make both hot explicitly by making the function from the right inlet act like the left (and output on input).

bennyfromtheblock's icon

I'll check that out immediately, I must have missed that/ failed to understand it when I read that section before. Thanks once again for the help, your obviously a very helpful kinda chap!

andrea agostini's icon

Just a clarification:

there is nothing wrong with the 'while' keyword! It's just a way of making an iteration in C, along with its siblings for and do...while, and there is not much interesting stuff you can do in computing without using iterations. So, it's perfectly ok using 'while' inside a Max external - don't give it up!

best,
aa

bennyfromtheblock's icon

Also interesting....I have been using if and do without any problems whatsoever, I will keep at it as you say. Thank you very much!

cap10subtext's icon

I'd never had problems with anything but while statements. Could just be coincidence, as I said I am also a beginner at this. Could just be in the way I was trying to use it.

Emmanuel Jourdan's icon

You won't have any problem with the while loop it ends ;-)