I’m attaching my app states in a certain order (let’s say A before B) since they have some dependencies (B depends on A) so that the latter attached app state will be able to lookup it’s dependency via getState(A.class) via the AppStateManager. An both states implement a tight cleanup() method detaching all added Nodes and Geoms etc and deleting all references used while the app state was attached. Since B depends on A it will need some of A’s methods in B.cleanup(). These methods only work before A was cleaned up.
Now I found that the AppStateManager detaches all states in the order they were attached in AppStateManager.cleanup() (called by Application.destroy()) which in case breaks my dependency since A was made unavailable before B. Is there any way to change this? (I don’t think so from looking at AppStateManager.cleanup()) Or should I maintain a list of attached AppStates myself since it’s not allowed to call AppStateManager.getStates() from outside…?
Now I switched to not clearing references other app states might need in their cleanup() - not so neat but should work. Maybe this kind of hygenie is fulsome anyways…
maybe you should just create the dependency of B inside of A instead of creating both outside then linking B to A.
So in your appstate A
- declare B
- in the update method just call B.update
That should mean when A gets cleaned up B goes away also
I would have to understand more about the use-case to really comment. I can’t picture why one app state would need another to clean itself up.
If you could describe what your app states are doing then maybe I have some ideas.
Ah ok I will try it:
For everything sky-related I introduced a new node (“skyNode”) that e.g. the sky dome and other geometries are attached to so to make the sky dome move with the player/camera only the skyNode needs a control that implements this following.
This skyNode is controlled and provided by an (base layer) app state, the sky dome and the other geometries are provided by other (higher layer) app states that retrieve a reference to the skyNode, do their things and are happy…
And on cleanup of the base layer app state this skyNode is detached from scenegraph and the reference to it is thrown away - and due to the fact that I attach base layer app state before the highe layer app states (so the latter one can use them already) the higher layer app states like e.g. that one that manages the sky dome have no chance to detach their geometries from the skyNode (which they technically don’t have to do since the skyNode is detached from scenegraph anyways… but they shouldn’t know about this fact, the skyNode they use may be any node) in their cleanup() because the reference to the skyNode is thrown away already (yes they don’t store it on their own - maybe that’s the solution?).
Thank you for your attention and patience @pspeed!
You could just assume in the child app states that if the parent isn’t there anymore then they don’t have to clean up their attachments since the parent would have done so already.
The only alternative is to detach your app states in a specific order (they will be cleaned up in the order they are detached). You’d have to wait for update() to run before they were fully cleaned up and then you could exit for real when the last base app state is cleaned up.
I think the “if my parent doesn’t exist then I must not need to detach from them anymore” way is probably the easiest approach.
I implemented this simple behavior according to your advise (thank you!), it’s easier than maintaining the specific detach order and as long as I don’t need it otherwise I’ll drive the “conditional detach”.