Class integration

In my game, I use multiple classes, and in my main class, all my variables that my other classes need to access are static. I have an extension of BasicGameState with a CameraNode of my camera. I also have an object called dynamicNode in my main class. I call this function in my overrided BasicGameState's update function.

cam.setLocalTranslation(new Vector3f(Main.dynamicNode.getWorldTranslation().x+20, Main.dynamicNode.getWorldTranslation().y+20, Main.dynamicNode.getWorldTranslation().z));


But, when I do this, it gives me a NullPointerException.

Sep 19, 2008 7:50:02 PM com.jmex.game.DefaultUncaughtExceptionHandler uncaughtException
SEVERE: Main game loop broken by uncaught exception
java.lang.NullPointerException
        at demoapp.MainGameState.update(MainGameState.java:67)
        at com.jmex.game.state.GameStateNode.update(GameStateNode.java:71)
        at com.jmex.game.StandardGame.update(StandardGame.java:353)
        at com.jmex.game.StandardGame.run(StandardGame.java:225)
        at java.lang.Thread.run(Thread.java:619)


I think it might run the function before my dynamicNode is created.

If you're moving the position of the camera, I believe that counts as "changing the geometry" and therefore needs to be done by injecting the call into the OpenGL thread via the GameTaskQueueManager.

I think it might run the function before my dynamicNode is created.

That's possible.  Without knowing more about the code I would just say to make sure the node is initialized, as well as your cam, before that method is called.  Might also consider the effect of calling "getWorldTranslation" if it's not attached to a parent node (unsure about this, as I have no way to try it out right now).

That being said - having a class to hold static non-final variables is more often than not a bad way to share data.  You may wish to use a singleton with "getter" methods.  This way you can more easily deal with possible synchronization issues, as opposed to directly accessing the static variables.

Otherwise, if this data is being used in more than one thread you're likely to get a ConcurrentModificationException at some unpredictable time.

Oh. That doesn't sound good. I will find another way to do what I was trying to do, I saw the ThirdPersonHandler, which will accomplish my objective even better. Does anyone know how to do it?

Hey! I just found an amazing learning resource! There are tests for every function in the src/jMEtest folder!

I saw the ThirdPersonHandler, which will accomplish my objective even better. Does anyone know how to do it?


What you might want to check out is the com.jme.input.ChaseCamera.  It's very cool!

Ok. I will.

Thanks! That is just what I needed. jME has a lot of cool functions to mess with like ThirdPersonHandler, ChaseCamera, and FirstPersonHandler! This is by far the best engine I have seen yet, and I have seen/tried many(Irrlicht, CrystalSpace, Torque, etc.) matching up to even commercial level engines.  :wink:

Oh, and I did solve my problem. I simply locked the game while I am initializing with StandardGame.lock() before init and StandardGame.unlock() after init.