# Dealing with - Script Warning: attach_zone(): zone not found - problem



## yannistzav (Oct 17, 2016)

Hi guys,

I'm trying to fit a waveform in an instrument and I get the aforementioned problem (only affects the visual result) when pressing blank (without samples) keys.
I have tried setting text to blank for those keys ("") - but the script warning comes at top. I also put a blank sample of 2 secs in the mapping editor in these keys and the problem continues to exist. After that I don't even know what separates normal keys from the blank ones in order to get this message. I am being lost somewhere between the declarations.
The code I compiled (taken from the manual and some scripts here in the forum) is the following;


```
on init
    declare ui_waveform $waveform(1,1)
    move_control_px(waveform, 12,22)
    set_control_par(waveform,$CONTROL_PAR_HEIGHT,95)
    set_control_par(waveform,$CONTROL_PAR_WIDTH,200)
    set_ui_wf_property($waveform, $UI_WF_PROP_PLAY_CURSOR, 0, 1)
    attach_zone($waveform, find_zone("Zone"),$UI_WAVEFORM_TABLE_IS_BIPOLAR)

    declare $zzone
    declare $b
    declare $c
end on

on note
    fade_out($EVENT_ID,0,0)
    wait(10000)

    $zzone := get_event_par($EVENT_ID,$EVENT_PAR_ZONE_ID)
    attach_zone($waveform,$zzone,$UI_WAVEFORM_TABLE_IS_BIPOLAR)
    $b := get_sample_length($zzone)
    wait(10000)
   
    while ($NOTE_HELD = 1)
        $play_pos := get_event_par($EVENT_ID,$EVENT_PAR_PLAY_POS)
        set_ui_wf_property($waveform,$UI_WF_PROP_PLAY_CURSOR,0,$play_pos)
        wait (10000)
    end while
end on

on release
    set_ui_wf_property($waveform,$UI_WF_PROP_PLAY_CURSOR,0,$c)
end on
```


Thank you in advance for everything!


----------



## Lindon (Oct 17, 2016)

why not test for the blank keys themselves and only attach the zone for "real" keys?


----------



## yannistzav (Oct 17, 2016)

Lindon said:


> why not test for the blank keys themselves and only attach the zone for "real" keys?


Hi Lindon,
Thanks for taking the time.
How should I attach the zone for the real keys? Is there any way like the "case" command in order to set which keys are active and which are not? Didn't exactly get what you meant..


----------



## EvilDragon (Oct 17, 2016)

I'd put if (in_range($EVENT_NOTE,<min key>,<max key>) around the whole note and release callback, so that it only gets executed for the mapped keys (provided you have mapped samples contiguously, with no empty keys between min and max key).


----------



## yannistzav (Oct 17, 2016)

Hi EvilDragon,
What you said works.
But if we have multiple ranges (I have 43 to 46 and 48 to 57 and 60 to 69 and 72 to 81), then how can we add each other?
Or maybe we could define our full range and subtract notes which are empty?
You got any idea?
Thanks for your valuable feedback!


----------



## d.healey (Oct 17, 2016)

Pretty sure this is how I solved it

```
{/**
* Tests if the given MIDI note number's zone has a sample mapped to it. It does this by playing a silent note and checking if a note ID is generated.
*
* @param  note_num  MIDI note number of the zone to check
* @param  group  Specifiy the group(s) to check the zones of. < 0 = All groups

*/}
taskfunc kt.detect_zone(note_num, group) -> return

  declare $id

  $id := play_note(note_num, 1, 0, 1) {Play blip note}
  change_vol($id, -200000, 0) {Silence note}

  tcm.wait(1) {Wait a mo}

  if (group > 0) {If a group is passed}
  set_event_par_arr($id, $EVENT_PAR_ALLOW_GROUP, 0, $ALL_GROUPS) {Disallow all groups}
  set_event_par_arr($id, $EVENT_PAR_ALLOW_GROUP, 1, group) {Enable passed group}
  end if

  if (get_event_par($id, $EVENT_PAR_ZONE_ID) = -1)
  return := 0
  else 
  return := 1
  end if

end taskfunc
```


----------



## willbedford (Oct 17, 2016)

d.healey said:


> Pretty sure this is how I solved it
> 
> ```
> {/**
> ...


It seems quite unnecessary to use a taskfunc for this...


----------



## EvilDragon (Oct 17, 2016)

yannistzav said:


> But if we have multiple ranges (I have 43 to 46 and 48 to 57 and 60 to 69 and 72 to 81), then how can we add each other?



You can chain in_range() using "or"...



```
if (in_range($EVENT_NOTE,43,46) or in_range($EVENT_NOTE,48,57) or ...)
```


----------



## d.healey (Oct 17, 2016)

willbedford said:


> It seems quite unnecessary to use a taskfunc for this...


Yep, probably is, I always use taskfuncs automatically unless there is a reason I can't. I haven't ran into any problems yet though


----------



## EvilDragon (Oct 17, 2016)

There's no reason to use them unless concurrency needs to be handled, just bloats the code if it's not doing anything built-in functions can't handle.


----------



## willbedford (Oct 17, 2016)

The suggestions so far seem overly complicated. Since you're already getting the sample length, I would just use that to determine if the zone exists. If the length is greater than 0, there is a sample on the key.

```
on note
    fade_out($EVENT_ID,0,0)
    wait(10000)

    $zzone := get_event_par($EVENT_ID,$EVENT_PAR_ZONE_ID)
    $b := get_sample_length($zzone)
    if($b > 0)
        attach_zone($waveform,$zzone,$UI_WAVEFORM_TABLE_IS_BIPOLAR)
      
        wait(10000)
      
        while ($NOTE_HELD = 1)
            $play_pos := get_event_par($EVENT_ID,$EVENT_PAR_PLAY_POS)
            set_ui_wf_property($waveform,$UI_WF_PROP_PLAY_CURSOR,0,$play_pos)
            wait (10000)
        end while
    end if
end on
```
This also means you won't need to change any values if you move the zones to different keys, as you would with Mario's method.


----------



## yannistzav (Oct 17, 2016)

Hi David. It took me a while to figure yours, but it works like a charm as well. It was also a nice guide for general scripting purpose as I wasn't aware of the silent notes.
However, I went with Evil's plan which requires simpler coding and I guess I won't get confused in the future scripting of that instrument.

Any member of the forum having similar problems in the future, just follow either Evil's or David's guide and should be good to go.

Edit; And Will's reply also stands. I should have thought of that... So simple..

Thank you all for your replies!


----------



## EvilDragon (Oct 17, 2016)

Also I'm not sure that you really want that 10 ms delay right at the start of note callback... To get zone info all you need is a small wait(1) after play_note()...


----------

