# Saving a preset?



## Sasje (Jul 5, 2013)

Let's say I want to save all knob settings in a instrument with the click of a button, I want it to save into a text-file in the resource folder as an array/set or something, which we then can read inside the instrument by loading the text-file with the array/set in it. Can this be done? 

I wonder if it's possible and how you would do it?


----------



## polypx (Jul 5, 2013)

Yes it is possible. It's not a text file exactly, but you can open it in a text editor, it's an .nka file.

Use save_array and load_array.


----------



## Sasje (Jul 6, 2013)

Ah yes indeed. The NKA. Now I remember seeing something like this in Bob's scripts. I'll look it up. Thanks!


----------



## Sasje (Jul 8, 2013)

Okay, I got it.

But now I got another problem. I can save a slider value, however on loading the NKA, the slider value is only loaded to the slider itself, not the modulator itself.

The script needs the tonemachine setting with a formant knob. The formant knob will not get the value of the slider.

What am I missing? or do I assign the value of slider to the formant knob as well? 


```
on init 
declare %save[1] 
declare $sync_id 
declare ui_slider $Slider (0,100000) 
declare ui_menu $Menu 
declare $tmp
declare const $GFX := -1		 
declare const $GROUP := 0

add_menu_item($Menu,"Save",0) 
add_menu_item($Menu,"Load",1) 

end on 

on async_complete 
 if ($NI_ASYNC_ID = $sync_id) 
  $sync_id := -1 
end if 
end on 

on ui_control ($Menu) 
select ($Menu) 
   case 0 
   save_array (%save,0)   
   case 1 
   $sync_id := load_array (%save,0) 
   while ($sync_id # -1) 
   wait (1) 
  end while     
  $Slider := %save[0] 
end select 

end on 
on ui_control($Slider) 
	set_engine_par($ENGINE_PAR_FORMANT,$Slider,$GROUP,0,$GFX)
	$tmp := get_engine_par($ENGINE_PAR_FORMANT,$GROUP,0,$GFX)
   %save[0] := $Slider 
end on
```

In that case, it would be pretty tedious, and that means I need to do this:



> on ui_control ($Menu)
> select ($Menu)
> case 0
> save_array (%save,0)
> ...



Or is there a much easier way?


----------



## mk282 (Jul 8, 2013)

You need to execute all the code that's linked to UI callbacks after you load the values from the NKA.


----------



## argitoth (Jul 19, 2013)

need a little help with this, what am I missing? key.E[] holds ui_id for all controls in question, key.E.value[] should be holding all the values of those controls. why it no work? (I'm not getting any errors, it just isn't doing anything even though the dialogue box for saving and loading pops up..).

oh yeah, and call refresh_keyboard gets all the values of the controls and assigns key colors.


```
macro declare_menu(#name#,#items#)
  declare ui_menu #name#
  declare const #name#.items := #items#
  declare !#name#.item[#name#.items]
end macro

on init
  declare_menu(preset.M,2)
  preset.M.item[0] := "Load"
  preset.M.item[1] := "Save"
  for i := 0 to preset.M.items-1
    add_menu_item(preset.M,preset.M.item[i],i)
  end for
end on

on ui_control (preset.M)
  select preset.M
    case 0  
      load_array (key.E.value,0)
      set_control_par(key.E[i],CONTROL_PAR_VALUE,key.E.value[i])
      message("load array")
    case 1 
      for i := 0 to 51
        key.E.value[i] := get_control_par(key.E[i],CONTROL_PAR_VALUE)
      end for
      save_array (key.E.value,0)
      message("save array")
  end select
  call refresh_keyboard
end on
```


----------



## argitoth (Jul 20, 2013)

Alright, I got it working, you MUST involve a timer, take a look at the Kontakt 4 KSP Reference Manual. What happens is that, without a timer, the array gets loaded after a split second. Didn't have time to open the file? The array is sent to the controls before you even hit "Open" and that's the problem.

SOOO I'm wondering.. must I have a manual refresh button to actually load the array after the file has been selected? Seems like there's no way of telling Kontakt to wait until user has hit "open".


----------



## mk282 (Jul 21, 2013)

If dealing with arrays, and if using K5, you will definitely want (and should!) use the async callback!


----------

