Hi guys,
I’ve just started looking into appstates for a little game I’ve been working on, but I’ve come unstuck at the first hurdle.
I’ve successfully managed to get a cube to display from my “GameState” state, and when the state is detached from the state manager the update method is no longer called (which is the desired functionality)
However when the state is no longer attached, the cube is still being rendered???
I understand conceptually why this is happening, but I’m not sure of the way around it. In my gamestate’s initialize method I call app.getRootNode().attachChild(GameStateNode);
Which of course means that even when the state is detached, the node is still connected to the applications rootnode.
Any ideas folks?
Have a bit of code.
GAMESTATE.JAVA
[java]public class GameState extends AbstractAppState{
private Node rootNode = new Node(“gameRoot”);
private SimpleApplication app;
int updated = 0;
public Node getNode()
{
return rootNode;
}
@Override
public void update(float tpf)
{
super.update(tpf);
updated++;
System.out.println(updated);
rootNode.getChild(0).rotate(tpf, tpf, tpf);
}
@Override
public void initialize(AppStateManager stateManager, Application applicooter)
{
super.initialize(stateManager, applicooter);
this.app = (SimpleApplication)applicooter;
Box b = new Box(Vector3f.ZERO, 1, 1, 1);
Geometry geom = new Geometry(“Box”, b);
Material mat = new Material(app.getAssetManager(), “Common/MatDefs/Misc/Unshaded.j3md”);
mat.setColor(“Color”, ColorRGBA.Blue);
geom.setMaterial(mat);
rootNode.attachChild(geom);
app.getRootNode().attachChild(rootNode);
}
}[/java]
MAIN.JAVA:
[java]public class Main extends SimpleApplication {
GameState game;
int loop = 0;
public static void main(String[] args) {
Main app = new Main();
app.start();
}
@Override
public void simpleInitApp() {
game = new GameState();
stateManager.attach(game);
game.initialize(stateManager, this);
}
@Override
// all I do here is detach and attach the state periodically.
// blue cube stays persistent throughout but the update loop behaves as
//expected
public void simpleUpdate(float tpf) {
loop++;
if(loop % 100 != 0)
{
stateManager.detach(game);
}
else
{
stateManager.attach(game);
}
}
@Override
public void simpleRender(RenderManager rm) {
//TODO: add render code
}
}[/java]
Any help would be appreciated. I’m hoping it’s some ridiculous rookie error.
Ollie.
When you detach the state, call this:
GameStateNode.detachFromParent();
Thanks for the reply Addez!
Literally just after I posted the topic I noticed a couple of methods that I can override, so I ended up with:
[java]public class GameState extends AbstractAppState{
@Override
public void stateDetached(AppStateManager stateManager) {
super.stateDetached(stateManager);
app.getRootNode().detachChild(rootNode);
}
@Override
public void stateAttached(AppStateManager stateManager) {
super.stateAttached(stateManager);
app.getRootNode().attachChild(rootNode);
}
private Node rootNode = new Node("gameRoot");
private SimpleApplication app;
int updated = 0;
public Node getNode()
{
return rootNode;
}
@Override
public void update(float tpf)
{
super.update(tpf);
updated++;
System.out.println(updated);
rootNode.rotate(tpf, tpf, tpf);
}
@Override
public void initialize(AppStateManager stateManager, Application applicooter)
{
super.initialize(stateManager, applicooter);
this.app = (SimpleApplication)applicooter;
Box b = new Box(Vector3f.ZERO, 1, 1, 1);
Geometry geom = new Geometry("Box", b);
Material mat = new Material(app.getAssetManager(), "Common/MatDefs/Misc/Unshaded.j3md");
mat.setColor("Color", ColorRGBA.Blue);
geom.setMaterial(mat);
rootNode.attachChild(geom);
app.getRootNode().attachChild(rootNode);
}
}[/java]
Which I like, because it keeps everything nicely self contained, and the main class can just attach and detach states at will, without knowing their implementation details.
Thanks again for the reply man. Next step is to learn about controls!
Ollie
@lolliver0342 said:
Next step is to learn about controls!
This post contains a small example on how to use controls, it uses new functions of the SDK to test it but in general its a simple example on what controls can do.
http://hub.jmonkeyengine.org/groups/jmonkeyplatform/forum/topic/load-and-edit-custom-controls-in-scenecomposer/
Thanks Normen!
I’ll take a look at that in a minute. Although my scene composer does not currently have the “custom control” option, and there’re no available updates in the JMP. Is there some kind of nightly update repository I can force JMP to use instead?
Ollie.
You can enable nightly updates but its advised to create a separate application for that, see the manual on updating (press F1 in the SDK) or this wiki entry, but the interesting part is probably the creation of the Control class itself, you can add it in-code as well. The new functions should go into the stable updates of the SDK in about a week or so.
Ahh I see. Thanks for the help Normen!
Out of interest what’s the best way to handle the transition between a “menu” appstate and a “game” appstate?
I considered having an actionlistener in my MenuState class which either closes on escape, or attaches a new GameState to the statemanager, but then there’s no way to detach the MenuState… and I started to get confused by it all!
Should there be a containing AppState which holds both the gamestate and the menustate? Or how is this usually handled?
Sorry to ask so many questions!
Ollie
Ahh! Just read “Tip: You can create AppStates that enable and disable sets of other AppStates, e.g. InGameState versus MainScreenState.” in the wiki, so I guess that answers my question…
Cheers,
Ollie