Anatomy of a script
7.04 Anatomy of a script:
This page illustrates the basic outline of a script utilizing the Lokasenna GUI and dh_Toolkit. The order of the steps are important to maintain. For a detailed description refer to dh_Template.lua. That is a highly commented bare-bones script that can also be considered as a tutorial.

7.04.01 Script sections and actions required:
Load Lokasenna GUI:
    Set the name to appear in the script's title bar.
      GUI.name = "dh_Template v1.0"
    Uncomment any elements that the script uses.

Load dh_Toolkit: Loads any saved settings required to define GUI.
    Set window size.
    Set the name to use for saving to reaper.extstate.ini
      Usually the script name.
    Uncomment any elements that the script uses.

My Data:
    Define script variables and data structures.
  
My Functions:
    Define your functions here.
      This will include any functions called from a function
      in the "Element Functions" section.
  
Element Functions:
    Define functions called directly by GUI elements.
      (These functions could just as well be put intp "My Functions
         but I like to keep them separate.)
           
ELEMENTS:
    Declare and/or load your elements here.
  
Method Overrides:
    Override element methods here.
  
GET PROJECT DATA:
    Modify if saving your data to reaper.extstate.ini
      The templates use the checklist "template_options" as an
        example of how to maintain and persist data.
        "template_options" (all references) can be safely renamed
        or removed. 
        
SAVE PROJ EXT STATE:
    Provide for saving GUI settings and/or script settings.
    This is a place to put anything you want to save persistantly.
        "template_options" is also used here.
  
EXIT:
    Things to do when exiting script, perhaps, saveProjExtState(0)
    On exit: (See Note 1)  

Script Initialize:
    The place to do any last minute script/element initializations.
    Load any saved settings used by script.
    Do any setup for GUI.
  
MAIN LOOP: userFunction()
    User function to be called on every update cycle.
    Anything that would go into a reaper.defer function would go here. 
    The name of the function can be any unique name. I call mine dhMain().
    
    Include within it any code you wish to be called on every cycle.
    I use it to check if a project tab has changed 
      then save the previous project's data 
      then update the script with the newly selected project's data.
    dh_Toolkit uses it to check if the script scale has changed
      then updates the GUI. DO NOT change this!

-- Open the script window and initialize a few things.      
GUI.Init( )  

-- Tell the GUI library to run userFunction on each update cycle.
GUI.func = userFunction  -- Name it to your liking.

-- How often (in seconds) to run GUI.func. 0 = every cycle.
GUI.freq = 0
       
-- Start the main loop
GUI.Main()      

Note 1:
Create a function to hold your exit code and assign it to reaper.atexit (the name doesn't matter but "Exit" says it all):
local function Exit()
    -- Your exit code --
end

reaper.atexit(Exit)

Alternatively you can use the following, as long as it appears before GUI.Init( ).
local function Exit()
    -- Your exit code --
end

GUI.exit = Exit

Basically, GUI.Init( ) assigns it by calling
if GUI.exit then reaper.atexit(GUI.exit) end

I prefer the first, more direct method.