# Little problem with while loop



## d.healey (Oct 20, 2012)

Hello, 

The while loop below increases a counter 'i' and displays it via message(i) when a key is held down. So far so good. But when I hold more than one key down the rate at which i is incremented increases, this is not what I want. I'm sure this is easily solved but I just can't work it out. Any ideas?


```
on init
  declare $_i := 0
end on

on note
  ignore_event($EVENT_ID)
  $_i := 0
  while ($NOTE_HELD=1)
    message($_i)
    inc($_i)
    wait(1000000)
  end while
end on
```


----------



## TGV (Oct 20, 2012)

Is this what you're looking for?

polyphonic $ (polyphonic variable)
• A polyphonic variable acts as a unique variable for each executed event, avoiding conflicts in callbacks that are executed in parallel, for example on note and on release.


----------



## Raptor4 (Oct 20, 2012)

You have multiple note callbacks per each note. Another alternative is to declare a note count and set it to work like this:

```
on init
  declare $_i := 0
  declare $n_count
end on

on note
  inc($n_count)
  ignore_event($EVENT_ID)
  if ($n_count<2)
    $_i := 0
    while ($NOTE_HELD=1)
      message($_i)
      inc($_i)
      wait(1000000)
    end while
  end if
end on

on release
  dec($n_count)
end on
```


----------



## d.healey (Oct 20, 2012)

Thanks Raptor, that seems to be the solution I was after. 

TGV thanks for your input, I'm trying to avoid the use of polyphonic variables in the scipt I'm writing which is monophonic - the sample script I posted was just to demonstrate a problem i'm having in a more complex script.


----------



## TGV (Oct 20, 2012)

Polyphonic variables do not imply polyphony! They only are there to store different values for different note events.

My first thought was along the lines of Raptor, but I'm not sure about Kontakt's handling of concurrency (parallelism, multi-threading, however you want to call it). Under certain conditions it might be possible to increase $n_count by 2 before reaching the if. The result would be that no note plays.


----------



## d.healey (Oct 21, 2012)

TGV @ Sun Oct 21 said:


> Polyphonic variables do not imply polyphony!



This I understand, but using a polyphonic variable will make it difficult to track exactly which note event is in the variable at a given time, or so I believe, which will make things tricky in my more complex script.

Also I tried changing the variable 'i' to polyphonic and this didn't solve the problem. :(

The potential problem you mentioned with Raptor's script is an issue for me that I have experienced when trying his solution in my more complex script. :( So I am still looking for a water tight solution.


----------



## ScoringFilm (Oct 21, 2012)

This could be a solution without using a loop:


> *on init*
> ``*declare* ui_label info_label(1, 1)
> ``set_text(info_label," note(s)")
> ``*declare* $chord_time
> ...



A similar issue was discussed here:

http://www.vi-control.net/forum/viewtop ... t=#3628680

Hope it helps,

Justin


----------



## d.healey (Oct 21, 2012)

Thanks for your input Justin. I've had a look through your code, which works perfectly, but doesn't do what I need.

A loop is the issue so taking out the loop doesn't achieve what I want. I want a loop that increments a counter once per second as a note is held, and if I hold down more than one note the counter speed doesn't increase.


----------



## Raptor4 (Oct 21, 2012)

> with Raptor's script is an issue for me that I have experienced when trying his solution in my more complex script.


Two things are important:
1. Be very precised in the note CB order (inc the functions if any in that order) and conditions.
2. Use unique variable declarations for each counter, "i", loops etc.
Regards

BTW: One more thing:
You can try to replace *while ($NOTE_HELD=1)* with *while ($n_count>0)*.


----------



## d.healey (Oct 21, 2012)

I think I have solved it! :D



```
on init
  declare $CallBackID
  declare $_i := 0
end on

on note
  ignore_event($EVENT_ID)
  $_i := 0
  $CallBackID := $NI_CALLBACK_ID
  while ($NOTE_HELD=1 and ($CallBackID=$NI_CALLBACK_ID))
    inc($_i)
    message($_i)
    wait(1000000)
  end while
end on
```


----------



## TGV (Oct 21, 2012)

If I interpret it correctly, it will stop the while loop when you hold a second note.


----------



## d.healey (Oct 21, 2012)

Try it out. If you press a key the loop starts, if you press another key that loop stops and another one starts


----------



## Raptor4 (Oct 21, 2012)

> If you press a key the loop starts, if you press another key that loop stops and another one starts.


That's correct in case of using the $NI_CALLBACK_ID trick. Originally I was thinking that you need to execute a single callback ("i" loop) on the 1st note only after that you want to filter the rest callbacks which is a different scenario...
Anyway I'm glad that you have solved your problem.
Regards,
R4


----------

