Problem With Removing Notifiers

Depending on a preference value in my tool i want to dynamically enable/disable notifers for devices on each track. Here in the debug output one can see, that a notifier is added for each track successfully pointing all to the same function.

  
devices notifers enabled  
enabled: Kick  
enabled: Hats & Cymbal  
enabled: distortion  
enabled: help clap  
enabled: Clap  
enabled: Ethnic  
enabled: LoFi Break  
enabled: bassline  
enabled: acid  
enabled: pad  
enabled: dist 2  
enabled: psymelody  
enabled: arpeggio  
enabled: lead  
enabled: long verb + delay  
enabled: Mst  
enabled: bassline tweak  
enabled: bass  
devices notifers disabled  
*** std::logic_error: 'remove notifier: the given notifier function was not added.'  
*** stack traceback:  
*** [C]: in function 'remove_notifier'  
*** main.lua:132: in function 'init_notifiers'  
*** main.lua:274: in function 'preferences_gui'  
*** main.lua:49: in function <49><br>
<br>```

<br>
<br>
Here is my current notifier code:<br>
<br>

```lua<br><br>
--[[script initialization]]-----------------------------------------------]]--<br>
<br>
renoise.tool().app_new_document_observable:add_notifier (<br>
 function()<br>
	delete_snapshots()<br>
	init_notifiers()<br>
 end<br>
)<br>
<br>
function init_notifiers()<br>
<br>
 if not renoise.song().tracks_observable:has_notifier (<br>
 	function() delete_snapshots() end<br>
	) then<br>
<br>
 	renoise.song().tracks_observable:add_notifier (<br>
 	function() delete_snapshots() end<br>
 	)<br>
 end<br>
<br>
 if prefs.prf_devices.value then<br>
	print("devices notifers enabled")<br>
	for _, track in ipairs(renoise.song().tracks) do<br>
 	if not track.devices_observable:has_notifier (<br>
 	function() delete_snapshots() end<br>
 	) then<br>
<br>
 	print("enabled: " .. track.name)<br>
 	track.devices_observable:add_notifier (<br>
 	function() delete_snapshots() end<br>
 	)<br>
 	end<br>
	end<br>
 else<br>
	print("devices notifers disabled")<br>
	for _, track in ipairs(renoise.song().tracks) do<br>
--- if track.devices_observable:has_notifier (<br>
--- function() delete_snapshots() end<br>
--- ) then<br>
<br>
 	--- isn't executed after a change in the preferences?<br>
 	print("disabled: " .. track.name)<br>
 	track.devices_observable:remove_notifier (<br>
 	function() delete_snapshots() end<br>
 	)<br>
--- end<br>
	end<br>
 end<br>
<br>
end<br>
<br>```

<br>
<br>
The weird thing is, that if i include the commented out check for has_notfier, the remove never get's executed. That function was added before successfully, but why does has_notifier return false now instead of executing the remove stuff?</49>

I had this problem, too:

When you wrap something in between function() BLAH end, you are creating an anonymous function, which can never be accessed again.

Replace

  
renoise.tool().app_new_document_observable:add_notifier (  
 function()  
 delete_snapshots()  
 init_notifiers()  
 end  
)  
  

With something like.

  
renoise.tool().app_new_document_observable:add_notifier(delete_snapshots)  
renoise.tool().app_new_document_observable:add_notifier(init_notifiers)  
  

Doh, thanks! Have it solved now after some issues about not declared variables. LUA isn’t smart enough to lookup these callback functions in the same file, if they are written below the notifier addition. Basically had to move all my initialization to the bottom of the code and also change the order of adding stuff to something like this:

  
function init_notifiers()  
...  
end  
  
renoise.tool().app_new_document_observable:add_notifier(delete_snapshots)  
renoise.tool().app_new_document_observable:add_notifier(init_notifiers)  
  

Yeah, had this issue too. Kind of annoying that order is important like this.

1 Like

“{id=k, track=track}” creates an anonymous temp table, so you can’t use it to remove it again. Either locally store that ref or use non temp ref (a class instance, some other “global” table)

Just spent some time banging my head into a wall with this, so a solution snippet reverse engineered from a vv script:

--notifier function to be added to a lot of listed parameters/ tracks etc.   
function my_func(index)
  --do stuff
end

--global table declared as empty
my_func_tab = {}  

-----------------------------------------------------
--loop to populate table and add add/attach notifiers 
-----------------------------------------------------

for i = 1,#renoise.song().ABC___some_list do
  --now we can populate the global table with my_func which becomes anonymous with this syntax. However we can pass it the index `i` into the function this way.      
  my_func_tab[i] = function() my_func(i) end 
  --add notifer function via table  
  renoise.song().some_observable:add_notifier(my_func_tab[i]) 
end 

-----------------------------------------  
--later remove via table in similar loop 
--the table references are retrievable where the anonymous functions would not be here.
-----------------------------------------  
renoise.song().some_observable:remove_notifier(my_func_tab[i])