Children of GuiNode

Hello! So, I am using the guiNode (not using NiftyGUI), and I would like to create a “subclass” of it, so that I can remove only certain children of the GUI without having to call guiNode.detachAllChildren() and then adding all the relevant children again! Basically like switching between appstates… so, if this is possible, what type should this child be of? A new guiNode? Node? :slight_smile:

(Some pseudocode if needed, the idea is clear in my head but I’m a bit tired so might not come out well)

[javan]
new Child1
new Child2

Attach all relevant pictures to each of the Children

if(playerWantsChild1) guiNode.attachChild(Child1)
if(playerWantsChild2) guiNode.attachChild(Child2)
if(playerWantstoKillChild1) guiNode.detachChild(Child1)
if(playerWantstoKillChild2) guiNode.detachChild(Child2)

[java]
new Child1
new Child2

Attach all relevant pictures to each of the Children

if(playerWantsChild1) guiNode.attachChild(Child1)
if(playerWantsChild2) guiNode.attachChild(Child2)
if(playerWantstoKillChild1) guiNode.detachChild(Child1)
if(playerWantstoKillChild2) guiNode.detachChild(Child2)[/java]

(my mistake, the code did not get this nifty code box, still new to this :-? )

I fail to see the need for a subclass.

There’s no need to detach all and then re-attach children. Why not simply loop over guiNode.getChildren() and detach the children you want to detach?

guiNode is an instance of com.jme3.scene.Node so if you were to create a subclass, that would be the superclass.

@SCOMS said: [java] new Child1 new Child2 … Attach all relevant pictures to each of the Children … if(playerWantsChild1) guiNode.attachChild(Child1) if(playerWantsChild2) guiNode.attachChild(Child2) if(playerWantstoKillChild1) guiNode.detachChild(Child1) if(playerWantstoKillChild2) guiNode.detachChild(Child2)[/java]

(my mistake, the code did not get this nifty code box, still new to this :-? )

[java]
if (child1.getParent() != null)
child1.removeFromParent();
[/java]

Will have the effect you are looking for.

Agreed. It is time to rethink your approach.

On the continuum of all possible solutions for this, subclassing Node and replacing guiNode have to be way down towards negative infinity. :wink:

@t0neg0d said: [java] if (child1.getParent() != null) child1.removeFromParent(); [/java]

Will have the effect you are looking for.

…and the parent check is unnecessary as removeFromParent() will work either way.

1 Like
@t0neg0d said: [java] if (child1.getParent() != null) child1.removeFromParent(); [/java]

Will have the effect you are looking for.

All right, thanks a lot for all the helpful answers (only quoting yours but all of them are really helpful!). I guess I’ll have to rethink my strategy… but the reason why I wanted to be able to remove large “blocks” from the GUI - realising now I probably should have mentioned this - is in the event of having quite a few items on the screen, which could make removing the individual pieces quite clumsy and ugly.

I guess it could be thought of like appstates again… Let’s say I want to swap from “menu-input” to “game-input”. I wouldn’t like to manually overwrite each and every mapped key, but rather just put them all once in an appstate and activate that.

Then put them in an app state. It can attach them on initialize and remove them on cleanup.