[Solved] Avoid error already registered with add_child and remove_chil

I have a somewhat peculiar problem with add_child and remove_child.

The case is that I want to add the same add_child every time I change of track, at the time of opening the volume column (also to the contrary with remove_child, child nonexistent).I then use two observables to selected_track_index and volume_column_visible.With observables I have no problem.

But I have problems with add_child when it has already been registered and also with remove child , when it has not yet been registered (does not exist). In both cases I can return error.

The thing is, I need to put a condition that says (with “if then”):

  1. If exist add_child, do nothing. (avoid the error: …was already registered to this viewbuilder instance )
  2. And on the contrary, if the child does not exist, it does not remove_child. (avoid the error of it is not possible to remove a child who does not exist)

How to check both cases to avoid the 2 typical errors of registration of the child?

Here is an example of the code I use:

local GUI_VOLUME_LVL = vb:row{ id = "GUI_VOLUME_LVL", visible = false }
function add_child_volume()
  if ( vb.views["VPD_CBX_CLR_YW"].value == true ) then
    vb.views["GUI_VOLUME_LVL"].visible = true
    GUI_VOLUME_LVL:add_child( GUI_VOLUME_LEVEL )
  else
    GUI_VOLUME_LVL:remove_child( GUI_VOLUME_LEVEL )
    vb.views["GUI_VOLUME_LVL"].visible = false
  end
end

The childGUI_VOLUME_LEVEL = vb:row { bla bla bla }

What two conditions are needed to avoid these two errors?

  1. id = “GUI_VOLUME_LVL”

This is found in vb.views.GUI_VOLUME_LVL, so you can just check if that exists.

  1. Some small advise that might be helpful later on (don’t confuse this with your question… it’s just advise). If you find that you want to rebuild registered children that already exists, you will get the “already registered” error even if you have removed them. This is kind of a design flaw imo, but can easily be circumvented by 1) removing the view/gui object, 2) doing vb.views.my_view = nil , 3) rebuilding & adding it.

Another approach would be to maintain references to the various views as variables.

(just like you’re already doing with theGUI_VOLUME_LVL)

This way, you will not need to rely on ID’s, so you can avoid having to tear down the view before rebuilding it (left overs will be garbage collected soon enough).

I’d especially recommend this approach when you are creating GUIs that does not necessarily have a fixed number of elements.

I can not get the tool to work properly using add_child and remove_child.In the end I decided to use the property visible = true or false.To attach or hide panels more simply.

I do not know what happens, but I always showed up vb.views.GUI_VOLUME_LEVEL ( or what is the same vb.views[“GUI_VOLUME_LEVEL”]) equal to nil, always, regardless of invoking the add_child or remove_child.Maybe if there was a “has_child” similar to the functioning of observables, this type of error would be avoided, although the query is inside the tool not inside Renoise.

I already have my new tool practically finished and running almost everything ok.But I agree with Joule that there may be a design flaw, which perhaps could be revised, it gives the feeling that sometimes it does not behave as it should, as if invoking the remove_child, is executed, but is not well notified internally, or something similar.As it is an unclear matter,I prefer not to use it.

Thanks for the help! :smiley:

I’ve had no problem using add_child or remove_child at all (other than the very minor problem I mentioned). It’s most likely just a matter of structuring the code correctly… or perhaps this:

You cannot toggle the existence of a view with add_child / remove_child the way your snippet implies. I’m pretty sure you have to rebuild it after it has been removed and you want to add it again. So basically:

  1. Build the view just before add_child’ing it.

  2. use the vb.views.my_view=nil trick right after removing it.

Then everything should be fine.

I have problems with add_child and remove_child wanting to invoke them fromdifferent tracks and its observables (for wanting to use the same children for all the tracks a rare invention of mine ^_^)… Then I make a mess…

Since in my tool they are only 3 panels, I can place them fixed and hide them with visible = true/false.