How to know a caller into notify function?

Hello. I’ve a button list with the same notify handler (function) for all of them. Each button has own id. Is it possible to know - what button pressed inside handler function?? How can i send button id into?

Thank you

At the moment, the notifier function does not have any direct reference back to the button itself.

However, if you’re generating your buttons dynamically, then it’s actually quite trivial to work around the problem. You can simply have the button notifier pass the button id into another function, and then get a reference to the button via the vb.views[] table instead.

Here’s a simple example that you can run directly from the scripting editor:

-- Constants.
local BTN_COUNT = 10

-- Reference to ViewBuilder that we can use elsewhere.
local vb = renoise.ViewBuilder()

-- Function to handle button clicks.
local function on_btn_clicked(btn_id)
  
  -- Reference to the clicked button.
  local btn = vb.views[btn_id]
  
  -- Your totally awesome code goes here.
  renoise.app():show_message(string.format("clicked button id: %s", btn_id))

end

-- Function to build and show the GUI.
local function build_and_show_gui()

  -- Dialog content.
  local vb_content = vb:row {}
  
  -- For each button...
  for i = 1, BTN_COUNT do
  
    -- Unique button ID, ex: "button_1", "button_2", etc.
    local btn_id = string.format("button_%d", i)
  
    -- Create button.
    local btn = vb:button {
      id = btn_id,
      width = 50, 
      height = 50, 
      text = string.format("%d", i),
      
      -- Pass the button ID into your custom click handler.
      notifier = function()
        on_btn_clicked(btn_id)
      end
    }
    
    -- Add button to content.
    vb_content:add_child(btn)
  end
  
  -- Show ViewBuilder content.
  renoise.app():show_custom_dialog('Buttons!', vb_content)

end

-- Main.
build_and_show_gui()

To go even simpler, you could also do the following:

-- Some dynamically generated button id.
local btn_id = "button_1"

-- Create the button.
local btn = vb:button {
  id = btn_id,  
  notifier = function()
    -- Get a reference to the clicked button.
    local clicked_btn = vb.views[btn_id]
  end
}

I personally think it’s cleaner to pass the id into a custom function as in my first example, but you can of course do whatever :wink:

Yeah. It’s works! The way number one is more preffered to me. It’s so hard code on LUA after years of MOS6510 low level coding :slight_smile:

Thank you.