It is common to want to initialize nodes when they first appear on screen. It is also common to have branches of the node tree hidden (one example - tabs in TabContainer) until they are needed. What is a good practice approach to detecting when my node becomes not created, not added to the tree, but actually shown? Is there really not a more civilized way than to track visibility by hand by polling for is_visible_in_tree() on every frame?
Who makes nodes visible? It’s your code. So you always know when your code makes something visible. For things that can be made visible via ui interaction like tabs in a tab container, they always emit signals that notify you of any relevant state changes.
No, in fact. My code can only set or reset Visible on individual nodes, that may not at all be related to the one I’m interested in. There isn’t a way for me to control if a particular node is shown. The engine is the one that determines that, evaluating the overall state of the tree. Best I can do is go in after and replicate its work by checking visibility of every parent on the way to root, on every frame, for the entire duration that the node exists. Given that normally there are callbacks and signals for such things, this looks odd.
Can you describe your exact use case?
Btw both Node3D and CanvasItem, which are base classes for all 2d and 3d objects, emit visiblity_changed signal.
I would rather not, since the question isn’t about the exact case. I can come up with an ad-hoc solution that would just work myself. What I am looking for by asking the community is the good practices for a class of cases (or a confirmation that there isn’t one).
Then your question will be left unanswered, because the only credible answer that can be given with information you provided is: “it depends”
That is perfectly fine, if that is the case.
Have you looked at visibility_changed signal? It should do exactly what you ask for.
Yes, I did. It tracks the individual Visible flag, not the visibility in tree. Would have to again connect all parent nodes to get something conclusive out of it. Though that might be a bit better than polling.
No, it will take into account ancestor visibility state changes. It does exactly what you ask. The signal fires whenever perceptual visibility changes. Try to test it more thoroughly.
Yes it actually works, thank you. Exactly what I was looking for.
This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.