What's new

Saving presets, working with arrays and resource container!

audiothing

Active Member
I'm working on a script for one of my new instruments. I have few questions about presets, arrays and resource container.

For the preset system I used arrays and the functions save_array/load_array. First I used one array per preset, then I switched to one array per parameter to clean the script a bit. Works as I wanted without problems.
Then I saw this on the KSP manual:
declare %presets[10*8] := (...
{1} 8,8,8,0, 0,0,0,0,...
{2} 8,8,8,8, 0,0,0,0,...
{3} 8,8,8,8, 8,8,8,8,...
{4} 0,0,5,3, 2,0,0,0,...
{5} 0,0,4,4, 3,2,0,0,...
{6} 0,0,8,7, 4,0,0,0,...
{7} 0,0,4,5, 4,4,2,2,...
{8} 0,0,5,4, 0,3,0,0,...
{9} 0,0,4,6, 7,5,3,0,...
{10} 0,0,5,6, 4,4,3,2)

2D Arrays, right? I can't figure out how to add and recall dynamically the data from these arrays. It would help a lot having one 2D array for storing all presets. Tips/help on this?

Anyway, not a big problem working with several arrays. Now I just want to store the data into the resource container (previously created) but, when I call the save_array function, it creates a directory /Data/. Is there a way to save arrays directly into the resource container? If not, how can I have a preset system without saving external files?

Thanks in advance!
 
Basically it's a "virtual" 2D array. It means that it's still a 1D array with x*y indices.

You'll probably gonna have to have a pointer variable to select the range of values in the array which you're going to save to or load from.

Take this simple example: you have 4 parameters, and want to have 4 presets for it. Attached is a little example of how to do it.

Code:
on init
	declare $i
	declare $menusel
	declare %preset[4 * 4] := ( ...
{1}   0, 127,   0, 127, ...
{2}  64,   0,  64,   0, ...
{3} 127,  64,   0,  64, ...
{4}   0,  32,  64, 127)

	declare ui_menu $Preset
	add_menu_item($Preset,"Load Preset 1",0)
	add_menu_item($Preset,"Load Preset 2",1)
	add_menu_item($Preset,"Load Preset 3",2)
	add_menu_item($Preset,"Load Preset 4",3)
	add_menu_item($Preset,"---------------",-1)
	add_menu_item($Preset,"Save As Preset 1",4)
	add_menu_item($Preset,"Save As Preset 2",5)
	add_menu_item($Preset,"Save As Preset 3",6)
	add_menu_item($Preset,"Save As Preset 4",7)


	declare ui_knob $Knob1 (0,127,1)
	declare ui_knob $Knob2 (0,127,1)
	declare ui_knob $Knob3 (0,127,1)
	declare ui_knob $Knob4 (0,127,1)

	declare %ID[4]	{ declares an array which stores UI IDs for all parameters used in the preset }
	%ID[0] := get_ui_id($Knob1)
	%ID[1] := get_ui_id($Knob2)
	%ID[2] := get_ui_id($Knob3)
	%ID[3] := get_ui_id($Knob4)

	make_persistent($Knob1)
	make_persistent($Knob2)
	make_persistent($Knob3)
	make_persistent($Knob4)
	make_persistent($menusel)

	message("")
end on

on ui_control ($Preset)
	if ($Preset = -1)
		$Preset := $menusel	{ don't ever show the divider line in menu, show previous entry instead }
	else
		$menusel := $Preset	{ save currently selected menu entry }
	end if

	if ($menusel < 4)	{ loading presets }
		$i := 0 { preset number * number of parameters per preset - so the while loop starts always at first parameter of selected preset }
		while ($i < 4)	{ add the number of parameters per preset to delimit how many parameters are gonna be loaded }
			set_control_par(%ID[$i],$CONTROL_PAR_VALUE,%preset[($menusel * 4) + $i])	{ load the saved value for all parameters }
			inc($i)
		end while
	else				{ saving presets }
		$i := $menusel mod 4	{ pointer to the first index used in array for the selected preset slot - 0, 1, 2, 3 }
		while ($i < ($menusel mod 4) + 4)	{ add the number of parameters per preset to delimit the range of indices that are gonna be updated by saving procedure }
			%Preset[$i] := get_control_par(%ID[$i],$CONTROL_PAR_VALUE)	{ get the parameter value and save it }
			inc($i)
		end while
		wait(500000)	{ wait a bit... }
		$Preset := $menusel mod 4	{ restore the menu to the preset slot that has just been updated }
		$menusel := $Preset	{ refresh the menusel value, too }
	end if
end on

I think you can't put the Data folder in Resource container.
 
Oh yes, stupid I, forgot that. Silly mistake, sorry!


Well, I, for one, think the new load/save array functionality is great - makes it easier to export patches without resaving the NKI.
 
Top Bottom