Page 1 of 1

min/max function needs its own handler?

Posted: Thu Nov 21, 2013 7:42 pm
by chris25
I am trying to limit speed. Not finding anything that discusses this I decided to follow dictionary example as best as I could. There is no indiaction at all that this min/max function needs a handler all by itself, considering that the whole script is actually in a handler and therefore the min/max function, being within that handler theoretically should work. This error message: card id 1002: execution error at line 10 (Handler: can't find handler) near "max", char 1 tells me otherwise. However I have no clue how to interpret this other than the max/min needs its own handler - but I do not understand then what to do.
EDIT: Ok stupid I discovered that you use 'Put' and so that returns ok, But, still the speed just gets faster and faster. So I am back to the same query, I can't get the speed set to a maximum despite fiddling.

chris

Code: Select all

on arrowKey theRudder
   if theRudder is "left" then
      put (gsubSpeedX - 0.1) into gsubSpeedX
         max(-0.1,min(gSubSpeedX,-0.3))
   end if
   if theRudder is "right" then
      put (gsubSpeedX + 0.1) into gsubSpeedX
   end if

Re: min/max function needs its own handler?

Posted: Thu Nov 21, 2013 10:24 pm
by SparkOut
max is a function, (and so is min) so you will need to send it some parameters and it will return a value. You need to do something with the value returned. The way you have it set is more like a command, which is why it's not working. It doesn't need it's own handler, the error message is telling you you can't use the max function like a handler.

You

Code: Select all

put (gsubSpeedX - 0.1) into gsubSpeedX
but then you need to fix the max (and min) value with

Code: Select all

put max(-0.1,min(gsubSpeedX,-0.3)) into gsubSpeedX
Although... I think you want to fix the range from -0.3 up to -0.1 right? Let's say gsubSpeedX was originally -0.3 and your first adjustment subtracted 0.1 giving -0.4, then your max/min will take it and return "the maximum of two values, either -0.1 or -0.4 (which is the minimum of another pair of values, either -0.4 or -0.3)"
As -0.1 is higher than -0.4 then you will get -0.1 returned by the max function (which nests a min function inside it).
So swap the min and max to

Code: Select all

put min(-0.1,max(gsubSpeedX,-0.3)) into gsubSpeedX
which returns "the minimum of two values, either -0.1 or -0.3 (which is the maximum of another pair of values, either -0.4 or -0.3)"
As -0.3 is higher than -0.4 it will return -0.3 from the nested max part, and as -0.3 is less than -0.1 it will return -0.3 from the min comparison too. So you have subtracted 0.1 from the speed but limited it so that it can't become less than -0.3

Where you are limiting values to a range, it's not that intuitive to see where the min and max apply - you might naturally think that you choose max when you want that to indicate the max value and min to choose the min value, but in fact you want the opposite - if the value is higher than the minimum limit - you're comparing the value against the minimum it can be so you want to pick the max of the choices. Make sense?

HTH

Re: min/max function needs its own handler?

Posted: Thu Nov 21, 2013 10:36 pm
by chris25
Hallo Sparkout I am eternally grateful for your explanation, now I need an evening to study this and Klaus, but I so appreciate this little tutorial, Big thankyou.