RonenNess / GeonBit.UI

UI system for MonoGame projects.
MIT License
477 stars 64 forks source link

Removing non-root entities (inside parent) #129

Closed dclipca closed 2 years ago

dclipca commented 2 years ago

Noticed this error when trying to use the ``UserInterface.Active.RemoveEntitymethod on an entity with a parent. It works on entities at the root level: GeonBit.UI.Exceptions.InvalidStateException: 'Child element to remove does not belong to this entity!'``` What's the canonical way to remove entities inside a parent? Do you really heave to remove it from the parent first?

dclipca commented 2 years ago

I think the canonical way to remove an item is to call .RemoveChild from the parent entity. Is it right? I thought it means detaching it from the parent but now I think it means really deleting it.

RonenNess commented 2 years ago

Not sure what you did there but the error is saying you are trying to remove an element from a parent, but the element is not a child of it.

Ie you can't do:

panel.RemoveChild(element);

unless you call

panel.AddChild(element);

first.

Regarding your second question - not sure what you mean - removing is just detaching you can do

panel.AddChild(elem);
panel.RemoveChild(elem);
panel.AddChild(elem);

As many times as you like and the element will end up working fine. You can ofc add it to other elements as well.

If you have any code to illustrate a bug in that mechanism please post it, meanwhile I'm closing.

dclipca commented 2 years ago

Not sure what you did there but the error is saying you are trying to remove an element from a parent, but the element is not a child of it.

Ie you can't do:

panel.RemoveChild(element);

unless you call

panel.AddChild(element);

first.

Regarding your second question - not sure what you mean - removing is just detaching you can do

panel.AddChild(elem);
panel.RemoveChild(elem);
panel.AddChild(elem);

As many times as you like and the element will end up working fine. You can ofc add it to other elements as well.

If you have any code to illustrate a bug in that mechanism please post it, meanwhile I'm closing.

What is not clear to me is what happens with the removed element entity. Is it deleted, cached etc. That's confusing.

RonenNess commented 2 years ago

'Remove' in c# just mean Remove, same as in geonbit.ui (for example see Remove behavior of the built-in c# collections - its all the same). And you don't really 'delete' objects in c#, they are managed by the GC.

dclipca commented 2 years ago

'Remove' in c# just mean Remove, same as in geonbit.ui (for example see Remove behavior of the built-in c# collections - its all the same). And you don't really 'delete' objects in c#, they are managed by the GC.

Aha I see. It's very convenient. Thank you!