Page tree
Skip to end of metadata
Go to start of metadata

onSave()

Description

This callback function is called when the script module is saved as part of a preset or project. The data you pass on to the return statement will be stored with the preset or project. The data can be of any type, but it is common practice to use a table that can easily be extended with more fields. When the script module is restored, the onLoad callback will receive the stored data.

Available in: Controller.

Return Values

The returned data will be stored as part of the preset or project.

Example

-- print the last three notes when calling onNote, onSave or onLoad
-- the last three notes are remembered even after restoring the program
 
local lastNotes = {}
 
function printLastNotes(callbackName)
    print("Last three notes when calling "..callbackName..":")
    for i = 1, 3 do
        if lastNotes[i] then
            print(i..":", "Note#:", lastNotes[i].noteNumber,", Velocity:", lastNotes[i].velocity)
        else
            print(i..":", "Note#: ---, Velocity: ---")
        end
    end
    print()
end
 
-- play some notes to fill the table
function onNote(event)
    postEvent(event)
    table.insert(lastNotes, 1, {noteNumber = event.note, velocity = event.velocity})
    -- store maximum three notes
    if #lastNotes > 3 then
        table.remove(lastNotes)
    end
    printLastNotes("onNote")
end
 
-- will be called when the program is saved  
function onSave()
    local data = {}
    data.lastNotes = lastNotes
    printLastNotes("onSave")
    return data -- any data in this table will be stored
end
 
-- will be called when the program is restored
function onLoad(data)
    lastNotes = data.lastNotes -- read the values from the data table
    printLastNotes("onLoad")
end

See Also: onLoad

1 Comment