-1

I make a function that contains if in NI Kontakt:

on init
    message(Add(1,2))
end on


function Add(x,y) -> output
    if x > 0
        output := x + y
    else 
        output := 0
    end if
end function

And I get the error message:

The definition of function Add needs to consist of a single line (eg. "result := ") in order to be used in this context

Ho do I make a function with if?

VVD
  • 178
  • 1
  • 9
mr_blond
  • 1,586
  • 2
  • 20
  • 52

1 Answers1

0

there are several things wrong. I'm sure reading some example code helps to avoid too much try-and-error with this exotic language. But that's probably done after almost 4 months? ;-)

Firstly you need to declare all variables in the on init and always use their corresponding prefix (for integers its "$") like so:

on init
    declare $x
    declare $y
    declare $output
end on

Secondly you can not call a function in the on init. For this example I use the on note callback that triggers every time you play a note. Additionally use "call" to execute a function.

on note
    $x := 1
    $y := 2
    call Add
    message($output)
end on

And lastly use brackets around your conditions:

function Add
    if ($x > 0)
        $output := $x + $y
    else
        $output := 0
    end if
end function

It is like in most programming languages important to declare all your functions before their execution. Since you can not use them in the on init, you can place this callback always on top followed by your functions.

This would be the full code:

on init
    declare $x
    declare $y
    declare $output
end on

function Add
    if ($x > 0)
        $output := $x + $y
    else
        $output := 0
    end if
end function

on note
    $x := 1
    $y := 2
    call Add
    message($output)
end on

Enjoy ;-)