Beginner question Lua .Function as keybinbing

beginner question here .
as an example i’m using simple but useful "Beatslaughter’s ClearSelection tool.

_AUTO_RELOAD_DEBUG = true

renoise.tool():add_keybinding {
name = “Pattern Editor:Selection:Clear”,
invoke = function()
renoise.song().selection_in_pattern = {}
end
}

renoise.tool():add_keybinding {
name = “Phrase Editor:Selection:Clear”,
invoke = function()
renoise.song().selection_in_phrase = {}
end
}

it works of course as it should . you assign key command to it and it clears selection as per design .
i experimented by substituding renoise.song().selection_in_pattern = {} with renoise.song():redo()
and it workes but if i substitude renoise.song().selection_in_pattern = {} with any other function
for example renoise.song():delete_track_at(index) i get error in LuaTestPad below

*** main.lua:6: variable ‘index’ is not declared
*** stack traceback:
*** [C]: in function ‘_error’
*** [string “local mt = getmetatable(_G)…”]:29: in function <[string “local mt = getmetatable(_G)…”]:24>
*** main.lua:6: in function main.lua:5

you can test it yourself ! why is that happening ?

Thank You
GC

What is setting the value of index? Is it a valid value for the current song?

could you give example of how it should be by correcting code below ?
song with say 3 tracks and i just want to delete 1 track out of 3 .
if my question makes no sense then why ? whatever your answer would be it will push me further ahead

_AUTO_RELOAD_DEBUG = true

renoise.tool():add_keybinding {
name = “Pattern Editor:Selection:Clear”,
invoke = function()
renoise.song():delete_track_at(index)
end
}

the issue here is that you need to tell it what the index is.
so if you want to delete the track at “where your cursor is at”, instead of index you need to put in renoise.song().selected_track_index
that way it’ll delete the selected track that is the one you’ve selected.

i hope this helps.

do let us know what you’re specifically trying to implement?

here’s the corrected code. if you want to delete the track at the selected_track_index - you provide it the selected_track_index. but if you are trying to delete the tracks that you have selected (you might have selected 5 tracks with the selector) - then you need to first detect which tracks are selected, and then run the deletion based on those indexes. i simplified this for you anyway so that it just deletes the currently selected track. if you’re looking for “mark selection, delete tracks”, then that’s a different beast altogether.

hopefully this will get you started anyway.

_AUTO_RELOAD_DEBUG = true

renoise.tool():add_keybinding {
name = “Pattern Editor:Delete Currently Selected Track”,
invoke = function()
renoise.song():delete_track_at(renoise.song().selected_track_index)
end
}