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

onLoad(data)

Description

This callback function is called when the script module is loaded as part of a preset or project. The data that is passed on is the data that was returned by onSave when the script module was saved.

Available in: Controller

Arguments

dataThe data that was returned by onSave.

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: onSave

1 Comment