Go Back   Cockos Incorporated Forums > REAPER Forums > ReaScript, JSFX, REAPER Plug-in Extensions, Developer Forum

Reply
 
Thread Tools Display Modes
Old 10-07-2019, 08:34 AM   #1
Thonex
Human being with feelings
 
Join Date: May 2018
Location: Los Angeles
Posts: 1,719
Default Script: 16 User definable External State Variables to be used by other scripts

Hi guys,

I wrote this script because I sometimes have scripts that work together and I sometimes need to have a centralized place to update certain variable values. So I wrote this script as a sort of 16 variable bucket to store the variables.

You gurus out there may want to look at the code.... I'm pretty sure it may be able to be optimized better. Or maybe you gurus have a better way to centralize variables... but anyway... here goes..

Maybe one of you will find it useful.



Here is the code to store the variables and also verify them (just hit cancel if you want to jus to see the values without changing them):

Code:
--[[
Written by Thonex

Creats 16 variables that can be called by other scripts when using:

"AK Get and Set External Vars", "Values" combined with 

GET_EXT_STATE()
SPLIT_STRING(inputstr, sep)

>>>>>> Var[1-16] would be what you use on another script after calling
>>>>>> the GET_EXT_STATE() and SPLIT_STRINGS() functions.
--]]


function Msg (param)
  reaper.ShowConsoleMsg(tostring (param).."\n")
end



function SET_EXT_STATE() -- sets the values to the system
    local str = table.concat(vals, ",")
    --                  Section (script name)   Item name  Val.  Keep when Reaper is closed
    reaper.SetExtState("AK Get and Set External Vars", "Values", str, true)    
end

function GET_EXT_STATE() -- gets the values from the system
   
    local str = reaper.GetExtState("AK Get and Set External Vars", "Values")
    
    if str and str ~= "" then
        vals = SPLIT_STRING(str, ",")
    end   
end


function SPLIT_STRING(inputstr, sep) -- Splits string example:  vals = SPLIT_STRING(retvals_csv,",")
    if sep == nil then
            sep = "%s"
    end
    local t={}
    for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
            t[#t+1] = str                          
    end
    --=================== THIS IS WHERE YOU STORE THE TABLE VALUES TO INDVIDUAL Vars or an Array
    for i = 1, 16 do
    Var[i] = t[i] -- this is an array represetnation of the string
    end
    --=================== END OF THIS IS WHERE YOU THE TABLE VALUES TO INDVIDUAL Vars or an Array
    return t
end


function STORE_GET_EXT_VARS ()
    
    reaper.ClearConsole ()
    
    Var = {}
    
    --<<<<<<<<<<<<<<< USER CAN DEFINE THEIR OWN VAR DESCRTIONS BETWEEN QUOTES (" ") >>>>>>>>>>>>>>>>>    
    var_list_t = {
                "var 1: Delete Items < than (Sec)",
                "var 2: Duplicate Nth",
                "var 3: Select X (interval)",
                "var 4: How many (N)",
                "var 5: Start at J (0 index N of x)",
                "var 6: User Definable",
                "var 7: User Definable",
                "var 8: User Definable",
                "var 9: User Definable",
                "var 10: User Definable",
                "var 11: User Definable",
                "var 12: User Definable",
                "var 13: User Definable",
                "var 14: User Definable",
                "var 15: User Definable",
                "var 16: User Definable"}
    --<<<<<<<<<<<<<<< END OF USER CAN DEFINE THEIR OWN VAR DESCRTIONS BETWEEN QUOTES (" ") >>>>>>>>>>>>>>>>>
    
    GET_EXT_STATE()
    
    ------ check to make sure there are some values in Ext State ----------
    local val_str 
    if #vals == 0 then
        val_str = "0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0"
    else
        --        Returns a contiguous table as a string using the second
        --        as a separator
        val_str = table.concat(vals, ",")
        --Msg(val_str)
    end
    
    ------ End of check to make sure there are some values in Ext State ----------
    
    var_list_str= table.concat(var_list_t, ",") -- creates a string to be used in GetUserInputs
    
    --var_list_str = "Var 1:,Var 2:,Var 3:,Var 4:,Var 5:,Var 6:,Var 7:,Var 8:,Var 9:,Var 10:,Var 11:,Var 12:,Var 13:,Var 14:,Var 15:,Var 16:"
    retval, retvals_csv = reaper.GetUserInputs( "External State Variables", 16,var_list_str, val_str )
    
    if retval == false then return end
  
    vals = SPLIT_STRING(retvals_csv,",") -- need to split string in order to Set extrernal state

    SET_EXT_STATE()
    
    --[[
    Msg(reaper.GetExtState("AK Get and Set External Vars", "Values"))
    for j=1, 16 do
    Msg(Var[j])
    end
    --]]
end

STORE_GET_EXT_VARS ()

Here is the code to block needed to pre-pend into your scripts to read the variable. Var[1-16] would be what you use on another script after calling the GET_EXT_STATE() and SPLIT_STRINGS() functions.:

Code:
function Msg (param)
  reaper.ShowConsoleMsg(tostring (param).."\n")
end

--######################### This is used for reading Ext State Variales ##########################
--[[
function SET_EXT_STATE() -- sets the values to the system
    local str = table.concat(vals, ",")
    --                  Section (script name)   Item name  Val.  Keep when Reaper is closed
    reaper.SetExtState("AK Get and Set External Vars", "Values", str, true)    
end
--]]
function GET_EXT_STATE() -- gets the values from the system
   
    local str = reaper.GetExtState("AK Get and Set External Vars", "Values")
    
    if str and str ~= "" then
        vals = SPLIT_STRING(str, ",")
    end   
end


function SPLIT_STRING(inputstr, sep) -- Splits string example:  vals = SPLIT_STRING(retvals_csv,",")
    if sep == nil then
            sep = "%s"
    end
    local t={}
    for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
            t[#t+1] = str                          
    end
    --=================== THIS IS WHERE YOU STORE THE TABLE VALUES TO INDVIDUAL Vars or an Array
    for i = 1, 16 do
    Var[i] = t[i] -- this is an array represetnation of the string
    end
    --=================== END OF THIS IS WHERE YOU THE TABLE VALUES TO INDVIDUAL Vars or an Array
    return t
end

function GET_EXT_VALS () 
    
    Var = {}
    
    GET_EXT_STATE()

    --Msg(Var[1]) --<<<<<< Var[1-16] are valid variables to assign to your script
   
end
--###################### End Of This is used for reading Ext State Variales ########################

GET_EXT_VALS ()
__________________
Cheers... Andrew K
Reaper v6.80+dev0621 - June 21 2023 • Catalina • Mac Mini 2020 6 core i7 • 64GB RAM • OS: Catalina • 4K monitor • RME RayDAT card with Sync Card and extended Light Pipe.

Last edited by Thonex; 10-09-2019 at 05:24 PM.
Thonex is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -7. The time now is 01:01 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.